Coverage Report

Created: 2025-10-14 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/half-2.7.1/src/bfloat/convert.rs
Line
Count
Source
1
use crate::leading_zeros::leading_zeros_u16;
2
use zerocopy::transmute;
3
4
#[inline]
5
0
pub(crate) const fn f32_to_bf16(value: f32) -> u16 {
6
    // TODO: Replace transmute with to_bits() once to_bits is const-stabilized
7
    // Convert to raw bytes
8
0
    let x: u32 = transmute!(value);
9
10
    // check for NaN
11
0
    if x & 0x7FFF_FFFFu32 > 0x7F80_0000u32 {
12
        // Keep high part of current mantissa but also set most significiant mantissa bit
13
0
        return ((x >> 16) | 0x0040u32) as u16;
14
0
    }
15
16
    // round and shift
17
0
    let round_bit = 0x0000_8000u32;
18
0
    if (x & round_bit) != 0 && (x & (3 * round_bit - 1)) != 0 {
19
0
        (x >> 16) as u16 + 1
20
    } else {
21
0
        (x >> 16) as u16
22
    }
23
0
}
24
25
#[inline]
26
0
pub(crate) const fn f64_to_bf16(value: f64) -> u16 {
27
    // TODO: Replace transmute with to_bits() once to_bits is const-stabilized
28
    // Convert to raw bytes, truncating the last 32-bits of mantissa; that precision will always
29
    // be lost on half-precision.
30
0
    let val: u64 = transmute!(value);
31
0
    let x = (val >> 32) as u32;
32
33
    // Extract IEEE754 components
34
0
    let sign = x & 0x8000_0000u32;
35
0
    let exp = x & 0x7FF0_0000u32;
36
0
    let man = x & 0x000F_FFFFu32;
37
38
    // Check for all exponent bits being set, which is Infinity or NaN
39
0
    if exp == 0x7FF0_0000u32 {
40
        // Set mantissa MSB for NaN (and also keep shifted mantissa bits).
41
        // We also have to check the last 32 bits.
42
0
        let nan_bit = if man == 0 && (val as u32 == 0) {
43
0
            0
44
        } else {
45
0
            0x0040u32
46
        };
47
0
        return ((sign >> 16) | 0x7F80u32 | nan_bit | (man >> 13)) as u16;
48
0
    }
49
50
    // The number is normalized, start assembling half precision version
51
0
    let half_sign = sign >> 16;
52
    // Unbias the exponent, then bias for bfloat16 precision
53
0
    let unbiased_exp = ((exp >> 20) as i64) - 1023;
54
0
    let half_exp = unbiased_exp + 127;
55
56
    // Check for exponent overflow, return +infinity
57
0
    if half_exp >= 0xFF {
58
0
        return (half_sign | 0x7F80u32) as u16;
59
0
    }
60
61
    // Check for underflow
62
0
    if half_exp <= 0 {
63
        // Check mantissa for what we can do
64
0
        if 7 - half_exp > 21 {
65
            // No rounding possibility, so this is a full underflow, return signed zero
66
0
            return half_sign as u16;
67
0
        }
68
        // Don't forget about hidden leading mantissa bit when assembling mantissa
69
0
        let man = man | 0x0010_0000u32;
70
0
        let mut half_man = man >> (14 - half_exp);
71
        // Check for rounding
72
0
        let round_bit = 1 << (13 - half_exp);
73
0
        if (man & round_bit) != 0 && (man & (3 * round_bit - 1)) != 0 {
74
0
            half_man += 1;
75
0
        }
76
        // No exponent for subnormals
77
0
        return (half_sign | half_man) as u16;
78
0
    }
79
80
    // Rebias the exponent
81
0
    let half_exp = (half_exp as u32) << 7;
82
0
    let half_man = man >> 13;
83
    // Check for rounding
84
0
    let round_bit = 0x0000_1000u32;
85
0
    if (man & round_bit) != 0 && (man & (3 * round_bit - 1)) != 0 {
86
        // Round it
87
0
        ((half_sign | half_exp | half_man) + 1) as u16
88
    } else {
89
0
        (half_sign | half_exp | half_man) as u16
90
    }
91
0
}
92
93
#[inline]
94
0
pub(crate) const fn bf16_to_f32(i: u16) -> f32 {
95
    // TODO: Replace transmute with from_bits() once from_bits is const-stabilized
96
    // If NaN, keep current mantissa but also set most significiant mantissa bit
97
0
    if i & 0x7FFFu16 > 0x7F80u16 {
98
0
        transmute!((i as u32 | 0x0040u32) << 16)
99
    } else {
100
0
        transmute!((i as u32) << 16)
101
    }
102
0
}
103
104
#[inline]
105
0
pub(crate) const fn bf16_to_f64(i: u16) -> f64 {
106
    // TODO: Replace transmute with from_bits() once from_bits is const-stabilized
107
    // Check for signed zero
108
0
    if i & 0x7FFFu16 == 0 {
109
0
        return transmute!((i as u64) << 48);
110
0
    }
111
112
0
    let half_sign = (i & 0x8000u16) as u64;
113
0
    let half_exp = (i & 0x7F80u16) as u64;
114
0
    let half_man = (i & 0x007Fu16) as u64;
115
116
    // Check for an infinity or NaN when all exponent bits set
117
0
    if half_exp == 0x7F80u64 {
118
        // Check for signed infinity if mantissa is zero
119
0
        if half_man == 0 {
120
0
            return transmute!((half_sign << 48) | 0x7FF0_0000_0000_0000u64);
121
        } else {
122
            // NaN, keep current mantissa but also set most significiant mantissa bit
123
0
            return transmute!((half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 45));
124
        }
125
0
    }
126
127
    // Calculate double-precision components with adjusted exponent
128
0
    let sign = half_sign << 48;
129
    // Unbias exponent
130
0
    let unbiased_exp = ((half_exp as i64) >> 7) - 127;
131
132
    // Check for subnormals, which will be normalized by adjusting exponent
133
0
    if half_exp == 0 {
134
        // Calculate how much to adjust the exponent by
135
0
        let e = leading_zeros_u16(half_man as u16) - 9;
136
137
        // Rebias and adjust exponent
138
0
        let exp = ((1023 - 127 - e) as u64) << 52;
139
0
        let man = (half_man << (46 + e)) & 0xF_FFFF_FFFF_FFFFu64;
140
0
        return transmute!(sign | exp | man);
141
0
    }
142
    // Rebias exponent for a normalized normal
143
0
    let exp = ((unbiased_exp + 1023) as u64) << 52;
144
0
    let man = (half_man & 0x007Fu64) << 45;
145
0
    transmute!(sign | exp | man)
146
0
}