nx_f32_sincos.nx
buildroot/runtime/nx_f32_sincos.nx
about
nx_f32_sincos.nx -- IEEE 754 binary32 sin(x) + cos(x) bits-up.
L6 transcendentals. Composes L4 mul/add/sub/cvt. No libm.
Algorithm (clean-room from cited refs):
1. Range reduction:
k = round(x * 2/pi)
r = x - k * pi/2, r in [-pi/4, pi/4]
Quadrant q = k mod 4.
2. Reduced-range polynomials (Taylor, 4 odd / 4 even terms):
sin(r) ~= r - r^3/6 + r^5/120 - r^7/5040
~= r * (1 - r^2/6 + r^4/120 - r^6/5040)
cos(r) ~= 1 - r^2/2 + r^4/24 - r^6/720
3. Quadrant dispatch:
sin: q=0 -> sin(r) ; q=1 -> cos(r) ; q=2 -> -sin(r) ; q=3 -> -cos(r)
cos: q=0 -> cos(r) ; q=1 -> -sin(r) ; q=2 -> -cos(r) ; q=3 -> sin(r)
References absorbed clean-room:
Hart 1968, Cody+Waite 1980, Muller 2016
Standard quadrant-based range-reduction canon
v1 accuracy: ~1000-10000 ULPs (Taylor truncation + range reduction
loss; classic single-word pi/2 issue). Adequate for ML positional
encoding (RoPE). v2 Remez minimax + Cody-Waite split pi/2 for
tighter bounds queued.
genealogy_id: standard_quadrant_reduction + taylor_horner_canon
lineage_id: substrate_f32_sincos_v1_taylor4
dependencies 4 imports · 9 importers
imports: nx_syscalls.nxnx_tier.nxnx_f32.nxnx_f32_cvt.nx
imported by: nx_autograd_tensor.nxnx_f32_rope.nxnx_f32_rope_test.nxnx_f32_sincos_test.nxnx_trig_kat.nxnx_tts_formant.nxnx_tts_seq.nxnx_tts_speak.nxnx_tts_word.nx
structs
| none |
consts
| 37 | const NX_MAGIC_4294967295: i64 = 4294967295 |
| 40 | const NX_F32_SC_ONE: i64 = 0x3F800000 // 1.0 |
| 41 | const NX_F32_SC_PI_2: i64 = 0x3FC90FDB // pi/2 = 1.5707963 |
| 42 | const NX_F32_SC_2_PI: i64 = 0x3F22F983 // 2/pi = 0.6366198 |
| 43 | const NX_F32_SC_INV_6: i64 = 0x3E2AAAAB // 1/6 |
| 44 | const NX_F32_SC_INV_120: i64 = 0x3C088889 // 1/120 |
| 45 | const NX_F32_SC_INV_5040: i64 = 0x39500D01 // 1/5040 |
| 46 | const NX_F32_SC_INV_2: i64 = 0x3F000000 // 1/2 |
| 47 | const NX_F32_SC_INV_24: i64 = 0x3D2AAAAB // 1/24 |
| 48 | const NX_F32_SC_INV_720: i64 = 0x3AB60B61 // 1/720 |
functions
| 53 | func _sc_f32_to_i32_rne(value: i64) -> i64 |
| 91 | func _sc_sin_poly(r: i64) -> i64 |
| 114 | func _sc_cos_poly(r: i64) -> i64 |
| 132 | func _sc_range_reduce(x: i64, out_r: *i64) -> i64 |
| 147 | func _sc_range_reduce_pk(x: i64) -> i64 |
| 159 | func nx_f32_sin(x: i64) -> i64 |
| 177 | func nx_f32_cos(x: i64) -> i64 |