-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add: PI upper/lower bound f16 constants to ScalarValue #19497
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
Conversation
| // Constants defined for scalar construction. | ||
|
|
||
| // Next F16 value above π (upper bound) | ||
| pub(super) const PI_UPPER_F16: half::f16 = half::f16::from_bits(0x4249); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How/where are these constants sourced from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the IEEE 754 binary16 half-precision bit representations closest to pi.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I converted std::f64::consts::PI to f16 which results in 0x4248 (approx 3.1406). Since this value is slightly lower than the actual Pi. I used the next representable bit pattern (0x4249) to ensure a strict upper bound.
Jefffrey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some quick sanity checks on Rust playground with following code:
use half::f16;
fn main() {
println!("f16 PI + next_up");
println!("0x{:X}", (f16::PI).to_bits());
println!("0x{:X}\n", next_up(f16::PI).to_bits());
println!("f16 -PI + next_down");
println!("0x{:X}", (-f16::PI).to_bits());
println!("0x{:X}\n", next_down(-f16::PI).to_bits());
println!("f16 FRAC_PI_2 + next_up");
println!("0x{:X}", (f16::FRAC_PI_2).to_bits());
println!("0x{:X}\n", next_up(f16::FRAC_PI_2).to_bits());
println!("f16 -FRAC_PI_2 + next_down");
println!("0x{:X}", (-f16::FRAC_PI_2).to_bits());
println!("0x{:X}\n", next_down(-f16::FRAC_PI_2).to_bits());
}
pub fn next_up(v: f16) -> f16 {
let bits = v.to_bits();
let sign_mask = 0x8000u16;
let abs = bits & !sign_mask;
let next_bits = if abs == 0 {
let tiny_bits = 0x1u16;
tiny_bits
} else if bits == abs {
bits + 1
} else {
bits - 1
};
f16::from_bits(next_bits)
}
pub fn next_down(v: f16) -> f16 {
let bits = v.to_bits();
let sign_mask = 0x8000u16;
let abs = bits & !sign_mask;
let next_bits = if abs == 0 {
let tiny_bits = 0x1u16 | sign_mask;
tiny_bits
} else if bits == abs {
bits - 1
} else {
bits + 1
};
f16::from_bits(next_bits)
}Adapted next_up and next_down from f32/f64 to f16, see references:
- https://docs.rs/half/latest/src/half/binary16.rs.html#764
- https://doc.rust-lang.org/src/core/num/f32.rs.html#754
Constants look correct
|
Thanks for taking the time to write this . Looks great! |
|
Thanks @xonx4l |
Which issue does this PR close?
Rationale for this change
To add Support scalarvalue APIs for f16 regarding Pi values
What changes are included in this PR?
Added half::f16 support to ScalarValue mathematical APIs.
Are these changes tested?
Yes
Are there any user-facing changes?
Yes