1 1.3 martin /* $NetBSD: fenv.c,v 1.3 2017/12/30 17:59:24 martin Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG> 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 christos * SUCH DAMAGE. 27 1.1 christos * 28 1.1 christos * $FreeBSD: src/lib/msun/alpha/fenv.c,v 1.2 2005/03/16 19:03:44 das Exp $ 29 1.1 christos */ 30 1.1 christos #include <sys/cdefs.h> 31 1.3 martin __RCSID("$NetBSD: fenv.c,v 1.3 2017/12/30 17:59:24 martin Exp $"); 32 1.2 chs 33 1.2 chs #include "namespace.h" 34 1.2 chs 35 1.2 chs #include <machine/sysarch.h> 36 1.2 chs #include <fenv.h> 37 1.1 christos 38 1.1 christos #ifdef __weak_alias 39 1.2 chs __weak_alias(fegetenv,_fegetenv) 40 1.2 chs __weak_alias(feholdexcept,_feholdexcept) 41 1.2 chs __weak_alias(fesetenv,_fesetenv) 42 1.2 chs __weak_alias(feupdateenv,_feupdateenv) 43 1.2 chs __weak_alias(feenableexcept,_feenableexcept) 44 1.2 chs __weak_alias(fedisableexcept,_fedisableexcept) 45 1.2 chs __weak_alias(fegetexcept,_fegetexcept) 46 1.1 christos #endif 47 1.1 christos 48 1.1 christos const fenv_t __fe_dfl_env = 0x680e000000000000ULL; 49 1.1 christos 50 1.1 christos /* 51 1.1 christos * The lower 49 bits of the FPCR are unused by the hardware, so we use 52 1.1 christos * the lower order bits to store the kernel's idea of the FP mask as 53 1.1 christos * described in the Alpha Architecture Manual. 54 1.1 christos */ 55 1.1 christos int 56 1.1 christos fegetenv(fenv_t *envp) 57 1.1 christos { 58 1.1 christos struct alpha_fp_except_args p; 59 1.1 christos union __fpcr r; 60 1.1 christos 61 1.1 christos /* 62 1.1 christos * The syscall acts as an implicit exception barrier, so we 63 1.1 christos * only need to issue an excb after the mf_fpcr to ensure that 64 1.1 christos * the read is executed before any subsequent FP ops. 65 1.1 christos */ 66 1.3 martin p.mask = sysarch(ALPHA_FPGETMASK, 0); 67 1.1 christos __mf_fpcr(&r.__d); 68 1.1 christos *envp = r.__bits | p.mask; 69 1.1 christos __excb(); 70 1.1 christos return 0; 71 1.1 christos } 72 1.1 christos 73 1.1 christos int 74 1.1 christos feholdexcept(fenv_t *envp) 75 1.1 christos { 76 1.1 christos struct alpha_fp_except_args p; 77 1.1 christos union __fpcr r; 78 1.1 christos 79 1.3 martin p.mask = sysarch(ALPHA_FPGETMASK, 0); 80 1.1 christos __mf_fpcr(&r.__d); 81 1.1 christos *envp = r.__bits | p.mask; 82 1.1 christos r.__bits &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT); 83 1.1 christos __mt_fpcr(r.__d); 84 1.1 christos if (p.mask & FE_ALL_EXCEPT) { 85 1.1 christos p.mask = 0; 86 1.1 christos sysarch(ALPHA_FPSETMASK, &p); 87 1.1 christos } 88 1.1 christos __excb(); 89 1.1 christos return 0; 90 1.1 christos } 91 1.1 christos 92 1.1 christos int 93 1.1 christos fesetenv(const fenv_t *envp) 94 1.1 christos { 95 1.1 christos struct alpha_fp_except_args p; 96 1.1 christos union __fpcr r; 97 1.1 christos 98 1.1 christos p.mask = *envp & FE_ALL_EXCEPT; 99 1.1 christos sysarch(ALPHA_FPSETMASK, &p); 100 1.1 christos r.__bits = *envp & ~FE_ALL_EXCEPT; 101 1.1 christos __mt_fpcr(r.__d); 102 1.1 christos __excb(); 103 1.1 christos return 0; 104 1.1 christos } 105 1.1 christos 106 1.1 christos int 107 1.1 christos feupdateenv(const fenv_t *envp) 108 1.1 christos { 109 1.1 christos struct alpha_fp_except_args p; 110 1.1 christos union __fpcr oldr, newr; 111 1.1 christos 112 1.1 christos p.mask = *envp & FE_ALL_EXCEPT; 113 1.1 christos sysarch(ALPHA_FPSETMASK, &p); 114 1.1 christos __mf_fpcr(&oldr.__d); 115 1.1 christos newr.__bits = *envp & ~FE_ALL_EXCEPT; 116 1.1 christos __excb(); 117 1.1 christos __mt_fpcr(newr.__d); 118 1.1 christos feraiseexcept((oldr.__bits >> _FPUSW_SHIFT) & FE_ALL_EXCEPT); 119 1.1 christos return 0; 120 1.1 christos } 121 1.1 christos 122 1.1 christos int 123 1.1 christos feenableexcept(int mask) 124 1.1 christos { 125 1.1 christos struct alpha_fp_except_args p; 126 1.1 christos 127 1.3 martin p.mask = sysarch(ALPHA_FPGETMASK, 0); 128 1.1 christos p.mask |= (mask & FE_ALL_EXCEPT); 129 1.1 christos sysarch(ALPHA_FPSETMASK, &p); 130 1.1 christos return p.mask; 131 1.1 christos } 132 1.1 christos 133 1.1 christos int 134 1.1 christos fedisableexcept(int mask) 135 1.1 christos { 136 1.1 christos struct alpha_fp_except_args p; 137 1.1 christos 138 1.3 martin p.mask = sysarch(ALPHA_FPGETMASK, 0); 139 1.1 christos p.mask &= ~(mask & FE_ALL_EXCEPT); 140 1.1 christos sysarch(ALPHA_FPSETMASK, &p); 141 1.1 christos return p.mask; 142 1.1 christos } 143 1.1 christos 144 1.1 christos int 145 1.1 christos fegetexcept(void) 146 1.1 christos { 147 1.1 christos 148 1.3 martin return sysarch(ALPHA_FPGETMASK, 0); 149 1.1 christos } 150