xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/aarch64/fp_mode.c (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
168d75effSDimitry Andric //===----- lib/aarch64/fp_mode.c - Floaing-point mode utilities ---*- C -*-===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric 
968d75effSDimitry Andric #include <stdint.h>
1068d75effSDimitry Andric 
1168d75effSDimitry Andric #include "../fp_mode.h"
1268d75effSDimitry Andric 
1368d75effSDimitry Andric #define AARCH64_TONEAREST  0x0
1468d75effSDimitry Andric #define AARCH64_UPWARD     0x1
1568d75effSDimitry Andric #define AARCH64_DOWNWARD   0x2
1668d75effSDimitry Andric #define AARCH64_TOWARDZERO 0x3
1768d75effSDimitry Andric #define AARCH64_RMODE_MASK (AARCH64_TONEAREST | AARCH64_UPWARD | \
1868d75effSDimitry Andric                             AARCH64_DOWNWARD | AARCH64_TOWARDZERO)
1968d75effSDimitry Andric #define AARCH64_RMODE_SHIFT 22
2068d75effSDimitry Andric 
2168d75effSDimitry Andric #define AARCH64_INEXACT     0x10
2268d75effSDimitry Andric 
2368d75effSDimitry Andric #ifndef __ARM_FP
2468d75effSDimitry Andric // For soft float targets, allow changing rounding mode by overriding the weak
2568d75effSDimitry Andric // __aarch64_fe_default_rmode symbol.
26fe6060f1SDimitry Andric CRT_FE_ROUND_MODE __attribute__((weak)) __aarch64_fe_default_rmode =
27fe6060f1SDimitry Andric     CRT_FE_TONEAREST;
2868d75effSDimitry Andric #endif
2968d75effSDimitry Andric 
__fe_getround(void)30*81ad6265SDimitry Andric CRT_FE_ROUND_MODE __fe_getround(void) {
3168d75effSDimitry Andric #ifdef __ARM_FP
3268d75effSDimitry Andric   uint64_t fpcr;
3368d75effSDimitry Andric   __asm__ __volatile__("mrs  %0, fpcr" : "=r" (fpcr));
3468d75effSDimitry Andric   fpcr = fpcr >> AARCH64_RMODE_SHIFT & AARCH64_RMODE_MASK;
3568d75effSDimitry Andric   switch (fpcr) {
3668d75effSDimitry Andric     case AARCH64_UPWARD:
37fe6060f1SDimitry Andric       return CRT_FE_UPWARD;
3868d75effSDimitry Andric     case AARCH64_DOWNWARD:
39fe6060f1SDimitry Andric       return CRT_FE_DOWNWARD;
4068d75effSDimitry Andric     case AARCH64_TOWARDZERO:
41fe6060f1SDimitry Andric       return CRT_FE_TOWARDZERO;
4268d75effSDimitry Andric     case AARCH64_TONEAREST:
4368d75effSDimitry Andric     default:
44fe6060f1SDimitry Andric       return CRT_FE_TONEAREST;
4568d75effSDimitry Andric   }
4668d75effSDimitry Andric #else
4768d75effSDimitry Andric   return __aarch64_fe_default_rmode;
4868d75effSDimitry Andric #endif
4968d75effSDimitry Andric }
5068d75effSDimitry Andric 
__fe_raise_inexact(void)51*81ad6265SDimitry Andric int __fe_raise_inexact(void) {
5268d75effSDimitry Andric #ifdef __ARM_FP
5368d75effSDimitry Andric   uint64_t fpsr;
5468d75effSDimitry Andric   __asm__ __volatile__("mrs  %0, fpsr" : "=r" (fpsr));
5568d75effSDimitry Andric   __asm__ __volatile__("msr  fpsr, %0" : : "ri" (fpsr | AARCH64_INEXACT));
5668d75effSDimitry Andric   return 0;
5768d75effSDimitry Andric #else
5868d75effSDimitry Andric   return 0;
5968d75effSDimitry Andric #endif
6068d75effSDimitry Andric }
61