1 /*-
2  * Copyright (c) 2004-2005 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$
27  */
28 
29 #ifndef	_FENV_H_
30 #define	_FENV_H_
31 
32 #include <sys/_types.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 typedef	__uint64_t	fenv_t;
39 typedef	__uint64_t	fexcept_t;
40 
41 /*
42  * We can't tell if we're using compiler-rt or libgcc; instead
43  * we assume a connection between the compiler in use and
44  * the runtime library.
45  */
46 #if defined(__clang__)
47 
48 /* compiler-rt has no exceptions in soft-float */
49 
50 #define PICOLIBC_LONG_DOUBLE_NOROUND
51 #define PICOLIBC_LONG_DOUBLE_NOEXCEPT
52 
53 #if (__ARM_FP & 0x8) == 0
54 #define PICOLIBC_DOUBLE_NOROUND
55 #define PICOLIBC_DOUBLE_NOEXCEPT
56 #endif
57 
58 #if (__ARM_FP & 0x4) == 0
59 #define PICOLIBC_FLOAT_NOROUND
60 #define PICOLIBC_FLOAT_NOEXCEPT
61 #endif
62 
63 #else
64 
65 /* libgcc has exceptions when there is an FPU */
66 
67 #if __ARM_FP == 0
68 #define PICOLIBC_LONG_DOUBLE_NOROUND
69 #define PICOLIBC_LONG_DOUBLE_NOEXCEPT
70 #define PICOLIBC_DOUBLE_NOROUND
71 #define PICOLIBC_DOUBLE_NOEXCEPT
72 #define PICOLIBC_FLOAT_NOROUND
73 #define PICOLIBC_FLOAT_NOEXCEPT
74 #endif
75 
76 #endif
77 
78 #if __ARM_FP
79 
80 /* Exception flags */
81 #define	FE_INVALID	0x00000001
82 #define	FE_DIVBYZERO	0x00000002
83 #define	FE_OVERFLOW	0x00000004
84 #define	FE_UNDERFLOW	0x00000008
85 #define	FE_INEXACT	0x00000010
86 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | \
87 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
88 
89 /*
90  * Rounding modes
91  *
92  * We can't just use the hardware bit values here, because that would
93  * make FE_UPWARD and FE_DOWNWARD negative, which is not allowed.
94  */
95 #define	FE_TONEAREST	0x0
96 #define	FE_UPWARD	0x1
97 #define	FE_DOWNWARD	0x2
98 #define	FE_TOWARDZERO	0x3
99 #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
100 			 FE_UPWARD | FE_TOWARDZERO)
101 #define	_ROUND_SHIFT	22
102 
103 /* We need to be able to map status flag positions to mask flag positions */
104 #define _FPUSW_SHIFT	8
105 #define	_ENABLE_MASK	(FE_ALL_EXCEPT << _FPUSW_SHIFT)
106 
107 #define	__mrs_fpcr(__r)	__asm __volatile("mrs %0, fpcr" : "=r" (__r))
108 #define	__msr_fpcr(__r)	__asm __volatile("msr fpcr, %0" : : "r" (__r))
109 
110 #define	__mrs_fpsr(__r)	__asm __volatile("mrs %0, fpsr" : "=r" (__r))
111 #define	__msr_fpsr(__r)	__asm __volatile("msr fpsr, %0" : : "r" (__r))
112 
113 #else
114 #define	FE_TONEAREST		0x00000000
115 #endif
116 
117 #if !defined(__declare_fenv_inline) && defined(__declare_extern_inline)
118 #define	__declare_fenv_inline(type) __declare_extern_inline(type)
119 #endif
120 
121 #ifdef __declare_fenv_inline
122 #if __ARM_FP
123 #include <machine/fenv-fp.h>
124 #else
125 #include <machine/fenv-softfloat.h>
126 #endif
127 
128 #endif
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif	/* !_FENV_H_ */
135