Skip to content

Absorb std.math.big.rational logic into std.math.big.int; fix @intFromFloat safety check #24188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions lib/compiler/aro/aro/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,25 @@ pub fn floatToInt(v: *Value, dest_ty: Type, comp: *Compilation) !FloatToIntChang
return .out_of_range;
}

const had_fraction = @rem(float_val, 1) != 0;
const is_negative = std.math.signbit(float_val);
const floored = @floor(@abs(float_val));

var rational = try std.math.big.Rational.init(comp.gpa);
defer rational.deinit();
rational.setFloat(f128, floored) catch |err| switch (err) {
error.NonFiniteFloat => {
v.* = .{};
return .overflow;
},
error.OutOfMemory => return error.OutOfMemory,
};

// The float is reduced in rational.setFloat, so we assert that denominator is equal to one
const big_one = BigIntConst{ .limbs = &.{1}, .positive = true };
assert(rational.q.toConst().eqlAbs(big_one));

if (is_negative) {
rational.negate();
}

const signedness = dest_ty.signedness(comp);
const bits: usize = @intCast(dest_ty.bitSizeof(comp).?);

// rational.p.truncate(rational.p.toConst(), signedness: Signedness, bit_count: usize)
const fits = rational.p.fitsInTwosComp(signedness, bits);
v.* = try intern(comp, .{ .int = .{ .big_int = rational.p.toConst() } });
try rational.p.truncate(&rational.p, signedness, bits);
var big_int: std.math.big.int.Mutable = .{
.limbs = try comp.gpa.alloc(std.math.big.Limb, @max(
std.math.big.int.calcLimbLen(float_val),
std.math.big.int.calcTwosCompLimbCount(bits),
)),
.len = undefined,
.positive = undefined,
};
const had_fraction = switch (big_int.setFloat(float_val, .trunc)) {
.inexact => true,
.exact => false,
};

const fits = big_int.toConst().fitsInTwosComp(signedness, bits);
v.* = try intern(comp, .{ .int = .{ .big_int = big_int.toConst() } });
big_int.truncate(big_int.toConst(), signedness, bits);

if (!was_zero and v.isZero(comp)) return .nonzero_to_zero;
if (!fits) return .out_of_range;
Expand Down
1 change: 1 addition & 0 deletions lib/std/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub const rad_per_deg = 0.017453292519943295769236907684886127134428718885417254
/// 180.0/pi
pub const deg_per_rad = 57.295779513082320876798154814105170332405472466564321549160243861;

pub const FloatRepr = float.FloatRepr;
pub const floatExponentBits = float.floatExponentBits;
pub const floatMantissaBits = float.floatMantissaBits;
pub const floatFractionalBits = float.floatFractionalBits;
Expand Down
2 changes: 0 additions & 2 deletions lib/std/math/big.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("../std.zig");
const assert = std.debug.assert;

pub const Rational = @import("big/rational.zig").Rational;
pub const int = @import("big/int.zig");
pub const Limb = usize;
const limb_info = @typeInfo(Limb).int;
Expand All @@ -18,7 +17,6 @@ comptime {

test {
_ = int;
_ = Rational;
_ = Limb;
_ = SignedLimb;
_ = DoubleLimb;
Expand Down
Loading
Loading