1122e1380SDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
4122e1380SDavid Schultz * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
5122e1380SDavid Schultz * All rights reserved.
6122e1380SDavid Schultz *
7122e1380SDavid Schultz * Redistribution and use in source and binary forms, with or without
8122e1380SDavid Schultz * modification, are permitted provided that the following conditions
9122e1380SDavid Schultz * are met:
10122e1380SDavid Schultz * 1. Redistributions of source code must retain the above copyright
11122e1380SDavid Schultz * notice, this list of conditions and the following disclaimer.
12122e1380SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright
13122e1380SDavid Schultz * notice, this list of conditions and the following disclaimer in the
14122e1380SDavid Schultz * documentation and/or other materials provided with the distribution.
15122e1380SDavid Schultz *
16122e1380SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17122e1380SDavid Schultz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18122e1380SDavid Schultz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19122e1380SDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20122e1380SDavid Schultz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21122e1380SDavid Schultz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22122e1380SDavid Schultz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23122e1380SDavid Schultz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24122e1380SDavid Schultz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25122e1380SDavid Schultz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26122e1380SDavid Schultz * SUCH DAMAGE.
27122e1380SDavid Schultz */
28122e1380SDavid Schultz
29d78e594bSDavid Schultz #define __fenv_static
305d9fefacSDavid Schultz #include "fenv.h"
3166405796SJustin Hibbits #ifdef __SPE__
3266405796SJustin Hibbits #include <sys/types.h>
3366405796SJustin Hibbits #include <machine/spr.h>
3466405796SJustin Hibbits #endif
35122e1380SDavid Schultz
36d78e594bSDavid Schultz #ifdef __GNUC_GNU_INLINE__
37d78e594bSDavid Schultz #error "This file must be compiled with C99 'inline' semantics"
38d78e594bSDavid Schultz #endif
39d78e594bSDavid Schultz
402b9a828cSJustin Hibbits #ifdef __SPE__
412b9a828cSJustin Hibbits const fenv_t __fe_dfl_env = SPEFSCR_DFLT;
422b9a828cSJustin Hibbits #else
43122e1380SDavid Schultz const fenv_t __fe_dfl_env = 0x00000000;
442b9a828cSJustin Hibbits #endif
45d78e594bSDavid Schultz
46d78e594bSDavid Schultz extern inline int feclearexcept(int __excepts);
47d78e594bSDavid Schultz extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
48d78e594bSDavid Schultz extern inline int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
4966405796SJustin Hibbits #ifndef __SPE__
50d78e594bSDavid Schultz extern inline int feraiseexcept(int __excepts);
5166405796SJustin Hibbits #endif
52d78e594bSDavid Schultz extern inline int fetestexcept(int __excepts);
53d78e594bSDavid Schultz extern inline int fegetround(void);
54d78e594bSDavid Schultz extern inline int fesetround(int __round);
55d78e594bSDavid Schultz extern inline int fegetenv(fenv_t *__envp);
56d78e594bSDavid Schultz extern inline int feholdexcept(fenv_t *__envp);
57d78e594bSDavid Schultz extern inline int fesetenv(const fenv_t *__envp);
58d78e594bSDavid Schultz extern inline int feupdateenv(const fenv_t *__envp);
59448c505cSJustin Hibbits extern inline int feenableexcept(int __mask);
60448c505cSJustin Hibbits extern inline int fedisableexcept(int __mask);
6166405796SJustin Hibbits
6266405796SJustin Hibbits #ifdef __SPE__
6366405796SJustin Hibbits #define PMAX 0x7f7fffff
6466405796SJustin Hibbits #define PMIN 0x00800000
feraiseexcept(int __excepts)6566405796SJustin Hibbits int feraiseexcept(int __excepts)
6666405796SJustin Hibbits {
6766405796SJustin Hibbits uint32_t spefscr;
6866405796SJustin Hibbits
6966405796SJustin Hibbits spefscr = mfspr(SPR_SPEFSCR);
7066405796SJustin Hibbits mtspr(SPR_SPEFSCR, spefscr | (__excepts & FE_ALL_EXCEPT));
7166405796SJustin Hibbits
7266405796SJustin Hibbits if (__excepts & FE_INVALID)
7366405796SJustin Hibbits __asm __volatile ("efsdiv %0, %0, %1" :: "r"(0), "r"(0));
7466405796SJustin Hibbits if (__excepts & FE_DIVBYZERO)
7566405796SJustin Hibbits __asm __volatile ("efsdiv %0, %0, %1" :: "r"(1.0f), "r"(0));
7666405796SJustin Hibbits if (__excepts & FE_UNDERFLOW)
7766405796SJustin Hibbits __asm __volatile ("efsmul %0, %0, %0" :: "r"(PMIN));
7866405796SJustin Hibbits if (__excepts & FE_OVERFLOW)
7966405796SJustin Hibbits __asm __volatile ("efsadd %0, %0, %0" :: "r"(PMAX));
8066405796SJustin Hibbits if (__excepts & FE_INEXACT)
8166405796SJustin Hibbits __asm __volatile ("efssub %0, %0, %1" :: "r"(PMIN), "r"(1.0f));
8266405796SJustin Hibbits return (0);
8366405796SJustin Hibbits }
8466405796SJustin Hibbits #endif
85