blob: 0f28d0c8db85d994cc67a0ae4ed7850df255481c [file] [log] [blame]
Serban Constantinescu1c4f1012013-10-11 10:44:43 +01001/*-
2 * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: libm/aarch64/fenv.c $
27 */
28
Christopher Ferrisfa775292015-10-23 15:42:27 -070029#include <stdint.h>
Serban Constantinescu1c4f1012013-10-11 10:44:43 +010030#include <fenv.h>
31
Serban Constantinescua147a1d2014-06-08 16:55:22 +010032#define FPCR_RMODE_SHIFT 22
33
34const fenv_t __fe_dfl_env = { 0 /* control */, 0 /* status */};
35
36typedef __uint32_t fpu_control_t; // FPCR, Floating-point Control Register.
37typedef __uint32_t fpu_status_t; // FPSR, Floating-point Status Register.
38
Christopher Ferrisfa775292015-10-23 15:42:27 -070039#define __get(REGISTER, __value) { \
40 uint64_t __value64; \
41 __asm__ __volatile__("mrs %0," REGISTER : "=r" (__value64)); \
42 __value = (__uint32_t) __value64; \
43}
44#define __get_fpcr(__fpcr) __get("fpcr", __fpcr)
45#define __get_fpsr(__fpsr) __get("fpsr", __fpsr)
46
47#define __set(REGISTER, __value) { \
48 uint64_t __value64 = __value; \
49 __asm__ __volatile__("msr " REGISTER ",%0" : : "ri" (__value64)); \
50}
51#define __set_fpcr(__fpcr) __set("fpcr", __fpcr)
52#define __set_fpsr(__fpsr) __set("fpsr", __fpsr)
Serban Constantinescua147a1d2014-06-08 16:55:22 +010053
54int fegetenv(fenv_t* envp) {
55 __get_fpcr(envp->__control);
56 __get_fpsr(envp->__status);
Calin Juravle2d367902014-02-25 14:49:41 +000057 return 0;
58}
59
Serban Constantinescua147a1d2014-06-08 16:55:22 +010060int fesetenv(const fenv_t* envp) {
61 fpu_control_t fpcr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +010062 __get_fpcr(fpcr);
63 if (envp->__control != fpcr) {
64 __set_fpcr(envp->__control);
65 }
66 __set_fpsr(envp->__status);
Calin Juravle2d367902014-02-25 14:49:41 +000067 return 0;
68}
69
Serban Constantinescua147a1d2014-06-08 16:55:22 +010070int feclearexcept(int excepts) {
71 fpu_status_t fpsr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +010072 __get_fpsr(fpsr);
Elliott Hughesa93431c2022-10-14 20:38:35 +000073 fpsr &= ~(excepts & FE_ALL_EXCEPT);
Serban Constantinescua147a1d2014-06-08 16:55:22 +010074 __set_fpsr(fpsr);
Calin Juravle2d367902014-02-25 14:49:41 +000075 return 0;
76}
77
Serban Constantinescua147a1d2014-06-08 16:55:22 +010078int fegetexceptflag(fexcept_t* flagp, int excepts) {
79 fpu_status_t fpsr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +010080 __get_fpsr(fpsr);
Elliott Hughesa93431c2022-10-14 20:38:35 +000081 *flagp = fpsr & (excepts & FE_ALL_EXCEPT);
Calin Juravle2d367902014-02-25 14:49:41 +000082 return 0;
83}
84
Serban Constantinescua147a1d2014-06-08 16:55:22 +010085int fesetexceptflag(const fexcept_t* flagp, int excepts) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +010086 excepts &= FE_ALL_EXCEPT;
Elliott Hughesa93431c2022-10-14 20:38:35 +000087 fpu_status_t fpsr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +010088 __get_fpsr(fpsr);
89 fpsr &= ~excepts;
90 fpsr |= *flagp & excepts;
91 __set_fpsr(fpsr);
Calin Juravle2d367902014-02-25 14:49:41 +000092 return 0;
93}
94
Serban Constantinescua147a1d2014-06-08 16:55:22 +010095int feraiseexcept(int excepts) {
96 fexcept_t ex = excepts;
Serban Constantinescua147a1d2014-06-08 16:55:22 +010097 fesetexceptflag(&ex, excepts);
Calin Juravle2d367902014-02-25 14:49:41 +000098 return 0;
99}
100
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100101int fetestexcept(int excepts) {
102 fpu_status_t fpsr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100103 __get_fpsr(fpsr);
Elliott Hughesa93431c2022-10-14 20:38:35 +0000104 return (fpsr & (excepts & FE_ALL_EXCEPT));
Calin Juravle2d367902014-02-25 14:49:41 +0000105}
106
107int fegetround(void) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100108 fpu_control_t fpcr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100109 __get_fpcr(fpcr);
110 return ((fpcr >> FPCR_RMODE_SHIFT) & FE_TOWARDZERO);
Calin Juravle2d367902014-02-25 14:49:41 +0000111}
112
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100113int fesetround(int round) {
Elliott Hughes70474312022-10-19 18:40:49 +0000114 if (round < FE_TONEAREST || round > FE_TOWARDZERO) return -1;
Elliott Hughesa93431c2022-10-14 20:38:35 +0000115 fpu_control_t fpcr;
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100116 __get_fpcr(fpcr);
Elliott Hughesa93431c2022-10-14 20:38:35 +0000117 fpu_control_t new_fpcr = fpcr & ~(FE_TOWARDZERO << FPCR_RMODE_SHIFT);
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100118 new_fpcr |= (round << FPCR_RMODE_SHIFT);
119 if (new_fpcr != fpcr) {
120 __set_fpcr(new_fpcr);
121 }
Calin Juravle2d367902014-02-25 14:49:41 +0000122 return 0;
123}
124
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100125int feholdexcept(fenv_t* envp) {
Elliott Hughesa93431c2022-10-14 20:38:35 +0000126 fegetenv(envp);
127 feclearexcept(FE_ALL_EXCEPT);
Calin Juravle2d367902014-02-25 14:49:41 +0000128 return 0;
129}
130
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100131int feupdateenv(const fenv_t* envp) {
Elliott Hughesa93431c2022-10-14 20:38:35 +0000132 int excepts = fetestexcept(FE_ALL_EXCEPT);
133 fesetenv(envp);
134 feraiseexcept(excepts);
Calin Juravle2d367902014-02-25 14:49:41 +0000135 return 0;
136}
137
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700138int feenableexcept(int mask __unused) {
139 return -1;
Calin Juravle2d367902014-02-25 14:49:41 +0000140}
141
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700142int fedisableexcept(int mask __unused) {
143 return 0;
Calin Juravle2d367902014-02-25 14:49:41 +0000144}
145
146int fegetexcept(void) {
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700147 return 0;
Calin Juravle2d367902014-02-25 14:49:41 +0000148}