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;...@@ -28,6 +28,9 @@ pub const f16_toint = 1.0 / f16_epsilon;
28pub const nan_u16 = u16(0x7C01);28pub const nan_u16 = u16(0x7C01);
29pub const nan_f16 = @bitCast(f16, nan_u16);29pub const nan_f16 = @bitCast(f16, nan_u16);
3030
31pub const inf_u16 = u16(0x7C00);
32pub const inf_f16 = @bitCast(f16, inf_u16);
33
31pub const nan_u32 = u32(0x7F800001);34pub const nan_u32 = u32(0x7F800001);
32pub const nan_f32 = @bitCast(f32, nan_u32);35pub const nan_f32 = @bitCast(f32, nan_u32);
3336
std/math/inf.zig+1-1
...@@ -1,9 +1,9 @@...@@ -1,9 +1,9 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const math = std.math;2const math = std.math;
3const assert = std.debug.assert;
43
5pub fn inf(comptime T: type) T {4pub fn inf(comptime T: type) T {
6 return switch (T) {5 return switch (T) {
6 f16 => @bitCast(f16, math.inf_u16),
7 f32 => @bitCast(f32, math.inf_u32),7 f32 => @bitCast(f32, math.inf_u32),
8 f64 => @bitCast(f64, math.inf_u64),8 f64 => @bitCast(f64, math.inf_u64),
9 else => @compileError("inf not implemented for " ++ @typeName(T)),9 else => @compileError("inf not implemented for " ++ @typeName(T)),
std/math/isinf.zig+22
...@@ -5,6 +5,10 @@ const assert = std.debug.assert;...@@ -5,6 +5,10 @@ const assert = std.debug.assert;
5pub fn isInf(x: var) bool {5pub fn isInf(x: var) bool {
6 const T = @typeOf(x);6 const T = @typeOf(x);
7 switch (T) {7 switch (T) {
8 f16 => {
9 const bits = @bitCast(u16, x);
10 return bits & 0x7FFF == 0x7C00;
11 },
8 f32 => {12 f32 => {
9 const bits = @bitCast(u32, x);13 const bits = @bitCast(u32, x);
10 return bits & 0x7FFFFFFF == 0x7F800000;14 return bits & 0x7FFFFFFF == 0x7F800000;
...@@ -22,6 +26,9 @@ pub fn isInf(x: var) bool {...@@ -22,6 +26,9 @@ pub fn isInf(x: var) bool {
22pub fn isPositiveInf(x: var) bool {26pub fn isPositiveInf(x: var) bool {
23 const T = @typeOf(x);27 const T = @typeOf(x);
24 switch (T) {28 switch (T) {
29 f16 => {
30 return @bitCast(u16, x) == 0x7C00;
31 },
25 f32 => {32 f32 => {
26 return @bitCast(u32, x) == 0x7F800000;33 return @bitCast(u32, x) == 0x7F800000;
27 },34 },
...@@ -37,6 +44,9 @@ pub fn isPositiveInf(x: var) bool {...@@ -37,6 +44,9 @@ pub fn isPositiveInf(x: var) bool {
37pub fn isNegativeInf(x: var) bool {44pub fn isNegativeInf(x: var) bool {
38 const T = @typeOf(x);45 const T = @typeOf(x);
39 switch (T) {46 switch (T) {
47 f16 => {
48 return @bitCast(u16, x) == 0xFC00;
49 },
40 f32 => {50 f32 => {
41 return @bitCast(u32, x) == 0xFF800000;51 return @bitCast(u32, x) == 0xFF800000;
42 },52 },
...@@ -50,10 +60,14 @@ pub fn isNegativeInf(x: var) bool {...@@ -50,10 +60,14 @@ pub fn isNegativeInf(x: var) bool {
50}60}
5161
52test "math.isInf" {62test "math.isInf" {
63 assert(!isInf(f16(0.0)));
64 assert(!isInf(f16(-0.0)));
53 assert(!isInf(f32(0.0)));65 assert(!isInf(f32(0.0)));
54 assert(!isInf(f32(-0.0)));66 assert(!isInf(f32(-0.0)));
55 assert(!isInf(f64(0.0)));67 assert(!isInf(f64(0.0)));
56 assert(!isInf(f64(-0.0)));68 assert(!isInf(f64(-0.0)));
69 assert(isInf(math.inf(f16)));
70 assert(isInf(-math.inf(f16)));
57 assert(isInf(math.inf(f32)));71 assert(isInf(math.inf(f32)));
58 assert(isInf(-math.inf(f32)));72 assert(isInf(-math.inf(f32)));
59 assert(isInf(math.inf(f64)));73 assert(isInf(math.inf(f64)));
...@@ -61,10 +75,14 @@ test "math.isInf" {...@@ -61,10 +75,14 @@ test "math.isInf" {
61}75}
6276
63test "math.isPositiveInf" {77test "math.isPositiveInf" {
78 assert(!isPositiveInf(f16(0.0)));
79 assert(!isPositiveInf(f16(-0.0)));
64 assert(!isPositiveInf(f32(0.0)));80 assert(!isPositiveInf(f32(0.0)));
65 assert(!isPositiveInf(f32(-0.0)));81 assert(!isPositiveInf(f32(-0.0)));
66 assert(!isPositiveInf(f64(0.0)));82 assert(!isPositiveInf(f64(0.0)));
67 assert(!isPositiveInf(f64(-0.0)));83 assert(!isPositiveInf(f64(-0.0)));
84 assert(isPositiveInf(math.inf(f16)));
85 assert(!isPositiveInf(-math.inf(f16)));
68 assert(isPositiveInf(math.inf(f32)));86 assert(isPositiveInf(math.inf(f32)));
69 assert(!isPositiveInf(-math.inf(f32)));87 assert(!isPositiveInf(-math.inf(f32)));
70 assert(isPositiveInf(math.inf(f64)));88 assert(isPositiveInf(math.inf(f64)));
...@@ -72,10 +90,14 @@ test "math.isPositiveInf" {...@@ -72,10 +90,14 @@ test "math.isPositiveInf" {
72}90}
7391
74test "math.isNegativeInf" {92test "math.isNegativeInf" {
93 assert(!isNegativeInf(f16(0.0)));
94 assert(!isNegativeInf(f16(-0.0)));
75 assert(!isNegativeInf(f32(0.0)));95 assert(!isNegativeInf(f32(0.0)));
76 assert(!isNegativeInf(f32(-0.0)));96 assert(!isNegativeInf(f32(-0.0)));
77 assert(!isNegativeInf(f64(0.0)));97 assert(!isNegativeInf(f64(0.0)));
78 assert(!isNegativeInf(f64(-0.0)));98 assert(!isNegativeInf(f64(-0.0)));
99 assert(!isNegativeInf(math.inf(f16)));
100 assert(isNegativeInf(-math.inf(f16)));
79 assert(!isNegativeInf(math.inf(f32)));101 assert(!isNegativeInf(math.inf(f32)));
80 assert(isNegativeInf(-math.inf(f32)));102 assert(isNegativeInf(-math.inf(f32)));
81 assert(!isNegativeInf(math.inf(f64)));103 assert(!isNegativeInf(math.inf(f64)));