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