aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-infer / src / half.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! IEEE binary16 and bfloat16 to f32 without std. |
| 17 | |
| 18 | /// Convert an IEEE 754 binary16 bit pattern to f32 (exact for all inputs, |
| 19 | /// including subnormals, infinities and NaNs). |
| 20 | pub fn f16_to_f32(h: u16) -> f32 { |
| 21 | let sign = ((h >> 15) as u32) << 31; |
| 22 | let exp = ((h >> 10) & 0x1f) as u32; |
| 23 | let man = (h & 0x3ff) as u32; |
| 24 | let bits = if exp == 0 { |
| 25 | if man == 0 { |
| 26 | sign |
| 27 | } else { |
| 28 | // Subnormal: normalise. |
| 29 | let shift = man.leading_zeros() - 21; // man has <= 10 bits |
| 30 | let m = (man << shift) & 0x3ff; |
| 31 | let e = 113 - shift; // 127 - 15 + 1 - shift |
| 32 | sign | (e << 23) | (m << 13) |
| 33 | } |
| 34 | } else if exp == 0x1f { |
| 35 | sign | 0x7f80_0000 | (man << 13) |
| 36 | } else { |
| 37 | sign | ((exp + 112) << 23) | (man << 13) |
| 38 | }; |
| 39 | f32::from_bits(bits) |
| 40 | } |
| 41 | |
| 42 | /// Convert a bfloat16 bit pattern to f32. |
| 43 | pub fn bf16_to_f32(b: u16) -> f32 { |
| 44 | f32::from_bits((b as u32) << 16) |
| 45 | } |
| 46 | |
| 47 | #[cfg(test)] |
| 48 | mod tests { |
| 49 | use super::*; |
| 50 | |
| 51 | #[test] |
| 52 | fn known_values() { |
| 53 | assert_eq!(f16_to_f32(0x0000), 0.0); |
| 54 | assert_eq!(f16_to_f32(0x3c00), 1.0); |
| 55 | assert_eq!(f16_to_f32(0xc000), -2.0); |
| 56 | assert_eq!(f16_to_f32(0x3800), 0.5); |
| 57 | assert_eq!(f16_to_f32(0x7bff), 65504.0); |
| 58 | assert_eq!(f16_to_f32(0x0400), f32::from_bits(0x3880_0000)); // 2^-14 smallest normal |
| 59 | assert_eq!(f16_to_f32(0x0001), f32::from_bits(0x3380_0000)); // 2^-24 smallest subnormal |
| 60 | assert_eq!(f16_to_f32(0x03ff), 1023.0 / 16_777_216.0); // 1023 * 2^-24 |
| 61 | assert!(f16_to_f32(0x7c00).is_infinite()); |
| 62 | assert!(f16_to_f32(0xfc00).is_sign_negative()); |
| 63 | assert!(f16_to_f32(0x7e00).is_nan()); |
| 64 | assert_eq!(f16_to_f32(0x8000).to_bits(), 0x8000_0000); |
| 65 | assert_eq!(bf16_to_f32(0x3f80), 1.0); |
| 66 | } |
| 67 | } |