1122e1380SDavid Schultz /*- 25e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * $FreeBSD$ 29122e1380SDavid Schultz */ 30122e1380SDavid Schultz 31d78e594bSDavid Schultz #define __fenv_static 325d9fefacSDavid Schultz #include "fenv.h" 3366405796SJustin Hibbits #ifdef __SPE__ 3466405796SJustin Hibbits #include <sys/types.h> 3566405796SJustin Hibbits #include <machine/spr.h> 3666405796SJustin Hibbits #endif 37122e1380SDavid Schultz 38d78e594bSDavid Schultz #ifdef __GNUC_GNU_INLINE__ 39d78e594bSDavid Schultz #error "This file must be compiled with C99 'inline' semantics" 40d78e594bSDavid Schultz #endif 41d78e594bSDavid Schultz 422b9a828cSJustin Hibbits #ifdef __SPE__ 432b9a828cSJustin Hibbits const fenv_t __fe_dfl_env = SPEFSCR_DFLT; 442b9a828cSJustin Hibbits #else 45122e1380SDavid Schultz const fenv_t __fe_dfl_env = 0x00000000; 462b9a828cSJustin Hibbits #endif 47d78e594bSDavid Schultz 48d78e594bSDavid Schultz extern inline int feclearexcept(int __excepts); 49d78e594bSDavid Schultz extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts); 50d78e594bSDavid Schultz extern inline int fesetexceptflag(const fexcept_t *__flagp, int __excepts); 5166405796SJustin Hibbits #ifndef __SPE__ 52d78e594bSDavid Schultz extern inline int feraiseexcept(int __excepts); 5366405796SJustin Hibbits #endif 54d78e594bSDavid Schultz extern inline int fetestexcept(int __excepts); 55d78e594bSDavid Schultz extern inline int fegetround(void); 56d78e594bSDavid Schultz extern inline int fesetround(int __round); 57d78e594bSDavid Schultz extern inline int fegetenv(fenv_t *__envp); 58d78e594bSDavid Schultz extern inline int feholdexcept(fenv_t *__envp); 59d78e594bSDavid Schultz extern inline int fesetenv(const fenv_t *__envp); 60d78e594bSDavid Schultz extern inline int feupdateenv(const fenv_t *__envp); 61*448c505cSJustin Hibbits extern inline int feenableexcept(int __mask); 62*448c505cSJustin Hibbits extern inline int fedisableexcept(int __mask); 6366405796SJustin Hibbits 6466405796SJustin Hibbits #ifdef __SPE__ 6566405796SJustin Hibbits #define PMAX 0x7f7fffff 6666405796SJustin Hibbits #define PMIN 0x00800000 6766405796SJustin Hibbits int feraiseexcept(int __excepts) 6866405796SJustin Hibbits { 6966405796SJustin Hibbits uint32_t spefscr; 7066405796SJustin Hibbits 7166405796SJustin Hibbits spefscr = mfspr(SPR_SPEFSCR); 7266405796SJustin Hibbits mtspr(SPR_SPEFSCR, spefscr | (__excepts & FE_ALL_EXCEPT)); 7366405796SJustin Hibbits 7466405796SJustin Hibbits if (__excepts & FE_INVALID) 7566405796SJustin Hibbits __asm __volatile ("efsdiv %0, %0, %1" :: "r"(0), "r"(0)); 7666405796SJustin Hibbits if (__excepts & FE_DIVBYZERO) 7766405796SJustin Hibbits __asm __volatile ("efsdiv %0, %0, %1" :: "r"(1.0f), "r"(0)); 7866405796SJustin Hibbits if (__excepts & FE_UNDERFLOW) 7966405796SJustin Hibbits __asm __volatile ("efsmul %0, %0, %0" :: "r"(PMIN)); 8066405796SJustin Hibbits if (__excepts & FE_OVERFLOW) 8166405796SJustin Hibbits __asm __volatile ("efsadd %0, %0, %0" :: "r"(PMAX)); 8266405796SJustin Hibbits if (__excepts & FE_INEXACT) 8366405796SJustin Hibbits __asm __volatile ("efssub %0, %0, %1" :: "r"(PMIN), "r"(1.0f)); 8466405796SJustin Hibbits return (0); 8566405796SJustin Hibbits } 8666405796SJustin Hibbits #endif 87