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-fpu-probe / src / lib.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 | //! FP/SIMD probe: one unit that uses f32 math and NEON, called from the C |
| 17 | //! kernel (which is built -mgeneral-regs-only and never touches FP itself). |
| 18 | //! Inputs go through black_box so LLVM cannot constant-fold the FP work away |
| 19 | //! (the gate must execute real FP/SIMD instructions). No alloc, no std, no panics reachable (panic=abort, handler spins). |
| 20 | #![no_std] |
| 21 | |
| 22 | #[cfg(target_arch = "aarch64")] |
| 23 | use core::arch::aarch64::*; |
| 24 | |
| 25 | #[panic_handler] |
| 26 | fn panic(_: &core::panic::PanicInfo) -> ! { |
| 27 | loop {} |
| 28 | } |
| 29 | |
| 30 | /// Words written to `out` by `aienos_fpu_probe`. |
| 31 | pub const OUT_WORDS: usize = 6; |
| 32 | |
| 33 | /// exp(x) for |x| < 80: x*log2(e) = n + f, 2^f by a degree-6 polynomial |
| 34 | /// (Taylor of e^(f*ln2), f in [0,1)), 2^n by building the exponent bits. |
| 35 | fn exp_approx(x: f32) -> f32 { |
| 36 | let t = x * core::f32::consts::LOG2_E; |
| 37 | let mut n = t as i32; // truncates toward zero |
| 38 | if (n as f32) > t { |
| 39 | n -= 1; // floor |
| 40 | } |
| 41 | let f = (t - n as f32) * core::f32::consts::LN_2; // in [0, ln2) |
| 42 | let p = 1.0 |
| 43 | + f * (1.0 |
| 44 | + f * (0.5 |
| 45 | + f * (1.0 / 6.0 + f * (1.0 / 24.0 + f * (1.0 / 120.0 + f * (1.0 / 720.0)))))); |
| 46 | let scale = f32::from_bits(((n + 127) as u32) << 23); |
| 47 | p * scale |
| 48 | } |
| 49 | |
| 50 | /// Writes six u32 words to `out`: |
| 51 | /// [0] bits of dot(a, b) with a[i]=i+1, b[i]=0.5 (i in 0..16): exactly 68.0 |
| 52 | /// [1] bits of exp_approx(1.0) |
| 53 | /// [2..6] NEON vaddq_f32([1,2,3,4], [10,20,30,40]) lanes as bits: 11,22,33,44 |
| 54 | /// Returns 0 when all three agree with their known answers (dot exact, |
| 55 | /// exp(1) within 1e-4, NEON exact), else a bit mask of the failing parts |
| 56 | /// (1 dot, 2 exp, 4 neon), or -1 for a null `out`. |
| 57 | /// |
| 58 | /// # Safety |
| 59 | /// `out` must point to at least 6 writable u32. FP/SIMD must be enabled for |
| 60 | /// the calling exception level (CPACR_EL1.FPEN) and interrupts masked: the |
| 61 | /// kernel does not save FP state across exceptions. |
| 62 | #[no_mangle] |
| 63 | pub unsafe extern "C" fn aienos_fpu_probe(out: *mut u32) -> i32 { |
| 64 | if out.is_null() { |
| 65 | return -1; |
| 66 | } |
| 67 | let mut a = [0.0f32; 16]; |
| 68 | let mut b = [0.0f32; 16]; |
| 69 | let mut i = 0; |
| 70 | while i < 16 { |
| 71 | a[i] = core::hint::black_box((i + 1) as f32); |
| 72 | b[i] = core::hint::black_box(0.5f32); |
| 73 | i += 1; |
| 74 | } |
| 75 | let mut dot = 0.0f32; |
| 76 | i = 0; |
| 77 | while i < 16 { |
| 78 | dot += a[i] * b[i]; |
| 79 | i += 1; |
| 80 | } |
| 81 | let e = exp_approx(core::hint::black_box(1.0f32)); |
| 82 | |
| 83 | let mut lanes = [0.0f32; 4]; |
| 84 | #[cfg(target_arch = "aarch64")] |
| 85 | { |
| 86 | let x = core::hint::black_box([1.0f32, 2.0, 3.0, 4.0]); |
| 87 | let y = core::hint::black_box([10.0f32, 20.0, 30.0, 40.0]); |
| 88 | let s = vaddq_f32(vld1q_f32(x.as_ptr()), vld1q_f32(y.as_ptr())); |
| 89 | vst1q_f32(lanes.as_mut_ptr(), s); |
| 90 | } |
| 91 | |
| 92 | out.add(0).write_volatile(dot.to_bits()); |
| 93 | out.add(1).write_volatile(e.to_bits()); |
| 94 | i = 0; |
| 95 | while i < 4 { |
| 96 | out.add(2 + i).write_volatile(lanes[i].to_bits()); |
| 97 | i += 1; |
| 98 | } |
| 99 | |
| 100 | let mut rc = 0; |
| 101 | if dot != 68.0 { |
| 102 | rc |= 1; |
| 103 | } |
| 104 | let d = e - core::f32::consts::E; |
| 105 | if !(d < 1e-4 && d > -1e-4) { |
| 106 | rc |= 2; |
| 107 | } |
| 108 | if lanes != [11.0, 22.0, 33.0, 44.0] { |
| 109 | rc |= 4; |
| 110 | } |
| 111 | rc |
| 112 | } |