| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 1 | /*- | 
|  | 2 | * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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: | 
| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 8 | * 1. Redistributions of source code must retain the above copyright | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 9 | *    notice, this list of conditions and the following disclaimer. | 
| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 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. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 13 | * | 
| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 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 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 24 | * SUCH DAMAGE. | 
| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 25 | * | 
|  | 26 | * $FreeBSD: src/lib/msun/arm/fenv.c,v 1.1 2004/06/06 10:03:59 das Exp $ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <fenv.h> | 
|  | 30 |  | 
| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 31 | #define FPSCR_RMODE_SHIFT 22 | 
| Elliott Hughes | 9812a02 | 2014-06-09 13:57:57 -0700 | [diff] [blame] | 32 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 33 | const fenv_t __fe_dfl_env = 0; | 
| Elliott Hughes | 2f5829b | 2023-02-24 00:15:29 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | int fegetenv(fenv_t* __envp) { | 
|  | 36 | fenv_t _fpscr; | 
|  | 37 | __asm__ __volatile__("vmrs %0,fpscr" : "=r"(_fpscr)); | 
|  | 38 | *__envp = _fpscr; | 
|  | 39 | return 0; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | int fesetenv(const fenv_t* __envp) { | 
|  | 43 | fenv_t _fpscr = *__envp; | 
|  | 44 | __asm__ __volatile__("vmsr fpscr,%0" : : "ri"(_fpscr)); | 
|  | 45 | return 0; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | int feclearexcept(int __excepts) { | 
|  | 49 | fexcept_t __fpscr; | 
|  | 50 | fegetenv(&__fpscr); | 
|  | 51 | __fpscr &= ~__excepts; | 
|  | 52 | fesetenv(&__fpscr); | 
|  | 53 | return 0; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | int fegetexceptflag(fexcept_t* __flagp, int __excepts) { | 
|  | 57 | fexcept_t __fpscr; | 
|  | 58 | fegetenv(&__fpscr); | 
|  | 59 | *__flagp = __fpscr & __excepts; | 
|  | 60 | return 0; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | int fesetexceptflag(const fexcept_t* __flagp, int __excepts) { | 
|  | 64 | fexcept_t __fpscr; | 
|  | 65 | fegetenv(&__fpscr); | 
|  | 66 | __fpscr &= ~__excepts; | 
|  | 67 | __fpscr |= *__flagp & __excepts; | 
|  | 68 | fesetenv(&__fpscr); | 
|  | 69 | return 0; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | int feraiseexcept(int __excepts) { | 
|  | 73 | fexcept_t __ex = __excepts; | 
|  | 74 | fesetexceptflag(&__ex, __excepts); | 
|  | 75 | return 0; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | int fetestexcept(int __excepts) { | 
|  | 79 | fexcept_t __fpscr; | 
|  | 80 | fegetenv(&__fpscr); | 
|  | 81 | return (__fpscr & __excepts); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | int fegetround(void) { | 
|  | 85 | fenv_t _fpscr; | 
|  | 86 | fegetenv(&_fpscr); | 
|  | 87 | return ((_fpscr >> FPSCR_RMODE_SHIFT) & 0x3); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | int fesetround(int __round) { | 
|  | 91 | fenv_t _fpscr; | 
|  | 92 | fegetenv(&_fpscr); | 
|  | 93 | _fpscr &= ~(0x3 << FPSCR_RMODE_SHIFT); | 
|  | 94 | _fpscr |= (__round << FPSCR_RMODE_SHIFT); | 
|  | 95 | fesetenv(&_fpscr); | 
|  | 96 | return 0; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | int feholdexcept(fenv_t* __envp) { | 
|  | 100 | fenv_t __env; | 
|  | 101 | fegetenv(&__env); | 
|  | 102 | *__envp = __env; | 
|  | 103 | __env &= ~FE_ALL_EXCEPT; | 
|  | 104 | fesetenv(&__env); | 
|  | 105 | return 0; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | int feupdateenv(const fenv_t* __envp) { | 
|  | 109 | fexcept_t __fpscr; | 
|  | 110 | fegetenv(&__fpscr); | 
|  | 111 | fesetenv(__envp); | 
|  | 112 | feraiseexcept(__fpscr & FE_ALL_EXCEPT); | 
|  | 113 | return 0; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | int feenableexcept(int __mask __unused) { | 
|  | 117 | return -1; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | int fedisableexcept(int __mask __unused) { | 
|  | 121 | return 0; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | int fegetexcept(void) { | 
|  | 125 | return 0; | 
|  | 126 | } |