1 | /* Set FP exception mask and rounding mode. |
2 | Copyright (C) 1996-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library. If not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <fpu_control.h> |
20 | #include <fenv_libc.h> |
21 | |
22 | |
23 | #define convert_bit(M, F, T) \ |
24 | ((T) < (F) \ |
25 | ? ((M) / ((F) / (T))) & (T) \ |
26 | : ((M) & (F)) * ((T) / (F))) |
27 | |
28 | |
29 | void |
30 | __setfpucw (fpu_control_t fpu_control) |
31 | { |
32 | unsigned long fpcr, swcr, fc = (int)fpu_control; |
33 | |
34 | /* ??? If this was a real external interface we'd want to read the current |
35 | exception state with __ieee_get_fp_control. But this is an internal |
36 | function only called at process startup, so there's no point in trying |
37 | to preserve exceptions that cannot have been raised yet. Indeed, this |
38 | entire function is likely to be one big nop unless the user overrides |
39 | the default __fpu_control variable. */ |
40 | |
41 | /* Convert the rounding mode from fpu_control.h format. */ |
42 | const unsigned long conv_rnd |
43 | = ( (FE_TOWARDZERO << (_FPU_RC_ZERO >> 8)) |
44 | | (FE_DOWNWARD << (_FPU_RC_DOWN >> 8)) |
45 | | (FE_TONEAREST << (_FPU_RC_NEAREST >> 8)) |
46 | | (FE_UPWARD << (_FPU_RC_UP >> 8))); |
47 | |
48 | fpcr = ((conv_rnd >> ((fc >> 8) & 3)) & 3) << FPCR_ROUND_SHIFT; |
49 | |
50 | /* Convert the exception mask from fpu_control.h format. */ |
51 | swcr = convert_bit (~fc, _FPU_MASK_IM, FE_INVALID >> SWCR_ENABLE_SHIFT); |
52 | swcr |= convert_bit (~fc, _FPU_MASK_DM, FE_UNDERFLOW >> SWCR_ENABLE_SHIFT); |
53 | swcr |= convert_bit (~fc, _FPU_MASK_ZM, FE_DIVBYZERO >> SWCR_ENABLE_SHIFT); |
54 | swcr |= convert_bit (~fc, _FPU_MASK_OM, FE_OVERFLOW >> SWCR_ENABLE_SHIFT); |
55 | swcr |= convert_bit (~fc, _FPU_MASK_PM, FE_INEXACT >> SWCR_ENABLE_SHIFT); |
56 | |
57 | /* Install everything. */ |
58 | __fpu_control = fc; |
59 | asm volatile ("mt_fpcr %0" : : "f" (fpcr)); |
60 | __ieee_set_fp_control(swcr); |
61 | } |
62 | |