xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/arm/fp_mode.c (revision 85868e8a1daeaae7a0e48effb2ea2310ae3b02c6)
1 //===----- lib/arm/fp_mode.c - Floaing-point mode utilities -------*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <stdint.h>
10 
11 #include "../fp_mode.h"
12 
13 #define ARM_TONEAREST  0x0
14 #define ARM_UPWARD     0x1
15 #define ARM_DOWNWARD   0x2
16 #define ARM_TOWARDZERO 0x3
17 #define ARM_RMODE_MASK (ARM_TONEAREST | ARM_UPWARD | \
18                         ARM_DOWNWARD | ARM_TOWARDZERO)
19 #define ARM_RMODE_SHIFT 22
20 
21 #define ARM_INEXACT     0x1000
22 
23 #ifndef __ARM_FP
24 // For soft float targets, allow changing rounding mode by overriding the weak
25 // __arm_fe_default_rmode symbol.
26 FE_ROUND_MODE __attribute__((weak)) __arm_fe_default_rmode = FE_TONEAREST;
27 #endif
28 
29 FE_ROUND_MODE __fe_getround() {
30 #ifdef __ARM_FP
31   uint32_t fpscr;
32   __asm__ __volatile__("vmrs  %0, fpscr" : "=r" (fpscr));
33   fpscr = fpscr >> ARM_RMODE_SHIFT & ARM_RMODE_MASK;
34   switch (fpscr) {
35     case ARM_UPWARD:
36       return FE_UPWARD;
37     case ARM_DOWNWARD:
38       return FE_DOWNWARD;
39     case ARM_TOWARDZERO:
40       return FE_TOWARDZERO;
41     case ARM_TONEAREST:
42     default:
43       return FE_TONEAREST;
44   }
45 #else
46   return __arm_fe_default_rmode;
47 #endif
48 }
49 
50 int __fe_raise_inexact() {
51 #ifdef __ARM_FP
52   uint32_t fpscr;
53   __asm__ __volatile__("vmrs  %0, fpscr" : "=r" (fpscr));
54   __asm__ __volatile__("vmsr  fpscr, %0" : : "ri" (fpscr | ARM_INEXACT));
55   return 0;
56 #else
57   return 0;
58 #endif
59 }
60