| ... | @@ -1853,17 +1853,26 @@ pub const Value = extern union { | ... | @@ -1853,17 +1853,26 @@ pub const Value = extern union { |
| 1853 | }; | 1853 | }; |
| 1854 | } | 1854 | } |
| 1855 | | 1855 | |
| 1856 | pub fn intToFloat(val: Value, allocator: *Allocator, dest_ty: Type, target: Target) !Value { | 1856 | pub fn intToFloat(val: Value, arena: *Allocator, dest_ty: Type, target: Target) !Value { |
| 1857 | switch (val.tag()) { | 1857 | switch (val.tag()) { |
| 1858 | .undef, .zero, .one => return val, | 1858 | .undef, .zero, .one => return val, |
| 1859 | .the_only_possible_value => return Value.initTag(.zero), // for i0, u0 | 1859 | .the_only_possible_value => return Value.initTag(.zero), // for i0, u0 |
| 1860 | .int_u64 => { | 1860 | .int_u64 => { |
| 1861 | return intToFloatInner(val.castTag(.int_u64).?.data, allocator, dest_ty, target); | 1861 | return intToFloatInner(val.castTag(.int_u64).?.data, arena, dest_ty, target); |
| 1862 | }, | 1862 | }, |
| 1863 | .int_i64 => { | 1863 | .int_i64 => { |
| 1864 | return intToFloatInner(val.castTag(.int_i64).?.data, allocator, dest_ty, target); | 1864 | return intToFloatInner(val.castTag(.int_i64).?.data, arena, dest_ty, target); |
| | 1865 | }, |
| | 1866 | .int_big_positive => { |
| | 1867 | const limbs = val.castTag(.int_big_positive).?.data; |
| | 1868 | const float = bigIntToFloat(limbs, true); |
| | 1869 | return floatToValue(float, arena, dest_ty, target); |
| | 1870 | }, |
| | 1871 | .int_big_negative => { |
| | 1872 | const limbs = val.castTag(.int_big_negative).?.data; |
| | 1873 | const float = bigIntToFloat(limbs, false); |
| | 1874 | return floatToValue(float, arena, dest_ty, target); |
| 1865 | }, | 1875 | }, |
| 1866 | .int_big_positive, .int_big_negative => @panic("big int to float"), | | |
| 1867 | else => unreachable, | 1876 | else => unreachable, |
| 1868 | } | 1877 | } |
| 1869 | } | 1878 | } |
| ... | @@ -1878,6 +1887,16 @@ pub const Value = extern union { | ... | @@ -1878,6 +1887,16 @@ pub const Value = extern union { |
| 1878 | } | 1887 | } |
| 1879 | } | 1888 | } |
| 1880 | | 1889 | |
| | 1890 | fn floatToValue(float: f128, arena: *Allocator, dest_ty: Type, target: Target) !Value { |
| | 1891 | switch (dest_ty.floatBits(target)) { |
| | 1892 | 16 => return Value.Tag.float_16.create(arena, @floatCast(f16, float)), |
| | 1893 | 32 => return Value.Tag.float_32.create(arena, @floatCast(f32, float)), |
| | 1894 | 64 => return Value.Tag.float_64.create(arena, @floatCast(f64, float)), |
| | 1895 | 128 => return Value.Tag.float_128.create(arena, float), |
| | 1896 | else => unreachable, |
| | 1897 | } |
| | 1898 | } |
| | 1899 | |
| 1881 | /// Supports both floats and ints; handles undefined. | 1900 | /// Supports both floats and ints; handles undefined. |
| 1882 | pub fn numberAddWrap( | 1901 | pub fn numberAddWrap( |
| 1883 | lhs: Value, | 1902 | lhs: Value, |