authorgravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-06-30 01:44:54+02:00
committergravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-06-30 01:58:17+02:00
loga36d7b613185c28c508bcc7b0e8b580a0ffd5e28
treee4d95fe7b9b730be7a7db309431d294c56ad696d
parent27b02413dc3dacc7d784fe84ff8ba6cb0361842d

add std.math f16 inf support

refs #1122

3 files changed, 26 insertions(+), 1 deletions(-)

std/math/index.zig+3
......@@ -28,6 +28,9 @@ pub const f16_toint = 1.0 / f16_epsilon;
2828pub const nan_u16 = u16(0x7C01);
2929pub const nan_f16 = @bitCast(f16, nan_u16);
3030
31pub const inf_u16 = u16(0x7C00);
32pub const inf_f16 = @bitCast(f16, inf_u16);
33
3134pub const nan_u32 = u32(0x7F800001);
3235pub const nan_f32 = @bitCast(f32, nan_u32);
3336
std/math/inf.zig+1-1
......@@ -1,9 +1,9 @@
11const std = @import("../index.zig");
22const math = std.math;
3const assert = std.debug.assert;
43
54pub fn inf(comptime T: type) T {
65 return switch (T) {
6 f16 => @bitCast(f16, math.inf_u16),
77 f32 => @bitCast(f32, math.inf_u32),
88 f64 => @bitCast(f64, math.inf_u64),
99 else => @compileError("inf not implemented for " ++ @typeName(T)),
std/math/isinf.zig+22
......@@ -5,6 +5,10 @@ const assert = std.debug.assert;
55pub fn isInf(x: var) bool {
66 const T = @typeOf(x);
77 switch (T) {
8 f16 => {
9 const bits = @bitCast(u16, x);
10 return bits & 0x7FFF == 0x7C00;
11 },
812 f32 => {
913 const bits = @bitCast(u32, x);
1014 return bits & 0x7FFFFFFF == 0x7F800000;
......@@ -22,6 +26,9 @@ pub fn isInf(x: var) bool {
2226pub fn isPositiveInf(x: var) bool {
2327 const T = @typeOf(x);
2428 switch (T) {
29 f16 => {
30 return @bitCast(u16, x) == 0x7C00;
31 },
2532 f32 => {
2633 return @bitCast(u32, x) == 0x7F800000;
2734 },
......@@ -37,6 +44,9 @@ pub fn isPositiveInf(x: var) bool {
3744pub fn isNegativeInf(x: var) bool {
3845 const T = @typeOf(x);
3946 switch (T) {
47 f16 => {
48 return @bitCast(u16, x) == 0xFC00;
49 },
4050 f32 => {
4151 return @bitCast(u32, x) == 0xFF800000;
4252 },
......@@ -50,10 +60,14 @@ pub fn isNegativeInf(x: var) bool {
5060}
5161
5262test "math.isInf" {
63 assert(!isInf(f16(0.0)));
64 assert(!isInf(f16(-0.0)));
5365 assert(!isInf(f32(0.0)));
5466 assert(!isInf(f32(-0.0)));
5567 assert(!isInf(f64(0.0)));
5668 assert(!isInf(f64(-0.0)));
69 assert(isInf(math.inf(f16)));
70 assert(isInf(-math.inf(f16)));
5771 assert(isInf(math.inf(f32)));
5872 assert(isInf(-math.inf(f32)));
5973 assert(isInf(math.inf(f64)));
......@@ -61,10 +75,14 @@ test "math.isInf" {
6175}
6276
6377test "math.isPositiveInf" {
78 assert(!isPositiveInf(f16(0.0)));
79 assert(!isPositiveInf(f16(-0.0)));
6480 assert(!isPositiveInf(f32(0.0)));
6581 assert(!isPositiveInf(f32(-0.0)));
6682 assert(!isPositiveInf(f64(0.0)));
6783 assert(!isPositiveInf(f64(-0.0)));
84 assert(isPositiveInf(math.inf(f16)));
85 assert(!isPositiveInf(-math.inf(f16)));
6886 assert(isPositiveInf(math.inf(f32)));
6987 assert(!isPositiveInf(-math.inf(f32)));
7088 assert(isPositiveInf(math.inf(f64)));
......@@ -72,10 +90,14 @@ test "math.isPositiveInf" {
7290}
7391
7492test "math.isNegativeInf" {
93 assert(!isNegativeInf(f16(0.0)));
94 assert(!isNegativeInf(f16(-0.0)));
7595 assert(!isNegativeInf(f32(0.0)));
7696 assert(!isNegativeInf(f32(-0.0)));
7797 assert(!isNegativeInf(f64(0.0)));
7898 assert(!isNegativeInf(f64(-0.0)));
99 assert(!isNegativeInf(math.inf(f16)));
100 assert(isNegativeInf(-math.inf(f16)));
79101 assert(!isNegativeInf(math.inf(f32)));
80102 assert(isNegativeInf(-math.inf(f32)));
81103 assert(!isNegativeInf(math.inf(f64)));