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