keel-libc 0.1.7

A Rust no_std musl-compatible libc implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright (c) 2026 Meng Zhuo
// SPDX-License-Identifier: BSD-3-Clause

#[cfg(target_arch = "x86_64")]
pub type fexcept_t = u16;

#[cfg(target_arch = "aarch64")]
pub type fexcept_t = u32;

#[cfg(target_arch = "riscv64")]
pub type fexcept_t = u32;

#[cfg(target_arch = "powerpc64")]
pub type fexcept_t = u32;

#[cfg(target_arch = "loongarch64")]
pub type fexcept_t = u32;

// x87 fnstenv/fldenv (28 bytes) + stmxcsr/ldmxcsr (4 bytes) = 32 bytes
#[cfg(target_arch = "x86_64")]
#[repr(C)]
pub struct fenv_t {
    __x87_env: [u8; 28],
    __mxcsr: u32,
}

#[cfg(target_arch = "aarch64")]
#[repr(C)]
pub struct fenv_t {
    __fpcr: u32,
    __fpsr: u32,
}

#[cfg(target_arch = "riscv64")]
pub type fenv_t = u32;

#[cfg(target_arch = "powerpc64")]
pub type fenv_t = f64;

#[cfg(target_arch = "loongarch64")]
#[repr(C)]
pub struct fenv_t {
    __cw: u32,
}

#[cfg(target_arch = "x86_64")]
const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;
#[cfg(target_arch = "aarch64")]
const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;
#[cfg(target_arch = "riscv64")]
const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;

#[cfg(any(target_arch = "powerpc64", target_arch = "loongarch64"))]
const FE_DFL_ENV: *const fenv_t = -1isize as *const fenv_t;

#[cfg(target_arch = "x86_64")]
mod feconst {
    use super::*;
    pub const FE_INVALID: c_int = 1;
    pub const FE_DIVBYZERO: c_int = 4;
    pub const FE_OVERFLOW: c_int = 8;
    pub const FE_UNDERFLOW: c_int = 16;
    pub const FE_INEXACT: c_int = 32;
    pub const FE_ALL_EXCEPT: c_int = 63;

    pub const FE_TONEAREST: c_int = 0;
    pub const FE_DOWNWARD: c_int = 0x400;
    pub const FE_UPWARD: c_int = 0x800;
    pub const FE_TOWARDZERO: c_int = 0xc00;
}

#[cfg(target_arch = "aarch64")]
mod feconst {
    use super::*;
    pub const FE_INVALID: c_int = 1;
    pub const FE_DIVBYZERO: c_int = 2;
    pub const FE_OVERFLOW: c_int = 4;
    pub const FE_UNDERFLOW: c_int = 8;
    pub const FE_INEXACT: c_int = 16;
    pub const FE_ALL_EXCEPT: c_int = 31;

    pub const FE_TONEAREST: c_int = 0;
    pub const FE_DOWNWARD: c_int = 0x800000;
    pub const FE_UPWARD: c_int = 0x400000;
    pub const FE_TOWARDZERO: c_int = 0xc00000;
}

#[cfg(target_arch = "riscv64")]
mod feconst {
    use super::*;
    pub const FE_INVALID: c_int = 16;
    pub const FE_DIVBYZERO: c_int = 8;
    pub const FE_OVERFLOW: c_int = 4;
    pub const FE_UNDERFLOW: c_int = 2;
    pub const FE_INEXACT: c_int = 1;
    pub const FE_ALL_EXCEPT: c_int = 31;

    pub const FE_TONEAREST: c_int = 0;
    pub const FE_DOWNWARD: c_int = 2;
    pub const FE_UPWARD: c_int = 3;
    pub const FE_TOWARDZERO: c_int = 1;
}

#[cfg(target_arch = "powerpc64")]
mod feconst {
    use super::*;
    pub const FE_INEXACT: c_int = 0x0200_0000;
    pub const FE_DIVBYZERO: c_int = 0x0400_0000;
    pub const FE_UNDERFLOW: c_int = 0x0800_0000;
    pub const FE_OVERFLOW: c_int = 0x1000_0000;
    pub const FE_INVALID: c_int = 0x2000_0000;
    pub const FE_ALL_EXCEPT: c_int = 0x3e00_0000;
    pub const FE_TONEAREST: c_int = 0;
    pub const FE_TOWARDZERO: c_int = 1;
    pub const FE_UPWARD: c_int = 2;
    pub const FE_DOWNWARD: c_int = 3;

    pub const FE_INVALID_SOFTWARE: c_int = 0x0000_0400;
    pub const FE_ALL_INVALID: c_int = 0x01f8_0700;
}

