1*25c28e83SPiotr Jasiukajtis /* 2*25c28e83SPiotr Jasiukajtis * CDDL HEADER START 3*25c28e83SPiotr Jasiukajtis * 4*25c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 5*25c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 6*25c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 7*25c28e83SPiotr Jasiukajtis * 8*25c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*25c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 10*25c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 11*25c28e83SPiotr Jasiukajtis * and limitations under the License. 12*25c28e83SPiotr Jasiukajtis * 13*25c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 14*25c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*25c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 16*25c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 17*25c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 18*25c28e83SPiotr Jasiukajtis * 19*25c28e83SPiotr Jasiukajtis * CDDL HEADER END 20*25c28e83SPiotr Jasiukajtis */ 21*25c28e83SPiotr Jasiukajtis 22*25c28e83SPiotr Jasiukajtis /* 23*25c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*25c28e83SPiotr Jasiukajtis */ 25*25c28e83SPiotr Jasiukajtis /* 26*25c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 27*25c28e83SPiotr Jasiukajtis * Use is subject to license terms. 28*25c28e83SPiotr Jasiukajtis */ 29*25c28e83SPiotr Jasiukajtis 30*25c28e83SPiotr Jasiukajtis #pragma weak nearbyintf = __nearbyintf 31*25c28e83SPiotr Jasiukajtis 32*25c28e83SPiotr Jasiukajtis #include "libm.h" 33*25c28e83SPiotr Jasiukajtis #include <fenv.h> 34*25c28e83SPiotr Jasiukajtis 35*25c28e83SPiotr Jasiukajtis float 36*25c28e83SPiotr Jasiukajtis __nearbyintf(float x) { 37*25c28e83SPiotr Jasiukajtis union { 38*25c28e83SPiotr Jasiukajtis unsigned i; 39*25c28e83SPiotr Jasiukajtis float f; 40*25c28e83SPiotr Jasiukajtis } xx; 41*25c28e83SPiotr Jasiukajtis unsigned hx, sx, i, frac; 42*25c28e83SPiotr Jasiukajtis int rm; 43*25c28e83SPiotr Jasiukajtis 44*25c28e83SPiotr Jasiukajtis xx.f = x; 45*25c28e83SPiotr Jasiukajtis sx = xx.i & 0x80000000; 46*25c28e83SPiotr Jasiukajtis hx = xx.i & ~0x80000000; 47*25c28e83SPiotr Jasiukajtis 48*25c28e83SPiotr Jasiukajtis /* handle trivial cases */ 49*25c28e83SPiotr Jasiukajtis if (hx >= 0x4b000000) { /* x is nan, inf, or already integral */ 50*25c28e83SPiotr Jasiukajtis if (hx > 0x7f800000) /* x is nan */ 51*25c28e83SPiotr Jasiukajtis return (x * x); /* + -> * for Cheetah */ 52*25c28e83SPiotr Jasiukajtis return (x); 53*25c28e83SPiotr Jasiukajtis } else if (hx == 0) /* x is zero */ 54*25c28e83SPiotr Jasiukajtis return (x); 55*25c28e83SPiotr Jasiukajtis 56*25c28e83SPiotr Jasiukajtis /* get the rounding mode */ 57*25c28e83SPiotr Jasiukajtis rm = fegetround(); 58*25c28e83SPiotr Jasiukajtis 59*25c28e83SPiotr Jasiukajtis /* flip the sense of directed roundings if x is negative */ 60*25c28e83SPiotr Jasiukajtis if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD)) 61*25c28e83SPiotr Jasiukajtis rm = (FE_UPWARD + FE_DOWNWARD) - rm; 62*25c28e83SPiotr Jasiukajtis 63*25c28e83SPiotr Jasiukajtis /* handle |x| < 1 */ 64*25c28e83SPiotr Jasiukajtis if (hx < 0x3f800000) { 65*25c28e83SPiotr Jasiukajtis if (rm == FE_UPWARD || (rm == FE_TONEAREST && hx > 0x3f000000)) 66*25c28e83SPiotr Jasiukajtis xx.i = sx | 0x3f800000; 67*25c28e83SPiotr Jasiukajtis else 68*25c28e83SPiotr Jasiukajtis xx.i = sx; 69*25c28e83SPiotr Jasiukajtis return (xx.f); 70*25c28e83SPiotr Jasiukajtis } 71*25c28e83SPiotr Jasiukajtis 72*25c28e83SPiotr Jasiukajtis /* round x at the integer bit */ 73*25c28e83SPiotr Jasiukajtis i = 1 << (0x96 - (hx >> 23)); 74*25c28e83SPiotr Jasiukajtis frac = hx & (i - 1); 75*25c28e83SPiotr Jasiukajtis if (!frac) 76*25c28e83SPiotr Jasiukajtis return (x); 77*25c28e83SPiotr Jasiukajtis 78*25c28e83SPiotr Jasiukajtis hx &= ~(i - 1); 79*25c28e83SPiotr Jasiukajtis if (rm == FE_UPWARD || (rm == FE_TONEAREST && (frac > (i >> 1) || 80*25c28e83SPiotr Jasiukajtis ((frac == (i >> 1)) && (hx & i))))) 81*25c28e83SPiotr Jasiukajtis xx.i = sx | (hx + i); 82*25c28e83SPiotr Jasiukajtis else 83*25c28e83SPiotr Jasiukajtis xx.i = sx | hx; 84*25c28e83SPiotr Jasiukajtis return (xx.f); 85*25c28e83SPiotr Jasiukajtis } 86*25c28e83SPiotr Jasiukajtis 87*25c28e83SPiotr Jasiukajtis #if 0 88*25c28e83SPiotr Jasiukajtis 89*25c28e83SPiotr Jasiukajtis /* 90*25c28e83SPiotr Jasiukajtis * Alternate implementations for SPARC, x86, using fp ops. These may 91*25c28e83SPiotr Jasiukajtis * be faster depending on how expensive saving and restoring the fp 92*25c28e83SPiotr Jasiukajtis * modes and status flags is. 93*25c28e83SPiotr Jasiukajtis */ 94*25c28e83SPiotr Jasiukajtis 95*25c28e83SPiotr Jasiukajtis #include "libm.h" 96*25c28e83SPiotr Jasiukajtis #include "fma.h" 97*25c28e83SPiotr Jasiukajtis 98*25c28e83SPiotr Jasiukajtis #if defined(__sparc) 99*25c28e83SPiotr Jasiukajtis 100*25c28e83SPiotr Jasiukajtis float 101*25c28e83SPiotr Jasiukajtis __nearbyintf(float x) { 102*25c28e83SPiotr Jasiukajtis union { 103*25c28e83SPiotr Jasiukajtis unsigned i; 104*25c28e83SPiotr Jasiukajtis float f; 105*25c28e83SPiotr Jasiukajtis } xx, yy; 106*25c28e83SPiotr Jasiukajtis float z; 107*25c28e83SPiotr Jasiukajtis unsigned hx, sx, fsr, oldfsr; 108*25c28e83SPiotr Jasiukajtis int rm; 109*25c28e83SPiotr Jasiukajtis 110*25c28e83SPiotr Jasiukajtis xx.f = x; 111*25c28e83SPiotr Jasiukajtis sx = xx.i & 0x80000000; 112*25c28e83SPiotr Jasiukajtis hx = xx.i & ~0x80000000; 113*25c28e83SPiotr Jasiukajtis 114*25c28e83SPiotr Jasiukajtis /* handle trivial cases */ 115*25c28e83SPiotr Jasiukajtis if (hx >= 0x4b000000) /* x is nan, inf, or already integral */ 116*25c28e83SPiotr Jasiukajtis return (x + 0.0f); 117*25c28e83SPiotr Jasiukajtis else if (hx == 0) /* x is zero */ 118*25c28e83SPiotr Jasiukajtis return (x); 119*25c28e83SPiotr Jasiukajtis 120*25c28e83SPiotr Jasiukajtis /* save the fsr */ 121*25c28e83SPiotr Jasiukajtis __fenv_getfsr(&oldfsr); 122*25c28e83SPiotr Jasiukajtis 123*25c28e83SPiotr Jasiukajtis /* handle |x| < 1 */ 124*25c28e83SPiotr Jasiukajtis if (hx < 0x3f800000) { 125*25c28e83SPiotr Jasiukajtis /* flip the sense of directed roundings if x is negative */ 126*25c28e83SPiotr Jasiukajtis rm = oldfsr >> 30; 127*25c28e83SPiotr Jasiukajtis if (sx) 128*25c28e83SPiotr Jasiukajtis rm ^= rm >> 1; 129*25c28e83SPiotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && hx > 0x3f000000)) 130*25c28e83SPiotr Jasiukajtis xx.i = sx | 0x3f800000; 131*25c28e83SPiotr Jasiukajtis else 132*25c28e83SPiotr Jasiukajtis xx.i = sx; 133*25c28e83SPiotr Jasiukajtis return (xx.f); 134*25c28e83SPiotr Jasiukajtis } 135*25c28e83SPiotr Jasiukajtis 136*25c28e83SPiotr Jasiukajtis /* clear the inexact trap */ 137*25c28e83SPiotr Jasiukajtis fsr = oldfsr & ~FSR_NXM; 138*25c28e83SPiotr Jasiukajtis __fenv_setfsr(&fsr); 139*25c28e83SPiotr Jasiukajtis 140*25c28e83SPiotr Jasiukajtis /* round x at the integer bit */ 141*25c28e83SPiotr Jasiukajtis yy.i = sx | 0x4b000000; 142*25c28e83SPiotr Jasiukajtis z = (x + yy.f) - yy.f; 143*25c28e83SPiotr Jasiukajtis 144*25c28e83SPiotr Jasiukajtis /* restore the old fsr */ 145*25c28e83SPiotr Jasiukajtis __fenv_setfsr(&oldfsr); 146*25c28e83SPiotr Jasiukajtis 147*25c28e83SPiotr Jasiukajtis return (z); 148*25c28e83SPiotr Jasiukajtis } 149*25c28e83SPiotr Jasiukajtis 150*25c28e83SPiotr Jasiukajtis #elif defined(__x86) 151*25c28e83SPiotr Jasiukajtis 152*25c28e83SPiotr Jasiukajtis /* inline template */ 153*25c28e83SPiotr Jasiukajtis extern long double frndint(long double); 154*25c28e83SPiotr Jasiukajtis 155*25c28e83SPiotr Jasiukajtis float 156*25c28e83SPiotr Jasiukajtis __nearbyintf(float x) { 157*25c28e83SPiotr Jasiukajtis long double z; 158*25c28e83SPiotr Jasiukajtis unsigned oldcwsw, cwsw; 159*25c28e83SPiotr Jasiukajtis 160*25c28e83SPiotr Jasiukajtis /* save the control and status words, mask the inexact exception */ 161*25c28e83SPiotr Jasiukajtis __fenv_getcwsw(&oldcwsw); 162*25c28e83SPiotr Jasiukajtis cwsw = oldcwsw | 0x00200000; 163*25c28e83SPiotr Jasiukajtis __fenv_setcwsw(&cwsw); 164*25c28e83SPiotr Jasiukajtis 165*25c28e83SPiotr Jasiukajtis z = frndint((long double) x); 166*25c28e83SPiotr Jasiukajtis 167*25c28e83SPiotr Jasiukajtis /* 168*25c28e83SPiotr Jasiukajtis * restore the control and status words, preserving all but the 169*25c28e83SPiotr Jasiukajtis * inexact flag 170*25c28e83SPiotr Jasiukajtis */ 171*25c28e83SPiotr Jasiukajtis __fenv_getcwsw(&cwsw); 172*25c28e83SPiotr Jasiukajtis oldcwsw |= (cwsw & 0x1f); 173*25c28e83SPiotr Jasiukajtis __fenv_setcwsw(&oldcwsw); 174*25c28e83SPiotr Jasiukajtis 175*25c28e83SPiotr Jasiukajtis /* note: the value of z is representable in single precision */ 176*25c28e83SPiotr Jasiukajtis return (z); 177*25c28e83SPiotr Jasiukajtis } 178*25c28e83SPiotr Jasiukajtis 179*25c28e83SPiotr Jasiukajtis #else 180*25c28e83SPiotr Jasiukajtis #error Unknown architecture 181*25c28e83SPiotr Jasiukajtis #endif 182*25c28e83SPiotr Jasiukajtis 183*25c28e83SPiotr Jasiukajtis #endif 184