Home | History | Annotate | Line # | Download | only in alpha
fenv.c revision 1.1
      1  1.1  christos /*	$NetBSD: fenv.c,v 1.1 2016/08/23 10:00:15 christos 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.1  christos __RCSID("$NetBSD: fenv.c,v 1.1 2016/08/23 10:00:15 christos Exp $");
     32  1.1  christos 
     33  1.1  christos #ifdef __weak_alias
     34  1.1  christos #define feenableexcept _feenableexcept
     35  1.1  christos #define fedisableexcept _fedisableexcept
     36  1.1  christos #define fegetexcept _fegetexcept
     37  1.1  christos #endif
     38  1.1  christos #include <machine/sysarch.h>
     39  1.1  christos #include <fenv.h>
     40  1.1  christos 
     41  1.1  christos const fenv_t __fe_dfl_env = 0x680e000000000000ULL;
     42  1.1  christos 
     43  1.1  christos /*
     44  1.1  christos  * The lower 49 bits of the FPCR are unused by the hardware, so we use
     45  1.1  christos  * the lower order bits to store the kernel's idea of the FP mask as
     46  1.1  christos  * described in the Alpha Architecture Manual.
     47  1.1  christos  */
     48  1.1  christos int
     49  1.1  christos fegetenv(fenv_t *envp)
     50  1.1  christos {
     51  1.1  christos 	struct alpha_fp_except_args p;
     52  1.1  christos 	union __fpcr r;
     53  1.1  christos 
     54  1.1  christos 	/*
     55  1.1  christos 	 * The syscall acts as an implicit exception barrier, so we
     56  1.1  christos 	 * only need to issue an excb after the mf_fpcr to ensure that
     57  1.1  christos 	 * the read is executed before any subsequent FP ops.
     58  1.1  christos 	 */
     59  1.1  christos 	sysarch(ALPHA_FPGETMASK, (char *)&p);
     60  1.1  christos 	__mf_fpcr(&r.__d);
     61  1.1  christos 	*envp = r.__bits | p.mask;
     62  1.1  christos 	__excb();
     63  1.1  christos 	return 0;
     64  1.1  christos }
     65  1.1  christos 
     66  1.1  christos int
     67  1.1  christos feholdexcept(fenv_t *envp)
     68  1.1  christos {
     69  1.1  christos 	struct alpha_fp_except_args p;
     70  1.1  christos 	union __fpcr r;
     71  1.1  christos 
     72  1.1  christos 	sysarch(ALPHA_FPGETMASK, (char *)&p);
     73  1.1  christos 	__mf_fpcr(&r.__d);
     74  1.1  christos 	*envp = r.__bits | p.mask;
     75  1.1  christos 	r.__bits &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT);
     76  1.1  christos 	__mt_fpcr(r.__d);
     77  1.1  christos 	if (p.mask & FE_ALL_EXCEPT) {
     78  1.1  christos 		p.mask = 0;
     79  1.1  christos 		sysarch(ALPHA_FPSETMASK, &p);
     80  1.1  christos 	}
     81  1.1  christos 	__excb();
     82  1.1  christos 	return 0;
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos int
     86  1.1  christos fesetenv(const fenv_t *envp)
     87  1.1  christos {
     88  1.1  christos 	struct alpha_fp_except_args p;
     89  1.1  christos 	union __fpcr r;
     90  1.1  christos 
     91  1.1  christos 	p.mask = *envp & FE_ALL_EXCEPT;
     92  1.1  christos 	sysarch(ALPHA_FPSETMASK, &p);
     93  1.1  christos 	r.__bits = *envp & ~FE_ALL_EXCEPT;
     94  1.1  christos 	__mt_fpcr(r.__d);
     95  1.1  christos 	__excb();
     96  1.1  christos 	return 0;
     97  1.1  christos }
     98  1.1  christos 
     99  1.1  christos int
    100  1.1  christos feupdateenv(const fenv_t *envp)
    101  1.1  christos {
    102  1.1  christos 	struct alpha_fp_except_args p;
    103  1.1  christos 	union __fpcr oldr, newr;
    104  1.1  christos 
    105  1.1  christos 	p.mask = *envp & FE_ALL_EXCEPT;
    106  1.1  christos 	sysarch(ALPHA_FPSETMASK, &p);
    107  1.1  christos 	__mf_fpcr(&oldr.__d);
    108  1.1  christos 	newr.__bits = *envp & ~FE_ALL_EXCEPT;
    109  1.1  christos 	__excb();
    110  1.1  christos 	__mt_fpcr(newr.__d);
    111  1.1  christos 	feraiseexcept((oldr.__bits >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
    112  1.1  christos 	return 0;
    113  1.1  christos }
    114  1.1  christos 
    115  1.1  christos #ifdef __weak_alias
    116  1.1  christos __weak_alias(feenableexcept, _feenableexcept);
    117  1.1  christos __weak_alias(fedisableexcept, _fedisableexcept);
    118  1.1  christos __weak_alias(fegetexcept, _fegetexcept);
    119  1.1  christos #endif
    120  1.1  christos 
    121  1.1  christos int
    122  1.1  christos feenableexcept(int mask)
    123  1.1  christos {
    124  1.1  christos 	struct alpha_fp_except_args p;
    125  1.1  christos 
    126  1.1  christos 	sysarch(ALPHA_FPGETMASK, &p);
    127  1.1  christos 	p.mask |= (mask & FE_ALL_EXCEPT);
    128  1.1  christos 	sysarch(ALPHA_FPSETMASK, &p);
    129  1.1  christos 	return p.mask;
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos int
    133  1.1  christos fedisableexcept(int mask)
    134  1.1  christos {
    135  1.1  christos 	struct alpha_fp_except_args p;
    136  1.1  christos 
    137  1.1  christos 	sysarch(ALPHA_FPGETMASK, &p);
    138  1.1  christos 	p.mask &= ~(mask & FE_ALL_EXCEPT);
    139  1.1  christos 	sysarch(ALPHA_FPSETMASK, &p);
    140  1.1  christos 	return p.mask;
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos int
    144  1.1  christos fegetexcept(void)
    145  1.1  christos {
    146  1.1  christos 	struct alpha_fp_except_args p;
    147  1.1  christos 
    148  1.1  christos 	sysarch(ALPHA_FPGETMASK, &p);
    149  1.1  christos 	return p.mask;
    150  1.1  christos }
    151