#[cfg(target_arch = "loongarch64")]
mod feconst {
    use super::*;
    pub const FE_INEXACT: c_int = 0x010000;
    pub const FE_UNDERFLOW: c_int = 0x020000;
    pub const FE_OVERFLOW: c_int = 0x040000;
    pub const FE_DIVBYZERO: c_int = 0x080000;
    pub const FE_INVALID: c_int = 0x100000;
    pub const FE_ALL_EXCEPT: c_int = 0x1f0000;
    pub const FE_TONEAREST: c_int = 0x000;
    pub const FE_TOWARDZERO: c_int = 0x100;
    pub const FE_UPWARD: c_int = 0x200;
    pub const FE_DOWNWARD: c_int = 0x300;
}

use feconst::*;

// x86_64 implementation using x87/MXCSR
#[cfg(target_arch = "x86_64")]
mod x86_64_imp {
    use super::*;

    // CW=0x037f (all masked, nearest, extended), tags=0xffff (all empty)
    const DEFAULT_X87_ENV: [u8; 28] = {
        let mut env = [0u8; 28];
        env[0] = 0x7f;
        env[1] = 0x03;
        env[8] = 0xff;
        env[9] = 0xff;
        env
    };
    const DEFAULT_MXCSR: u32 = 0x1f80;

    // musl semantics: clears x87, merges old x87 into MXCSR then clears MXCSR
    #[no_mangle]
    pub unsafe extern "C" fn feclearexcept(excepts: c_int) -> c_int {
        let mask = excepts & 0x3f;
        let sw: u16;
        core::arch::asm!("fnstsw ax", out("ax") sw);
        if (sw as c_int) & mask != 0 {
            core::arch::asm!("fnclex");
        }
        let mut mxcsr: u32 = 0;
        let p = &mut mxcsr as *mut u32;
        core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
        let mxcsr_val = core::ptr::read(p) | ((sw as u32) & 0x3f);
        if mxcsr_val & (mask as u32) != 0 {
            core::ptr::write(p, mxcsr_val & !(mask as u32));
            core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p);
        }
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn feraiseexcept(excepts: c_int) -> c_int {
        let mask = (excepts & 0x3f) as u32;
        let mut mxcsr: u32 = 0;
        let p = &mut mxcsr as *mut u32;
        core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
        core::ptr::write(p, core::ptr::read(p) | mask);
        core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fetestexcept(excepts: c_int) -> c_int {
        let mask = excepts & 0x3f;
        let mut mxcsr: u32 = 0;
        let p = &mut mxcsr as *mut u32;
        core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
        let sw: u16;
        core::arch::asm!("fnstsw ax", out("ax") sw);
        ((sw as c_int) | (core::ptr::read(p) as c_int)) & mask
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetround() -> c_int {
        let mut mxcsr: u32 = 0;
        let p = &mut mxcsr as *mut u32;
        core::arch::asm!("stmxcsr [{p}]", p = in(reg) p);
        // MXCSR bits 13-14 map to x87 CW bits 10-11
        ((core::ptr::read(p) >> 3) & 0xc00) as c_int
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
        if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
            return -1;
        }
        let mut cw: u16 = 0;
        let p_cw = &mut cw as *mut u16;
        core::arch::asm!("fnstcw [{p}]", p = in(reg) p_cw);
        core::ptr::write(p_cw, (core::ptr::read(p_cw) & 0xf3ff) | (r as u16 & 0x0c00));
        core::arch::asm!("fldcw [{p}]", p = in(reg) p_cw);

        let mut mxcsr: u32 = 0;
        let p_mx = &mut mxcsr as *mut u32;
        core::arch::asm!("stmxcsr [{p}]", p = in(reg) p_mx);
        core::ptr::write(p_mx, (core::ptr::read(p_mx) & 0xffff9fff) | ((r as u32 & 0xc00) << 3));
        core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p_mx);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
        core::arch::asm!("fnstenv [{p}]", p = in(reg) envp);
        let mxcsr_p = (envp as *mut u8).add(28) as *mut u32;
        core::arch::asm!("stmxcsr [{p}]", p = in(reg) mxcsr_p);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
        if envp == FE_DFL_ENV {
            core::arch::asm!("fldenv [{p}]", p = in(reg) DEFAULT_X87_ENV.as_ptr());
            let p = &DEFAULT_MXCSR as *const u32;
            core::arch::asm!("ldmxcsr [{p}]", p = in(reg) p);
        } else {
            core::arch::asm!("fldenv [{p}]", p = in(reg) envp);
            let mxcsr_p = (envp as *const u8).add(28) as *const u32;
            core::arch::asm!("ldmxcsr [{p}]", p = in(reg) mxcsr_p);
        }
        0
    }
}

#[cfg(target_arch = "x86_64")]
use x86_64_imp::*;

// aarch64 implementation using FPCR/FPSR
#[cfg(target_arch = "aarch64")]
mod aarch64_imp {
    use super::*;

