@@ -148,35 +148,25 @@ pub fn floatToInt(v: *Value, dest_ty: Type, comp: *Compilation) !FloatToIntChang
148
148
return .out_of_range ;
149
149
}
150
150
151
- const had_fraction = @rem (float_val , 1 ) != 0 ;
152
- const is_negative = std .math .signbit (float_val );
153
- const floored = @floor (@abs (float_val ));
154
-
155
- var rational = try std .math .big .Rational .init (comp .gpa );
156
- defer rational .deinit ();
157
- rational .setFloat (f128 , floored ) catch | err | switch (err ) {
158
- error .NonFiniteFloat = > {
159
- v .* = .{};
160
- return .overflow ;
161
- },
162
- error .OutOfMemory = > return error .OutOfMemory ,
163
- };
164
-
165
- // The float is reduced in rational.setFloat, so we assert that denominator is equal to one
166
- const big_one = BigIntConst { .limbs = &.{1 }, .positive = true };
167
- assert (rational .q .toConst ().eqlAbs (big_one ));
168
-
169
- if (is_negative ) {
170
- rational .negate ();
171
- }
172
-
173
151
const signedness = dest_ty .signedness (comp );
174
152
const bits : usize = @intCast (dest_ty .bitSizeof (comp ).? );
175
153
176
- // rational.p.truncate(rational.p.toConst(), signedness: Signedness, bit_count: usize)
177
- const fits = rational .p .fitsInTwosComp (signedness , bits );
178
- v .* = try intern (comp , .{ .int = .{ .big_int = rational .p .toConst () } });
179
- try rational .p .truncate (& rational .p , signedness , bits );
154
+ var big_int : std.math.big.int.Mutable = .{
155
+ .limbs = try comp .gpa .alloc (std .math .big .Limb , @max (
156
+ std .math .big .int .calcLimbLen (float_val ),
157
+ std .math .big .int .calcTwosCompLimbCount (bits ),
158
+ )),
159
+ .len = undefined ,
160
+ .positive = undefined ,
161
+ };
162
+ const had_fraction = switch (big_int .setFloat (float_val , .trunc )) {
163
+ .inexact = > true ,
164
+ .exact = > false ,
165
+ };
166
+
167
+ const fits = big_int .toConst ().fitsInTwosComp (signedness , bits );
168
+ v .* = try intern (comp , .{ .int = .{ .big_int = big_int .toConst () } });
169
+ big_int .truncate (big_int .toConst (), signedness , bits );
180
170
181
171
if (! was_zero and v .isZero (comp )) return .nonzero_to_zero ;
182
172
if (! fits ) return .out_of_range ;
0 commit comments