Skip to content

Correctly rounded floating point div_euclid. #134145

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
div_euclid: add a fast path for overflow
  • Loading branch information
tczajka committed Dec 12, 2024
commit d0f8bae821930eb0a4e34b0326f36bb3ff374ef5
12 changes: 10 additions & 2 deletions library/std/src/f128/soft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn div_floor_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
// `q` fits in `Bits` because `a.mantissa < 2 * b.mantissa`.
let q: Bits = (aa / bb).wrap_u128();
q as FP
} else {
} else if exp <= FP::MAX_EXP {
// exp >= Bits::BITS
let aa = DoubleBits::from(a.mantissa) << (Bits::BITS - 1);
let bb = DoubleBits::from(b.mantissa);
Expand Down Expand Up @@ -256,6 +256,10 @@ fn div_floor_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
q | 1
};
q_adj as FP * pow2(e)
} else {
// a.mantissa / b.mantissa >= 1/2, hence
// a / b >= 1/2 * 2^(MAX_EXP+1) = 2^MAX_EXP
FP::INFINITY
}
}

Expand All @@ -282,7 +286,7 @@ fn div_ceil_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
// <= 2^Bits::BITS - 2^(Bits::BITS - SIGNIFICAND_BITS) + 1 <= Bits::MAX
let q = ((aa - U256::ONE) / bb).wrap_u128() + 1;
q as FP
} else {
} else if exp <= FP::MAX_EXP {
let aa = DoubleBits::from(a.mantissa) << (Bits::BITS - 1);
let bb = DoubleBits::from(b.mantissa);
// e > 0
Expand Down Expand Up @@ -346,5 +350,9 @@ fn div_ceil_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
q + 1
};
q_adj as FP * pow2(e)
} else {
// a.mantissa / b.mantissa >= 1/2, hence
// a / b >= 1/2 * 2^(MAX_EXP+1) = 2^MAX_EXP
FP::INFINITY
}
}
12 changes: 10 additions & 2 deletions library/std/src/f32/soft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn div_floor_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
// `q` fits in `Bits` because `a.mantissa < 2 * b.mantissa`.
let q = (aa / bb) as Bits;
q as FP
} else {
} else if exp <= FP::MAX_EXP {
// exp >= Bits::BITS
let aa = DoubleBits::from(a.mantissa) << (Bits::BITS - 1);
let bb = DoubleBits::from(b.mantissa);
Expand Down Expand Up @@ -252,6 +252,10 @@ fn div_floor_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
q | 1
};
q_adj as FP * pow2(e)
} else {
// a.mantissa / b.mantissa >= 1/2, hence
// a / b >= 1/2 * 2^(MAX_EXP+1) = 2^MAX_EXP
FP::INFINITY
}
}

Expand All @@ -277,7 +281,7 @@ fn div_ceil_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
// <= 2^Bits::BITS - 2^(Bits::BITS - SIGNIFICAND_BITS) + 1 <= Bits::MAX
let q = ((aa - 1) / bb) as Bits + 1;
q as FP
} else {
} else if exp <= FP::MAX_EXP {
let aa = DoubleBits::from(a.mantissa) << (Bits::BITS - 1);
let bb = DoubleBits::from(b.mantissa);
// e > 0
Expand Down Expand Up @@ -340,5 +344,9 @@ fn div_ceil_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
q + 1
};
q_adj as FP * pow2(e)
} else {
// a.mantissa / b.mantissa >= 1/2, hence
// a / b >= 1/2 * 2^(MAX_EXP+1) = 2^MAX_EXP
FP::INFINITY
}
}
12 changes: 10 additions & 2 deletions library/std/src/f64/soft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn div_floor_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
// `q` fits in `Bits` because `a.mantissa < 2 * b.mantissa`.
let q = (aa / bb) as Bits;
q as FP
} else {
} else if exp <= FP::MAX_EXP {
// exp >= Bits::BITS
let aa = DoubleBits::from(a.mantissa) << (Bits::BITS - 1);
let bb = DoubleBits::from(b.mantissa);
Expand Down Expand Up @@ -252,6 +252,10 @@ fn div_floor_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
q | 1
};
q_adj as FP * pow2(e)
} else {
// a.mantissa / b.mantissa >= 1/2, hence
// a / b >= 1/2 * 2^(MAX_EXP+1) = 2^MAX_EXP
FP::INFINITY
}
}

Expand All @@ -277,7 +281,7 @@ fn div_ceil_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
// <= 2^Bits::BITS - 2^(Bits::BITS - SIGNIFICAND_BITS) + 1 <= Bits::MAX
let q = ((aa - 1) / bb) as Bits + 1;
q as FP
} else {
} else if exp <= FP::MAX_EXP {
let aa = DoubleBits::from(a.mantissa) << (Bits::BITS - 1);
let bb = DoubleBits::from(b.mantissa);
// e > 0
Expand Down Expand Up @@ -340,5 +344,9 @@ fn div_ceil_pos_finite(a: PositiveFinite, b: PositiveFinite) -> FP {
q + 1
};
q_adj as FP * pow2(e)
} else {
// a.mantissa / b.mantissa >= 1/2, hence
// a / b >= 1/2 * 2^(MAX_EXP+1) = 2^MAX_EXP
FP::INFINITY
}
}
Loading