    #[no_mangle]
    pub unsafe extern "C" fn feclearexcept(excepts: c_int) -> c_int {
        let mask = (excepts & 0x1f) as u64;
        let mut fpsr: u64;
        core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
        let new_fpsr = fpsr & !mask;
        core::arch::asm!("msr fpsr, {fpsr}", fpsr = in(reg) new_fpsr);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn feraiseexcept(excepts: c_int) -> c_int {
        let mask = (excepts & 0x1f) as u64;
        let mut fpsr: u64;
        core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
        let new_fpsr = fpsr | mask;
        core::arch::asm!("msr fpsr, {fpsr}", fpsr = in(reg) new_fpsr);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fetestexcept(excepts: c_int) -> c_int {
        let mask = (excepts & 0x1f) as u64;
        let mut fpsr: u64;
        core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
        (fpsr & mask) as c_int
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetround() -> c_int {
        let mut fpcr: u64;
        core::arch::asm!("mrs {fpcr}, fpcr", fpcr = out(reg) fpcr);
        (fpcr as c_int) & 0xc00000
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
        if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
            return -1;
        }
        let mut fpcr: u64;
        core::arch::asm!("mrs {fpcr}, fpcr", fpcr = out(reg) fpcr);
        let new_fpcr = (fpcr & !0xc00000u64) | ((r as u64) & 0xc00000);
        core::arch::asm!("msr fpcr, {fpcr}", fpcr = in(reg) new_fpcr);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
        let mut fpcr: u64;
        let mut fpsr: u64;
        core::arch::asm!("mrs {fpcr}, fpcr", fpcr = out(reg) fpcr);
        core::arch::asm!("mrs {fpsr}, fpsr", fpsr = out(reg) fpsr);
        (*envp).__fpcr = fpcr as u32;
        (*envp).__fpsr = fpsr as u32;
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
        let (fpcr, fpsr) = if envp == FE_DFL_ENV {
            (0u64, 0u64)
        } else {
            ((*envp).__fpcr as u64, (*envp).__fpsr as u64)
        };
        core::arch::asm!("msr fpcr, {fpcr}", fpcr = in(reg) fpcr);
        core::arch::asm!("msr fpsr, {fpsr}", fpsr = in(reg) fpsr);
        0
    }
}

#[cfg(target_arch = "aarch64")]
use aarch64_imp::*;

// riscv64 implementation using fflags/frm/fcsr CSRs
#[cfg(target_arch = "riscv64")]
mod riscv64_imp {
    use super::*;

    #[no_mangle]
    pub unsafe extern "C" fn feclearexcept(excepts: c_int) -> c_int {
        let mask = (excepts & 0x1f) as u64;
        // csrc fflags, a0 — clear bits in fflags
        core::arch::asm!(
            "csrc fflags, {mask}",
            mask = in(reg) mask,
            options(nostack),
        );
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn feraiseexcept(excepts: c_int) -> c_int {
        let mask = (excepts & 0x1f) as u64;
        // csrs fflags, a0 — set bits in fflags
        core::arch::asm!(
            "csrs fflags, {mask}",
            mask = in(reg) mask,
            options(nostack),
        );
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fetestexcept(excepts: c_int) -> c_int {
        let fflags: u64;
        // frflags t0 — read fflags
        core::arch::asm!(
            "frflags {fflags}",
            fflags = out(reg) fflags,
            options(nostack),
        );
        (fflags as c_int) & (excepts & 0x1f)
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetround() -> c_int {
        let frm: u64;
        // frrm a0 — read rounding mode
        core::arch::asm!(
            "frrm {frm}",
            frm = out(reg) frm,
            options(nostack),
        );
        frm as c_int
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
        if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
            return -1;
        }
        // fsrm t0, a0 — set rounding mode
        core::arch::asm!(
            "fsrm zero, {r}",
            r = in(reg) r as u64,
            options(nostack),
        );
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
        let fcsr: u64;
        // frcsr t0 — read full CSR
        core::arch::asm!(
            "frcsr {fcsr}",
            fcsr = out(reg) fcsr,
            options(nostack),
        );
        *envp = fcsr as u32;
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
        let val = if envp == FE_DFL_ENV {
            0u64
        } else {
            *envp as u64
        };
        // fscsr t1 — set full CSR
        core::arch::asm!(
            "fscsr zero, {val}",
            val = in(reg) val,
            options(nostack),
        );
        0
    }
}

#[cfg(target_arch = "riscv64")]
use riscv64_imp::*;

#[cfg(target_arch = "powerpc64")]
mod powerpc64_imp {
    use super::*;

    #[inline]
    unsafe fn get_fpscr() -> u64 {
        let d: f64;
        core::arch::asm!("mffs {}", out(freg) d, options(nostack));
        d.to_bits()
    }

    #[inline]
    unsafe fn set_fpscr(v: u64) {
        core::arch::asm!(
            "mtfsf 255, {}",
            in(freg) f64::from_bits(v),
            options(nostack),
        );
    }

    #[no_mangle]
    pub unsafe extern "C" fn feclearexcept(e: c_int) -> c_int {
        let mut mask = e & FE_ALL_EXCEPT;
        if mask & FE_INVALID != 0 {
            mask |= FE_ALL_INVALID;
        }
        set_fpscr(get_fpscr() & !(mask as u64));
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn feraiseexcept(e: c_int) -> c_int {
        let mut mask = e & FE_ALL_EXCEPT;
        if mask & FE_INVALID != 0 {
            mask |= FE_INVALID_SOFTWARE;
        }
        set_fpscr(get_fpscr() | mask as u64);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fetestexcept(e: c_int) -> c_int {
        get_fpscr() as c_int & e & FE_ALL_EXCEPT
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetround() -> c_int {
        (get_fpscr() & 3) as c_int
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
        if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
            return -1;
        }
        set_fpscr((get_fpscr() & !3) | (r as u64 & 3));
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
        *envp = f64::from_bits(get_fpscr());
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
        let v = if envp == FE_DFL_ENV { 0.0 } else { *envp };
        set_fpscr(v.to_bits());
        0
    }
}

#[cfg(target_arch = "powerpc64")]
use powerpc64_imp::*;

#[cfg(target_arch = "loongarch64")]
mod loongarch64_imp {
    use super::*;

    #[inline]
    unsafe fn get_fcsr() -> u64 {
        let v: u64;
        core::arch::asm!("movfcsr2gr {v}, $fcsr0", v = out(reg) v, options(nostack));
        v
    }

    #[inline]
    unsafe fn set_fcsr(v: u64) {
        core::arch::asm!("movgr2fcsr $fcsr0, {v}", v = in(reg) v, options(nostack));
    }

    #[no_mangle]
    pub unsafe extern "C" fn feclearexcept(e: c_int) -> c_int {
        let mask = (e & FE_ALL_EXCEPT) as u64;
        set_fcsr(get_fcsr() & !mask);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn feraiseexcept(e: c_int) -> c_int {
        set_fcsr(get_fcsr() | (e & FE_ALL_EXCEPT) as u64);
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fetestexcept(e: c_int) -> c_int {
        get_fcsr() as c_int & e & FE_ALL_EXCEPT
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetround() -> c_int {
        (get_fcsr() & 0x300) as c_int
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetround(r: c_int) -> c_int {
        if r != FE_TONEAREST && r != FE_DOWNWARD && r != FE_UPWARD && r != FE_TOWARDZERO {
            return -1;
        }
        set_fcsr((get_fcsr() & !0x300) | (r as u64 & 0x300));
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fegetenv(envp: *mut fenv_t) -> c_int {
        (*envp).__cw = get_fcsr() as u32;
        0
    }

    #[no_mangle]
    pub unsafe extern "C" fn fesetenv(envp: *const fenv_t) -> c_int {
        let v = if envp == FE_DFL_ENV { 0 } else { (*envp).__cw as u64 };
        set_fcsr(v);
        0
    }
}

#[cfg(target_arch = "loongarch64")]
use loongarch64_imp::*;

#[no_mangle]
pub unsafe extern "C" fn fegetexceptflag(fp: *mut fexcept_t, mask: c_int) -> c_int {
    *fp = fetestexcept(mask) as fexcept_t;
    0
}

#[no_mangle]
pub unsafe extern "C" fn fesetexceptflag(fp: *const fexcept_t, mask: c_int) -> c_int {
    let flags = *fp as c_int;
    feclearexcept((!flags) & mask);
    feraiseexcept(flags & mask);
    0
}

#[no_mangle]
pub unsafe extern "C" fn feholdexcept(envp: *mut fenv_t) -> c_int {
    fegetenv(envp);
    feclearexcept(FE_ALL_EXCEPT);
    0
}

#[no_mangle]
pub unsafe extern "C" fn feupdateenv(envp: *const fenv_t) -> c_int {
    let ex = fetestexcept(FE_ALL_EXCEPT);
    fesetenv(envp);
    feraiseexcept(ex);
    0
}

// C99 FLT_ROUNDS: 0=towardzero, 1=nearest, 2=upward, 3=downward
#[no_mangle]
pub extern "C" fn __flt_rounds() -> c_int {
    match unsafe { fegetround() } {
        FE_TOWARDZERO => 0,
        FE_TONEAREST => 1,
        FE_UPWARD => 2,
        FE_DOWNWARD => 3,
        _ => -1,
    }
}