authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-21 17:02:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-21 23:21:07-07:00
logbe71195bba13256f0e0a955833b2ada3a27492fc
treeda8f3d5580fef0ee47996601cd409ee29cd441de
parent01f20c7f4891c7364486efb2d0f2e59df18dd1e4

stage2: enable f16 math

There was panic that said TODO add __trunctfhf2 to compiler-rt, but I checked and that function has been in compiler-rt since April.

4 files changed, 59 insertions(+), 60 deletions(-)

src/value.zig+17-22
...@@ -937,7 +937,7 @@ pub const Value = extern union {...@@ -937,7 +937,7 @@ pub const Value = extern union {
937 /// Asserts that the value is a float or an integer.937 /// Asserts that the value is a float or an integer.
938 pub fn toFloat(self: Value, comptime T: type) T {938 pub fn toFloat(self: Value, comptime T: type) T {
939 return switch (self.tag()) {939 return switch (self.tag()) {
940 .float_16 => @panic("TODO soft float"),940 .float_16 => @floatCast(T, self.castTag(.float_16).?.data),
941 .float_32 => @floatCast(T, self.castTag(.float_32).?.data),941 .float_32 => @floatCast(T, self.castTag(.float_32).?.data),
942 .float_64 => @floatCast(T, self.castTag(.float_64).?.data),942 .float_64 => @floatCast(T, self.castTag(.float_64).?.data),
943 .float_128 => @floatCast(T, self.castTag(.float_128).?.data),943 .float_128 => @floatCast(T, self.castTag(.float_128).?.data),
...@@ -1046,11 +1046,10 @@ pub const Value = extern union {...@@ -1046,11 +1046,10 @@ pub const Value = extern union {
1046 pub fn floatCast(self: Value, allocator: *Allocator, dest_ty: Type) !Value {1046 pub fn floatCast(self: Value, allocator: *Allocator, dest_ty: Type) !Value {
1047 switch (dest_ty.tag()) {1047 switch (dest_ty.tag()) {
1048 .f16 => {1048 .f16 => {
1049 @panic("TODO add __trunctfhf2 to compiler-rt");1049 const res = try Value.Tag.float_16.create(allocator, self.toFloat(f16));
1050 //const res = try Value.Tag.float_16.create(allocator, self.toFloat(f16));1050 if (!self.eql(res, dest_ty))
1051 //if (!self.eql(res))1051 return error.Overflow;
1052 // return error.Overflow;1052 return res;
1053 //return res;
1054 },1053 },
1055 .f32 => {1054 .f32 => {
1056 const res = try Value.Tag.float_32.create(allocator, self.toFloat(f32));1055 const res = try Value.Tag.float_32.create(allocator, self.toFloat(f32));
...@@ -1901,10 +1900,9 @@ pub const Value = extern union {...@@ -1901,10 +1900,9 @@ pub const Value = extern union {
1901 ) !Value {1900 ) !Value {
1902 switch (float_type.tag()) {1901 switch (float_type.tag()) {
1903 .f16 => {1902 .f16 => {
1904 @panic("TODO add __trunctfhf2 to compiler-rt");1903 const lhs_val = lhs.toFloat(f16);
1905 //const lhs_val = lhs.toFloat(f16);1904 const rhs_val = rhs.toFloat(f16);
1906 //const rhs_val = rhs.toFloat(f16);1905 return Value.Tag.float_16.create(arena, lhs_val + rhs_val);
1907 //return Value.Tag.float_16.create(arena, lhs_val + rhs_val);
1908 },1906 },
1909 .f32 => {1907 .f32 => {
1910 const lhs_val = lhs.toFloat(f32);1908 const lhs_val = lhs.toFloat(f32);
...@@ -1933,10 +1931,9 @@ pub const Value = extern union {...@@ -1933,10 +1931,9 @@ pub const Value = extern union {
1933 ) !Value {1931 ) !Value {
1934 switch (float_type.tag()) {1932 switch (float_type.tag()) {
1935 .f16 => {1933 .f16 => {
1936 @panic("TODO add __trunctfhf2 to compiler-rt");1934 const lhs_val = lhs.toFloat(f16);
1937 //const lhs_val = lhs.toFloat(f16);1935 const rhs_val = rhs.toFloat(f16);
1938 //const rhs_val = rhs.toFloat(f16);1936 return Value.Tag.float_16.create(arena, lhs_val - rhs_val);
1939 //return Value.Tag.float_16.create(arena, lhs_val - rhs_val);
1940 },1937 },
1941 .f32 => {1938 .f32 => {
1942 const lhs_val = lhs.toFloat(f32);1939 const lhs_val = lhs.toFloat(f32);
...@@ -1965,10 +1962,9 @@ pub const Value = extern union {...@@ -1965,10 +1962,9 @@ pub const Value = extern union {
1965 ) !Value {1962 ) !Value {
1966 switch (float_type.tag()) {1963 switch (float_type.tag()) {
1967 .f16 => {1964 .f16 => {
1968 @panic("TODO add __trunctfhf2 to compiler-rt");1965 const lhs_val = lhs.toFloat(f16);
1969 //const lhs_val = lhs.toFloat(f16);1966 const rhs_val = rhs.toFloat(f16);
1970 //const rhs_val = rhs.toFloat(f16);1967 return Value.Tag.float_16.create(arena, lhs_val / rhs_val);
1971 //return Value.Tag.float_16.create(arena, lhs_val / rhs_val);
1972 },1968 },
1973 .f32 => {1969 .f32 => {
1974 const lhs_val = lhs.toFloat(f32);1970 const lhs_val = lhs.toFloat(f32);
...@@ -1997,10 +1993,9 @@ pub const Value = extern union {...@@ -1997,10 +1993,9 @@ pub const Value = extern union {
1997 ) !Value {1993 ) !Value {
1998 switch (float_type.tag()) {1994 switch (float_type.tag()) {
1999 .f16 => {1995 .f16 => {
2000 @panic("TODO add __trunctfhf2 to compiler-rt");1996 const lhs_val = lhs.toFloat(f16);
2001 //const lhs_val = lhs.toFloat(f16);1997 const rhs_val = rhs.toFloat(f16);
2002 //const rhs_val = rhs.toFloat(f16);1998 return Value.Tag.float_16.create(arena, lhs_val * rhs_val);
2003 //return Value.Tag.float_16.create(arena, lhs_val * rhs_val);
2004 },1999 },
2005 .f32 => {2000 .f32 => {
2006 const lhs_val = lhs.toFloat(f32);2001 const lhs_val = lhs.toFloat(f32);
test/behavior.zig+2-1
...@@ -13,6 +13,7 @@ test {...@@ -13,6 +13,7 @@ test {
13 _ = @import("behavior/atomics.zig");13 _ = @import("behavior/atomics.zig");
14 _ = @import("behavior/sizeof_and_typeof.zig");14 _ = @import("behavior/sizeof_and_typeof.zig");
15 _ = @import("behavior/translate_c_macros.zig");15 _ = @import("behavior/translate_c_macros.zig");
16 _ = @import("behavior/widening.zig");
1617
17 if (builtin.zig_is_stage2) {18 if (builtin.zig_is_stage2) {
18 // When all comptime_memory.zig tests pass, #9646 can be closed.19 // When all comptime_memory.zig tests pass, #9646 can be closed.
...@@ -157,7 +158,7 @@ test {...@@ -157,7 +158,7 @@ test {
157 _ = @import("behavior/wasm.zig");158 _ = @import("behavior/wasm.zig");
158 }159 }
159 _ = @import("behavior/while.zig");160 _ = @import("behavior/while.zig");
160 _ = @import("behavior/widening.zig");161 _ = @import("behavior/widening_stage1.zig");
161 _ = @import("behavior/src.zig");162 _ = @import("behavior/src.zig");
162 _ = @import("behavior/translate_c_macros_stage1.zig");163 _ = @import("behavior/translate_c_macros_stage1.zig");
163 }164 }
test/behavior/widening.zig-37
...@@ -11,40 +11,3 @@ test "integer widening" {...@@ -11,40 +11,3 @@ test "integer widening" {
11 var f: u128 = e;11 var f: u128 = e;
12 try expect(f == a);12 try expect(f == a);
13}13}
14
15test "implicit unsigned integer to signed integer" {
16 var a: u8 = 250;
17 var b: i16 = a;
18 try expect(b == 250);
19}
20
21test "float widening" {
22 var a: f16 = 12.34;
23 var b: f32 = a;
24 var c: f64 = b;
25 var d: f128 = c;
26 try expect(a == b);
27 try expect(b == c);
28 try expect(c == d);
29}
30
31test "float widening f16 to f128" {
32 // TODO https://github.com/ziglang/zig/issues/3282
33 if (@import("builtin").target.cpu.arch == .aarch64) return error.SkipZigTest;
34 if (@import("builtin").target.cpu.arch == .powerpc64le) return error.SkipZigTest;
35
36 var x: f16 = 12.34;
37 var y: f128 = x;
38 try expect(x == y);
39}
40
41test "cast small unsigned to larger signed" {
42 try expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
43 try expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
44}
45fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
46 return x;
47}
48fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
49 return x;
50}
test/behavior/widening_stage1.zig created+40
...@@ -0,0 +1,40 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const mem = std.mem;
4
5test "implicit unsigned integer to signed integer" {
6 var a: u8 = 250;
7 var b: i16 = a;
8 try expect(b == 250);
9}
10
11test "float widening" {
12 var a: f16 = 12.34;
13 var b: f32 = a;
14 var c: f64 = b;
15 var d: f128 = c;
16 try expect(a == b);
17 try expect(b == c);
18 try expect(c == d);
19}
20
21test "float widening f16 to f128" {
22 // TODO https://github.com/ziglang/zig/issues/3282
23 if (@import("builtin").stage2_arch == .aarch64) return error.SkipZigTest;
24 if (@import("builtin").stage2_arch == .powerpc64le) return error.SkipZigTest;
25
26 var x: f16 = 12.34;
27 var y: f128 = x;
28 try expect(x == y);
29}
30
31test "cast small unsigned to larger signed" {
32 try expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
33 try expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
34}
35fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
36 return x;
37}
38fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
39 return x;
40}