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 lrintl = __lrintl 32 #endif 33 34 #include <sys/isa_defs.h> /* _ILP32 */ 35 #include "libm.h" 36 37 #if defined(_ILP32) 38 #if defined(__sparc) 39 40 #include "fma.h" 41 #include "fenv_inlines.h" 42 43 long 44 lrintl(long double x) { 45 union { 46 unsigned int i[4]; 47 long double q; 48 } xx; 49 union { 50 unsigned int i; 51 float f; 52 } tt; 53 unsigned int hx, sx, frac, l, fsr; 54 int rm, j; 55 volatile float dummy; 56 57 xx.q = x; 58 sx = xx.i[0] & 0x80000000; 59 hx = xx.i[0] & ~0x80000000; 60 61 /* handle trivial cases */ 62 if (hx > 0x401e0000) { /* |x| > 2^31 + ... or x is nan */ 63 /* convert an out-of-range float */ 64 tt.i = sx | 0x7f000000; 65 return ((long) tt.f); 66 } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) /* x is zero */ 67 return (0L); 68 69 /* get the rounding mode */ 70 __fenv_getfsr32(&fsr); 71 rm = fsr >> 30; 72 73 /* flip the sense of directed roundings if x is negative */ 74 if (sx) 75 rm ^= rm >> 1; 76 77 /* handle |x| < 1 */ 78 if (hx < 0x3fff0000) { 79 dummy = 1.0e30F; /* x is nonzero, so raise inexact */ 80 dummy += 1.0e-30F; 81 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 && 82 ((hx & 0xffff) | xx.i[1] | xx.i[2] | xx.i[3])))) 83 return (sx ? -1L : 1L); 84 return (0L); 85 } 86 87 /* extract the integer and fractional parts of x */ 88 j = 0x406f - (hx >> 16); /* 91 <= j <= 112 */ 89 xx.i[0] = 0x10000 | (xx.i[0] & 0xffff); 90 if (j >= 96) { /* 96 <= j <= 112 */ 91 l = xx.i[0] >> (j - 96); 92 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96)); 93 if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3]) 94 frac |= 1; 95 } else { /* 91 <= j <= 95 */ 96 l = (xx.i[0] << (96 - j)) | (xx.i[1] >> (j - 64)); 97 frac = (xx.i[1] << (96 - j)) | (xx.i[2] >> (j - 64)); 98 if ((xx.i[2] << (96 - j)) | xx.i[3]) 99 frac |= 1; 100 } 101 102 /* round */ 103 if (frac && (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000U || 104 (frac == 0x80000000 && (l & 1)))))) 105 l++; 106 107 /* check for result out of range (note that z is |x| at this point) */ 108 if (l > 0x80000000U || (l == 0x80000000U && !sx)) { 109 tt.i = sx | 0x7f000000; 110 return ((long) tt.f); 111 } 112 113 /* raise inexact if need be */ 114 if (frac) { 115 dummy = 1.0e30F; 116 dummy += 1.0e-30F; 117 } 118 119 /* negate result if need be */ 120 if (sx) 121 l = -l; 122 return ((long) l); 123 } 124 #elif defined(__x86) 125 long 126 lrintl(long double x) { 127 /* 128 * Note: The following code works on x86 (in the default rounding 129 * precision mode), but one ought to just use the fistpl instruction 130 * instead. 131 */ 132 union { 133 unsigned i[3]; 134 long double e; 135 } xx, yy; 136 int ex; 137 138 xx.e = x; 139 ex = xx.i[2] & 0x7fff; 140 if (ex < 0x403e) { /* |x| < 2^63 */ 141 /* add and subtract a power of two to round x to an integer */ 142 yy.i[2] = (xx.i[2] & 0x8000) | 0x403e; 143 yy.i[1] = 0x80000000; 144 yy.i[0] = 0; 145 x = (x + yy.e) - yy.e; 146 } 147 148 /* now x is nan, inf, or integral */ 149 return ((long) x); 150 } 151 #else 152 #error Unknown architecture 153 #endif /* defined(__sparc) || defined(__x86) */ 154 #else 155 #error Unsupported architecture 156 #endif /* defined(_ILP32) */