Skip to content

Conversation

@xonx4l
Copy link
Contributor

@xonx4l xonx4l commented Dec 26, 2025

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

@github-actions github-actions bot added the common Related to common crate label Dec 26, 2025
@Jefffrey Jefffrey changed the title Add: f16 support to ScalarValue Add: PI upper/lower bound f16 constants to ScalarValue Dec 26, 2025
// Constants defined for scalar construction.

// Next F16 value above π (upper bound)
pub(super) const PI_UPPER_F16: half::f16 = half::f16::from_bits(0x4249);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@Jefffrey Jefffrey left a 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:

Constants look correct

@xonx4l
Copy link
Contributor Author

xonx4l commented Dec 26, 2025

Thanks for taking the time to write this . Looks great!

@Jefffrey Jefffrey added this pull request to the merge queue Dec 27, 2025
Merged via the queue into apache:main with commit ed7af0b Dec 27, 2025
28 checks passed
@Jefffrey
Copy link
Contributor

Thanks @xonx4l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support scalarvalue APIs for f16 regarding Pi values

2 participants