Home | History | Annotate | Line # | Download | only in aarch64
fenv.c revision 1.3.12.1
      1  1.3.12.1  christos /* $NetBSD: fenv.c,v 1.3.12.1 2019/06/10 22:05:24 christos Exp $ */
      2       1.1      matt 
      3       1.1      matt /*-
      4       1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5       1.1      matt  * All rights reserved.
      6       1.1      matt  *
      7       1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9       1.1      matt  *
     10       1.1      matt  * Redistribution and use in source and binary forms, with or without
     11       1.1      matt  * modification, are permitted provided that the following conditions
     12       1.1      matt  * are met:
     13       1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14       1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15       1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      matt  *    documentation and/or other materials provided with the distribution.
     18       1.1      matt  *
     19       1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      matt  */
     31       1.1      matt 
     32       1.1      matt #include <sys/cdefs.h>
     33  1.3.12.1  christos __RCSID("$NetBSD: fenv.c,v 1.3.12.1 2019/06/10 22:05:24 christos Exp $");
     34       1.3       chs 
     35       1.3       chs #include "namespace.h"
     36       1.1      matt 
     37       1.1      matt #include <sys/param.h>
     38       1.1      matt #include <sys/types.h>
     39       1.1      matt #include <assert.h>
     40       1.1      matt #include <fenv.h>
     41       1.1      matt #include <string.h>
     42       1.1      matt #include <unistd.h>
     43       1.1      matt #include <inttypes.h>
     44       1.1      matt 
     45       1.1      matt #include <aarch64/armreg.h>
     46       1.1      matt 
     47       1.3       chs #ifdef __weak_alias
     48       1.3       chs __weak_alias(feclearexcept,_feclearexcept)
     49       1.3       chs __weak_alias(fedisableexcept,_fedisableexcept)
     50       1.3       chs __weak_alias(feenableexcept,_feenableexcept)
     51       1.3       chs __weak_alias(fegetenv,_fegetenv)
     52       1.3       chs __weak_alias(fegetexcept,_fegetexcept)
     53       1.3       chs __weak_alias(fegetexceptflag,_fegetexceptflag)
     54       1.3       chs __weak_alias(fegetround,_fegetround)
     55       1.3       chs __weak_alias(feholdexcept,_feholdexcept)
     56       1.3       chs __weak_alias(feraiseexcept,_feraiseexcept)
     57       1.3       chs __weak_alias(fesetenv,_fesetenv)
     58       1.3       chs __weak_alias(fesetexceptflag,_fesetexceptflag)
     59       1.3       chs __weak_alias(fesetround,_fesetround)
     60       1.3       chs __weak_alias(fetestexcept,_fetestexcept)
     61       1.3       chs __weak_alias(feupdateenv,_feupdateenv)
     62       1.3       chs #endif
     63       1.3       chs 
     64       1.1      matt const fenv_t __fe_dfl_env = {
     65       1.1      matt 	.__fpsr = 0,
     66  1.3.12.1  christos 	.__fpcr = __SHIFTIN(FPCR_RN, FPCR_RMODE),
     67       1.1      matt };
     68       1.1      matt 
     69       1.1      matt /*
     70       1.1      matt  * The feclearexcept() function shall attempt to clear the supported
     71       1.1      matt  * floating-point exceptions represented by excepts.
     72       1.1      matt  */
     73       1.1      matt int
     74       1.1      matt feclearexcept(int excepts)
     75       1.1      matt {
     76       1.1      matt #ifndef lint
     77       1.2    martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
     78       1.1      matt #endif
     79       1.1      matt 	unsigned int tmp = reg_fpsr_read() & ~__SHIFTIN(excepts, FPSR_CSUM);
     80       1.1      matt 	reg_fpsr_write(tmp);
     81       1.1      matt 	return 0;
     82       1.1      matt }
     83       1.1      matt 
     84       1.1      matt /*
     85       1.1      matt  * The fegetexceptflag() function shall attempt to store an
     86       1.1      matt  * implementation-defined representation of the states of the floating-point
     87       1.1      matt  * status flags indicated by the argument excepts in the object pointed to by
     88       1.1      matt  * the argument flagp.
     89       1.1      matt  */
     90       1.1      matt int
     91       1.1      matt fegetexceptflag(fexcept_t *flagp, int excepts)
     92       1.1      matt {
     93       1.2    martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
     94       1.1      matt 	*flagp = __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts;
     95       1.1      matt 	return 0;
     96       1.1      matt }
     97       1.1      matt 
     98       1.1      matt /*
     99       1.1      matt  * The feraiseexcept() function shall attempt to raise the supported
    100       1.1      matt  * floating-point exceptions represented by the argument excepts. The order
    101       1.1      matt  * in which these floating-point exceptions are raised is unspecified.
    102       1.1      matt  */
    103       1.1      matt int
    104       1.1      matt feraiseexcept(int excepts)
    105       1.1      matt {
    106       1.1      matt #ifndef lint
    107       1.2    martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    108       1.1      matt #endif
    109       1.1      matt 	unsigned int fpsr = reg_fpsr_read();
    110       1.1      matt 	fpsr = (fpsr & ~FPSR_CSUM) | __SHIFTIN(excepts, FPSR_CSUM);
    111       1.1      matt 	reg_fpsr_write(fpsr);
    112       1.1      matt 	unsigned int fpcr = reg_fpcr_read();
    113       1.1      matt 	fpcr = (fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM);
    114       1.1      matt 	reg_fpcr_write(fpcr);
    115       1.1      matt 	return 0;
    116       1.1      matt }
    117       1.1      matt 
    118       1.1      matt /*
    119       1.1      matt  * The fesetexceptflag() function shall attempt to set the floating-point
    120       1.1      matt  * status flags indicated by the argument excepts to the states stored in the
    121       1.1      matt  * object pointed to by flagp. The value pointed to by flagp shall have been
    122       1.1      matt  * set by a previous call to fegetexceptflag() whose second argument
    123       1.1      matt  * represented at least those floating-point exceptions represented by the
    124       1.1      matt  * argument excepts. This function does not raise floating-point exceptions,
    125       1.1      matt  * but only sets the state of the flags.
    126       1.1      matt  */
    127       1.1      matt int
    128       1.1      matt fesetexceptflag(const fexcept_t *flagp, int excepts)
    129       1.1      matt {
    130       1.1      matt #ifndef lint
    131       1.2    martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    132       1.1      matt #endif
    133       1.1      matt 	unsigned int fpsr = reg_fpsr_read();
    134       1.1      matt 	fpsr &= ~__SHIFTIN(excepts, FPSR_CSUM);
    135       1.1      matt 	fpsr |= __SHIFTIN((*flagp & excepts), FPSR_CSUM);
    136       1.1      matt 	reg_fpsr_write(fpsr);
    137       1.1      matt 	return 0;
    138       1.1      matt }
    139       1.1      matt 
    140       1.1      matt /*
    141       1.1      matt  * The fetestexcept() function shall determine which of a specified subset of
    142       1.1      matt  * the floating-point exception flags are currently set. The excepts argument
    143       1.1      matt  * specifies the floating-point status flags to be queried.
    144       1.1      matt  */
    145       1.1      matt int
    146       1.1      matt fetestexcept(int excepts)
    147       1.1      matt {
    148       1.2    martin 	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
    149       1.1      matt 	return __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts;
    150       1.1      matt }
    151       1.1      matt 
    152       1.1      matt /*
    153       1.1      matt  * The fegetround() function shall get the current rounding direction.
    154       1.1      matt  */
    155       1.1      matt int
    156       1.1      matt fegetround(void)
    157       1.1      matt {
    158       1.1      matt 	return __SHIFTOUT(reg_fpcr_read(), FPCR_RMODE);
    159       1.1      matt }
    160       1.1      matt 
    161       1.1      matt /*
    162       1.1      matt  * The fesetround() function shall establish the rounding direction represented
    163       1.1      matt  * by its argument round. If the argument is not equal to the value of a
    164       1.1      matt  * rounding direction macro, the rounding direction is not changed.
    165       1.1      matt  */
    166       1.1      matt int
    167       1.1      matt fesetround(int round)
    168       1.1      matt {
    169       1.1      matt #ifndef lint
    170       1.1      matt 	_DIAGASSERT(!(round & ~__SHIFTOUT(FPCR_RMODE, FPCR_RMODE)));
    171       1.1      matt #endif
    172       1.1      matt 	unsigned int fpcr = reg_fpcr_read() & ~FPCR_RMODE;
    173       1.1      matt 	fpcr |= __SHIFTIN(round, FPCR_RMODE);
    174       1.1      matt 	reg_fpcr_write(fpcr);
    175       1.1      matt 	return 0;
    176       1.1      matt }
    177       1.1      matt 
    178       1.1      matt /*
    179       1.1      matt  * The fegetenv() function shall attempt to store the current floating-point
    180       1.1      matt  * environment in the object pointed to by envp.
    181       1.1      matt  */
    182       1.1      matt int
    183       1.1      matt fegetenv(fenv_t *envp)
    184       1.1      matt {
    185       1.1      matt 	envp->__fpcr = reg_fpcr_read();
    186       1.1      matt 	envp->__fpsr = reg_fpsr_read();
    187       1.1      matt 	return 0;
    188       1.1      matt }
    189       1.1      matt 
    190       1.1      matt /*
    191       1.1      matt  * The feholdexcept() function shall save the current floating-point
    192       1.1      matt  * environment in the object pointed to by envp, clear the floating-point
    193       1.1      matt  * status flags, and then install a non-stop (continue on floating-point
    194       1.1      matt  * exceptions) mode, if available, for all floating-point exceptions.
    195       1.1      matt  */
    196       1.1      matt int
    197       1.1      matt feholdexcept(fenv_t *envp)
    198       1.1      matt {
    199       1.1      matt 	envp->__fpsr = reg_fpsr_read();
    200       1.1      matt 	envp->__fpcr = reg_fpcr_read();
    201       1.1      matt 	reg_fpsr_write(envp->__fpsr & ~FPSR_CSUM);
    202       1.1      matt 	reg_fpcr_write(envp->__fpcr & ~FPCR_ESUM);
    203       1.1      matt 	return 0;
    204       1.1      matt }
    205       1.1      matt 
    206       1.1      matt /*
    207       1.1      matt  * The fesetenv() function shall attempt to establish the floating-point
    208       1.1      matt  * environment represented by the object pointed to by envp. The fesetenv()
    209       1.1      matt  * function does not raise floating-point exceptions, but only installs the
    210       1.1      matt  * state of the floating-point status flags represented through its argument.
    211       1.1      matt  */
    212       1.1      matt int
    213       1.1      matt fesetenv(const fenv_t *envp)
    214       1.1      matt {
    215       1.1      matt 	reg_fpsr_write(envp->__fpsr);
    216       1.1      matt 	return 0;
    217       1.1      matt }
    218       1.1      matt 
    219       1.1      matt /*
    220       1.1      matt  * The feupdateenv() function shall attempt to save the currently raised
    221       1.1      matt  * floating-point exceptions in its automatic storage, attempt to install the
    222       1.1      matt  * floating-point environment represented by the object pointed to by envp,
    223       1.1      matt  * and then attempt to raise the saved floating-point exceptions.
    224       1.1      matt  */
    225       1.1      matt int
    226       1.1      matt feupdateenv(const fenv_t *envp)
    227       1.1      matt {
    228       1.1      matt #ifndef lint
    229       1.1      matt 	_DIAGASSERT(envp != NULL);
    230       1.1      matt #endif
    231       1.1      matt 	reg_fpsr_write(envp->__fpsr);
    232       1.1      matt 	reg_fpcr_write(envp->__fpcr);
    233       1.1      matt 
    234       1.1      matt 	/* Success */
    235       1.1      matt 	return 0;
    236       1.1      matt }
    237       1.1      matt 
    238       1.1      matt int
    239       1.1      matt feenableexcept(int excepts)
    240       1.1      matt {
    241       1.1      matt 	const uint32_t __fpcr = reg_fpcr_read();
    242       1.1      matt 	reg_fpcr_write((__fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM));
    243       1.1      matt 	return __SHIFTOUT(__fpcr, FPCR_ESUM);
    244       1.1      matt }
    245       1.1      matt 
    246       1.1      matt int
    247       1.1      matt fedisableexcept(int excepts)
    248       1.1      matt {
    249       1.1      matt 	const uint32_t __fpcr = reg_fpcr_read();
    250       1.1      matt 	reg_fpcr_write(__fpcr & ~__SHIFTIN(excepts, FPCR_ESUM));
    251       1.1      matt 	return __SHIFTOUT(__fpcr, FPCR_ESUM);
    252       1.1      matt }
    253       1.1      matt 
    254       1.1      matt int
    255       1.1      matt fegetexcept(void)
    256       1.1      matt {
    257       1.1      matt 	const uint32_t __fpcr = reg_fpcr_read();
    258       1.1      matt 	return __SHIFTOUT(__fpcr, FPCR_ESUM);
    259       1.1      matt }
    260