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 lroundl = __lroundl 32 #endif 33 34 #include <sys/isa_defs.h> /* _ILP32 */ 35 #include "libm.h" 36 37 #if defined(_ILP32) 38 #if defined(__sparc) 39 long 40 lroundl(long double x) { 41 union { 42 unsigned i[4]; 43 long double q; 44 } xx; 45 union { 46 unsigned i; 47 float f; 48 } tt; 49 unsigned hx, sx, frac, l; 50 int j; 51 52 xx.q = x; 53 sx = xx.i[0] & 0x80000000; 54 hx = xx.i[0] & ~0x80000000; 55 56 /* handle trivial cases */ 57 if (hx > 0x401e0000) { /* |x| > 2^31 + ... or x is nan */ 58 /* convert an out-of-range float */ 59 tt.i = sx | 0x7f000000; 60 return ((long) tt.f); 61 } 62 63 /* handle |x| < 1 */ 64 if (hx < 0x3fff0000) { 65 if (hx >= 0x3ffe0000) 66 return (sx ? -1L : 1L); 67 return (0L); 68 } 69 70 /* extract the integer and fractional parts of x */ 71 j = 0x406f - (hx >> 16); /* 91 <= j <= 112 */ 72 xx.i[0] = 0x10000 | (xx.i[0] & 0xffff); 73 if (j >= 96) { /* 96 <= j <= 112 */ 74 l = xx.i[0] >> (j - 96); 75 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96)); 76 if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3]) 77 frac |= 1; 78 } else { /* 91 <= j <= 95 */ 79 l = (xx.i[0] << (96 - j)) | (xx.i[1] >> (j - 64)); 80 frac = (xx.i[1] << (96 - j)) | (xx.i[2] >> (j - 64)); 81 if ((xx.i[2] << (96 - j)) | xx.i[3]) 82 frac |= 1; 83 } 84 85 /* round */ 86 if (frac >= 0x80000000U) 87 l++; 88 89 /* check for result out of range (note that z is |x| at this point) */ 90 if (l > 0x80000000U || (l == 0x80000000U && !sx)) { 91 tt.i = sx | 0x7f000000; 92 return ((long) tt.f); 93 } 94 95 /* negate result if need be */ 96 if (sx) 97 l = -l; 98 return ((long) l); 99 } 100 #elif defined(__x86) 101 long 102 lroundl(long double x) { 103 union { 104 unsigned i[3]; 105 long double e; 106 } xx; 107 int ex, sx, i; 108 109 xx.e = x; 110 ex = xx.i[2] & 0x7fff; 111 sx = xx.i[2] & 0x8000; 112 if (ex < 0x403e) { /* |x| < 2^63 */ 113 if (ex < 0x3fff) { /* |x| < 1 */ 114 if (ex >= 0x3ffe) 115 return (sx ? -1L : 1L); 116 return (0L); 117 } 118 119 /* round x at the integer bit */ 120 if (ex < 0x401e) { 121 i = 1 << (0x401d - ex); 122 xx.i[1] = (xx.i[1] + i) & ~(i | (i - 1)); 123 xx.i[0] = 0; 124 } else { 125 i = 1 << (0x403d - ex); 126 xx.i[0] += i; 127 if (xx.i[0] < i) 128 xx.i[1]++; 129 xx.i[0] &= ~(i | (i - 1)); 130 } 131 if (xx.i[1] == 0) { 132 xx.i[2] = sx | ++ex; 133 xx.i[1] = 0x80000000U; 134 } 135 } 136 137 /* now x is nan, inf, or integral */ 138 return ((long) xx.e); 139 } 140 #else 141 #error Unknown architecture 142 #endif /* defined(__sparc) || defined(__x86) */ 143 #else 144 #error Unsupported architecture 145 #endif /* defined(_ILP32) */