1#[cfg(target_arch = "x86_64")]
5pub type fexcept_t = u16;
6
7#[cfg(target_arch = "aarch64")]
8pub type fexcept_t = u32;
9
10#[cfg(target_arch = "riscv64")]
11pub type fexcept_t = u32;
12
13#[cfg(target_arch = "powerpc64")]
14pub type fexcept_t = u32;
15
16#[cfg(target_arch = "loongarch64")]
17pub type fexcept_t = u32;
18
19#[cfg(target_arch = "x86_64")]
21#[repr(C)]
22pub struct fenv_t {
23 __x87_env: [u8; 28],
24 __mxcsr: u32,
25}
26
27#[cfg(target_arch = "aarch64")]
28#[repr(C)]
29pub struct fenv_t {
30 __fpcr: u32,
31 __fpsr: u32,
32}
33
34#[cfg(target_arch = "riscv64")]
35pub type fenv_t = u32;
36
37#[cfg(target_arch = "powerpc64")]
38pub type fenv_t = f64;
39
40#[cfg(target_arch = "loongarch64")]
41#[repr(C)]
42pub struct fenv_t {
43 __cw: u32,
44}
45
46#[cfg(target_arch = "x86_64")]
47const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;
48#[cfg(target_arch = "aarch64")]
49const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;
50#[cfg(target_arch = "riscv64")]
51const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;
52
53#[cfg(any(target_arch = "powerpc64", target_arch = "loongarch64"))]
54const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;
55
56#[cfg(target_arch = "x86_64")]
57mod feconst {
58 use super::*;
59 pub const FE_INVALID: c_int = 1;
60 pub const FE_DIVBYZERO: c_int = 4;
61 pub const FE_OVERFLOW: c_int = 8;
62 pub const FE_UNDERFLOW: c_int = 16;
63 pub const FE_INEXACT: c_int = 32;
64 pub const FE_ALL_EXCEPT: c_int = 63;
65
66 pub const FE_TONEAREST: c_int = 0;
67 pub const FE_DOWNWARD: c_int = 0x400;
68 pub const FE_UPWARD: c_int = 0x800;
69 pub const FE_TOWARDZERO: c_int = 0xc00;
70}
71
72#[cfg(target_arch = "aarch64")]
73mod feconst {
74 use super::*;
75 pub const FE_INVALID: c_int = 1;
76 pub const FE_DIVBYZERO: c_int = 2;
77 pub const FE_OVERFLOW: c_int = 4;
78 pub const FE_UNDERFLOW: c_int = 8;
79 pub const FE_INEXACT: c_int = 16;
80 pub const FE_ALL_EXCEPT: c_int = 31;
81
82 pub const FE_TONEAREST: c_int = 0;
83 pub const FE_DOWNWARD: c_int = 0x800000;
84 pub const FE_UPWARD: c_int = 0x400000;
85 pub const FE_TOWARDZERO: c_int = 0xc00000;
86}
87
88#[cfg(target_arch = "riscv64")]
89mod feconst {
90 use super::*;
91 pub const FE_INVALID: c_int = 16;
92 pub const FE_DIVBYZERO: c_int = 8;
93 pub const FE_OVERFLOW: c_int = 4;
94 pub const FE_UNDERFLOW: c_int = 2;
95 pub const FE_INEXACT: c_int = 1;
96 pub const FE_ALL_EXCEPT: c_int = 31;
97
98 pub const FE_TONEAREST: c_int = 0;
99 pub const FE_DOWNWARD: c_int = 2;
100 pub const FE_UPWARD: c_int = 3;
101 pub const FE_TOWARDZERO: c_int = 1;
102}
103
104#[cfg(target_arch = "powerpc64")]
105mod feconst {
106 use super::*;
107 pub const FE_INEXACT: c_int = 0x0200_0000;
108 pub const FE_DIVBYZERO: c_int = 0x0400_0000;
109 pub const FE_UNDERFLOW: c_int = 0x0800_0000;
110 pub const FE_OVERFLOW: c_int = 0x1000_0000;
111 pub const FE_INVALID: c_int = 0x2000_0000;
112 pub const FE_ALL_EXCEPT: c_int = 0x3e00_0000;
113 pub const FE_TONEAREST: c_int = 0;
114 pub const FE_TOWARDZERO: c_int = 1;
115 pub const FE_UPWARD: c_int = 2;
116 pub const FE_DOWNWARD: c_int = 3;
117
118 pub const FE_INVALID_SOFTWARE: c_int = 0x0000_0400;
119 pub const FE_ALL_INVALID: c_int = 0x01f8_0700;
120}
121
122#[cfg(target_arch = "loongarch64")]
123mod feconst {
124 use super::*;
125 pub const FE_INEXACT: c_int = 0x010000;
126 pub const FE_UNDERFLOW: c_int = 0x020000;
127 pub const FE_OVERFLOW: c_int = 0x040000;
128 pub const FE_DIVBYZERO: c_int = 0x080000;
129 pub const FE_INVALID: c_int = 0x100000;
130 pub const FE_ALL_EXCEPT: c_int = 0x1f0000;
131 pub const FE_TONEAREST: c_int = 0x000;
132 pub const FE_TOWARDZERO: c_int = 0x100;
133 pub const FE_UPWARD: c_int = 0x200;
134 pub const FE_DOWNWARD: c_int = 0x300;
135}
136
137use feconst::*;
138
139#[cfg(target_arch = "x86_64")]
141mod x86_64_imp {
142 use super::*;
143
144 const DEFAULT_X87_ENV: [u8; 28] = {
146 let mut env = [0u8; 28];
147 env[0] = 0x7f;
148 env[1] = 0x03;
149 env[8] = 0xff;
150 env[9] = 0xff;
151 env
152 };
153 const DEFAULT_MXCSR: u32 = 0x1f80;
154
155 #[no_mangle]
157 pub unsafe extern "C" fn feclearexcept(excepts: c_int) -> c_int {
158 let mask = excepts & 0x3f;
159 let sw: u16;
160 core::arch::asm!("fnstsw ax", out("ax") sw);
161 if (sw as c_int) & mask != 0 {
162 core::arch::asm!("fnclex");
163 }
164 let mut mxcsr: u32 = 0;
165 let p = &mut mxcsr as *mut u32;
166 core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
167 let mxcsr_val = core::ptr::read(p) | ((sw as u32) & 0x3f);
168 if mxcsr_val & (mask as u32) != 0 {
169 core::ptr::write(p, mxcsr_val & !(mask as u32));
170 core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p);
171 }
172 0
173 }
174
175 #[no_mangle]
176 pub unsafe extern "C" fn feraiseexcept(excepts: c_int) -> c_int {
177 let mask = (excepts & 0x3f) as u32;
178 let mut mxcsr: u32 = 0;
179 let p = &mut mxcsr as *mut u32;
180 core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
181 core::ptr::write(p, core::ptr::read(p) | mask);
182 core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p);
183 0
184 }
185
186 #[no_mangle]
187 pub unsafe extern "C" fn fetestexcept(excepts: c_int) -> c_int {
188 let mask = excepts & 0x3f;
189 let mut mxcsr: u32 = 0;
190 let p = &mut mxcsr as *mut u32;
191 core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
192 let sw: u16;
193 core::arch::asm!("fnstsw ax", out("ax") sw);
194 ((sw as c_int) | (core::ptr::read(p) as c_int)) & mask
195 }
196
197 #[no_mangle]
198 pub unsafe extern "C" fn fegetround() -> c_int {
199 let mut mxcsr: u32 = 0;
200 let p = &mut mxcsr as *mut u32;
201 core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
202 ((core::ptr::read(p) >> 3) & 0xc00) as c_int
204 }
205
206 #[no_mangle]
207 pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
208 if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
209 return -1;
210 }
211 let mut cw: u16 = 0;
212 let p_cw = &mut cw as *mut u16;
213 core::arch::asm!("fnstcw [{p}]", p = in(reg) p_cw);
214 core::ptr::write(p_cw, (core::ptr::read(p_cw) & 0xf3ff) | (r as u16 & 0x0c00));
215 core::arch::asm!("fldcw [{p}]", p = in(reg) p_cw);
216
217 let mut mxcsr: u32 = 0;
218 let p_mx = &mut mxcsr as *mut u32;
219 core::arch::asm!("stmxcsr [{p}]", p = in(reg) p_mx);
220 core::ptr::write(p_mx, (core::ptr::read(p_mx) & 0xffff9fff) | ((r as u32 & 0xc00) << 3));
221 core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p_mx);
222 0
223 }
224
225 #[no_mangle]
226 pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
227 core::arch::asm!("fnstenv [{p}]", p = in(reg) envp);
228 let mxcsr_p = (envp as *mut u8).add(28) as *mut u32;
229 core::arch::asm!("stmxcsr [{p}]", p = in(reg) mxcsr_p);
230 0
231 }
232
233 #[no_mangle]
234 pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
235 if envp == FE_DFL_ENV {
236 core::arch::asm!("fldenv [{p}]", p = in(reg) DEFAULT_X87_ENV.as_ptr());
237 let p = &DEFAULT_MXCSR as *const u32;
238 core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p);
239 } else {
240 core::arch::asm!("fldenv [{p}]", p = in(reg) envp);
241 let mxcsr_p = (envp as *const u8).add(28) as *const u32;
242 core::arch::asm!("ldmxcsr [{p}]", p = in(reg) mxcsr_p);
243 }
244 0
245 }
246}
247
248#[cfg(target_arch = "x86_64")]
249use x86_64_imp::*;
250
251#[cfg(target_arch = "aarch64")]
253mod aarch64_imp {
254 use super::*;
255
256 #[no_mangle]
257 pub unsafe extern "C" fn feclearexcept(excepts: c_int) -> c_int {
258 let mask = (excepts & 0x1f) as u64;
259 let mut fpsr: u64;
260 core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
261 let new_fpsr = fpsr & !mask;
262 core::arch::asm!("msr fpsr, {fpsr}", fpsr = in(reg) new_fpsr);
263 0
264 }
265
266 #[no_mangle]
267 pub unsafe extern "C" fn feraiseexcept(excepts: c_int) -> c_int {
268 let mask = (excepts & 0x1f) as u64;
269 let mut fpsr: u64;
270 core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
271 let new_fpsr = fpsr | mask;
272 core::arch::asm!("msr fpsr, {fpsr}", fpsr = in(reg) new_fpsr);
273 0
274 }
275
276 #[no_mangle]
277 pub unsafe extern "C" fn fetestexcept(excepts: c_int) -> c_int {
278 let mask = (excepts & 0x1f) as u64;
279 let mut fpsr: u64;
280 core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
281 (fpsr & mask) as c_int
282 }
283
284 #[no_mangle]
285 pub unsafe extern "C" fn fegetround() -> c_int {
286 let mut fpcr: u64;
287 core::arch::asm!("mrs {fpcr}, fpcr", fpcr = out(reg) fpcr);
288 (fpcr as c_int) & 0xc00000
289 }
290
291 #[no_mangle]
292 pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
293 if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
294 return -1;
295 }
296 let mut fpcr: u64;
297 core::arch::asm!("mrs {fpcr}, fpcr", fpcr = out(reg) fpcr);
298 let new_fpcr = (fpcr & !0xc00000u64) | ((r as u64) & 0xc00000);
299 core::arch::asm!("msr fpcr, {fpcr}", fpcr = in(reg) new_fpcr);
300 0
301 }
302
303 #[no_mangle]
304 pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
305 let mut fpcr: u64;
306 let mut fpsr: u64;
307 core::arch::asm!("mrs {fpcr}, fpcr", fpcr = out(reg) fpcr);
308 core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
309 (*envp).__fpcr = fpcr as u32;
310 (*envp).__fpsr = fpsr as u32;
311 0
312 }
313
314 #[no_mangle]
315 pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
316 let (fpcr, fpsr) = if envp == FE_DFL_ENV {
317 (0u64, 0u64)
318 } else {
319 ((*envp).__fpcr as u64, (*envp).__fpsr as u64)
320 };
321 core::arch::asm!("msr fpcr, {fpcr}", fpcr = in(reg) fpcr);
322 core::arch::asm!("msr fpsr, {fpsr}", fpsr = in(reg) fpsr);
323 0
324 }
325}
326
327#[cfg(target_arch = "aarch64")]
328use aarch64_imp::*;
329
330#[cfg(target_arch = "riscv64")]
332mod riscv64_imp {
333 use super::*;
334
335 #[no_mangle]
336 pub unsafe extern "C" fn feclearexcept(excepts: c_int) -> c_int {
337 let mask = (excepts & 0x1f) as u64;
338 core::arch::asm!(
340 "csrc fflags, {mask}",
341 mask = in(reg) mask,
342 options(nostack),
343 );
344 0
345 }
346
347 #[no_mangle]
348 pub unsafe extern "C" fn feraiseexcept(excepts: c_int) -> c_int {
349 let mask = (excepts & 0x1f) as u64;
350 core::arch::asm!(
352 "csrs fflags, {mask}",
353 mask = in(reg) mask,
354 options(nostack),
355 );
356 0
357 }
358
359 #[no_mangle]
360 pub unsafe extern "C" fn fetestexcept(excepts: c_int) -> c_int {
361 let fflags: u64;
362 core::arch::asm!(
364 "frflags {fflags}",
365 fflags = out(reg) fflags,
366 options(nostack),
367 );
368 (fflags as c_int) & (excepts & 0x1f)
369 }
370
371 #[no_mangle]
372 pub unsafe extern "C" fn fegetround() -> c_int {
373 let frm: u64;
374 core::arch::asm!(
376 "frrm {frm}",
377 frm = out(reg) frm,
378 options(nostack),
379 );
380 frm as c_int
381 }
382
383 #[no_mangle]
384 pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
385 if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
386 return -1;
387 }
388 core::arch::asm!(
390 "fsrm zero, {r}",
391 r = in(reg) r as u64,
392 options(nostack),
393 );
394 0
395 }
396
397 #[no_mangle]
398 pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
399 let fcsr: u64;
400 core::arch::asm!(
402 "frcsr {fcsr}",
403 fcsr = out(reg) fcsr,
404 options(nostack),
405 );
406 *envp = fcsr as u32;
407 0
408 }
409
410 #[no_mangle]
411 pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
412 let val = if envp == FE_DFL_ENV {
413 0u64
414 } else {
415 *envp as u64
416 };
417 core::arch::asm!(
419 "fscsr zero, {val}",
420 val = in(reg) val,
421 options(nostack),
422 );
423 0
424 }
425}
426
427#[cfg(target_arch = "riscv64")]
428use riscv64_imp::*;
429
430#[cfg(target_arch = "powerpc64")]
431mod powerpc64_imp {
432 use super::*;
433
434 #[inline]
435 unsafe fn get_fpscr() -> u64 {
436 let d: f64;
437 core::arch::asm!("mffs {}", out(freg) d, options(nostack));
438 d.to_bits()
439 }
440
441 #[inline]
442 unsafe fn set_fpscr(v: u64) {
443 core::arch::asm!(
444 "mtfsf 255, {}",
445 in(freg) f64::from_bits(v),
446 options(nostack),
447 );
448 }
449
450 #[no_mangle]
451 pub unsafe extern "C" fn feclearexcept(e: c_int) -> c_int {
452 let mut mask = e & FE_ALL_EXCEPT;
453 if mask & FE_INVALID != 0 {
454 mask |= FE_ALL_INVALID;
455 }
456 set_fpscr(get_fpscr() & !(mask as u64));
457 0
458 }
459
460 #[no_mangle]
461 pub unsafe extern "C" fn feraiseexcept(e: c_int) -> c_int {
462 let mut mask = e & FE_ALL_EXCEPT;
463 if mask & FE_INVALID != 0 {
464 mask |= FE_INVALID_SOFTWARE;
465 }
466 set_fpscr(get_fpscr() | mask as u64);
467 0
468 }
469
470 #[no_mangle]
471 pub unsafe extern "C" fn fetestexcept(e: c_int) -> c_int {
472 get_fpscr() as c_int & e & FE_ALL_EXCEPT
473 }
474
475 #[no_mangle]
476 pub unsafe extern "C" fn fegetround() -> c_int {
477 (get_fpscr() & 3) as c_int
478 }
479
480 #[no_mangle]
481 pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
482 if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
483 return -1;
484 }
485 set_fpscr((get_fpscr() & !3) | (r as u64 & 3));
486 0
487 }
488
489 #[no_mangle]
490 pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
491 *envp = f64::from_bits(get_fpscr());
492 0
493 }
494
495 #[no_mangle]
496 pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
497 let v = if envp == FE_DFL_ENV { 0.0 } else { *envp };
498 set_fpscr(v.to_bits());
499 0
500 }
501}
502
503#[cfg(target_arch = "powerpc64")]
504use powerpc64_imp::*;
505
506#[cfg(target_arch = "loongarch64")]
507mod loongarch64_imp {
508 use super::*;
509
510 #[inline]
511 unsafe fn get_fcsr() -> u64 {
512 let v: u64;
513 core::arch::asm!("movfcsr2gr {v}, $fcsr0", v = out(reg) v, options(nostack));
514 v
515 }
516
517 #[inline]
518 unsafe fn set_fcsr(v: u64) {
519 core::arch::asm!("movgr2fcsr $fcsr0, {v}", v = in(reg) v, options(nostack));
520 }
521
522 #[no_mangle]
523 pub unsafe extern "C" fn feclearexcept(e: c_int) -> c_int {
524 let mask = (e & FE_ALL_EXCEPT) as u64;
525 set_fcsr(get_fcsr() & !mask);
526 0
527 }
528
529 #[no_mangle]
530 pub unsafe extern "C" fn feraiseexcept(e: c_int) -> c_int {
531 set_fcsr(get_fcsr() | (e & FE_ALL_EXCEPT) as u64);
532 0
533 }
534
535 #[no_mangle]
536 pub unsafe extern "C" fn fetestexcept(e: c_int) -> c_int {
537 get_fcsr() as c_int & e & FE_ALL_EXCEPT
538 }
539
540 #[no_mangle]
541 pub unsafe extern "C" fn fegetround() -> c_int {
542 (get_fcsr() & 0x300) as c_int
543 }
544
545 #[no_mangle]
546 pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
547 if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
548 return -1;
549 }
550 set_fcsr((get_fcsr() & !0x300) | (r as u64 & 0x300));
551 0
552 }
553
554 #[no_mangle]
555 pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
556 (*envp).__cw = get_fcsr() as u32;
557 0
558 }
559
560 #[no_mangle]
561 pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
562 let v = if envp == FE_DFL_ENV { 0 } else { (*envp).__cw as u64 };
563 set_fcsr(v);
564 0
565 }
566}
567
568#[cfg(target_arch = "loongarch64")]
569use loongarch64_imp::*;
570
571#[no_mangle]
572pub unsafe extern "C" fn fegetexceptflag(fp: *mut fexcept_t, mask: c_int) -> c_int {
573 *fp = fetestexcept(mask) as fexcept_t;
574 0
575}
576
577#[no_mangle]
578pub unsafe extern "C" fn fesetexceptflag(fp: *const fexcept_t, mask: c_int) -> c_int {
579 let flags = *fp as c_int;
580 feclearexcept((!flags) & mask);
581 feraiseexcept(flags & mask);
582 0
583}
584
585#[no_mangle]
586pub unsafe extern "C" fn feholdexcept(envp: *mut fenv_t) -> c_int {
587 fegetenv(envp);
588 feclearexcept(FE_ALL_EXCEPT);
589 0
590}
591
592#[no_mangle]
593pub unsafe extern "C" fn feupdateenv(envp: *const fenv_t) -> c_int {
594 let ex = fetestexcept(FE_ALL_EXCEPT);
595 fesetenv(envp);
596 feraiseexcept(ex);
597 0
598}
599
600#[no_mangle]
602pub extern "C" fn __flt_rounds() -> c_int {
603 match unsafe { fegetround() } {
604 FE_TOWARDZERO => 0,
605 FE_TONEAREST => 1,
606 FE_UPWARD => 2,
607 FE_DOWNWARD => 3,
608 _ => -1,
609 }
610}