125c28e83SPiotr Jasiukajtis /* 225c28e83SPiotr Jasiukajtis * CDDL HEADER START 325c28e83SPiotr Jasiukajtis * 425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 725c28e83SPiotr Jasiukajtis * 825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 1125c28e83SPiotr Jasiukajtis * and limitations under the License. 1225c28e83SPiotr Jasiukajtis * 1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 1825c28e83SPiotr Jasiukajtis * 1925c28e83SPiotr Jasiukajtis * CDDL HEADER END 2025c28e83SPiotr Jasiukajtis */ 2125c28e83SPiotr Jasiukajtis 2225c28e83SPiotr Jasiukajtis /* 2325c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 2425c28e83SPiotr Jasiukajtis */ 2525c28e83SPiotr Jasiukajtis /* 2625c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 2725c28e83SPiotr Jasiukajtis * Use is subject to license terms. 2825c28e83SPiotr Jasiukajtis */ 2925c28e83SPiotr Jasiukajtis 30*ddc0e0b5SRichard Lowe #pragma weak __lroundl = lroundl 3125c28e83SPiotr Jasiukajtis 3225c28e83SPiotr Jasiukajtis #include <sys/isa_defs.h> /* _ILP32 */ 3325c28e83SPiotr Jasiukajtis #include "libm.h" 3425c28e83SPiotr Jasiukajtis 3525c28e83SPiotr Jasiukajtis #if defined(_ILP32) 3625c28e83SPiotr Jasiukajtis #if defined(__sparc) 3725c28e83SPiotr Jasiukajtis long 3825c28e83SPiotr Jasiukajtis lroundl(long double x) { 3925c28e83SPiotr Jasiukajtis union { 4025c28e83SPiotr Jasiukajtis unsigned i[4]; 4125c28e83SPiotr Jasiukajtis long double q; 4225c28e83SPiotr Jasiukajtis } xx; 4325c28e83SPiotr Jasiukajtis union { 4425c28e83SPiotr Jasiukajtis unsigned i; 4525c28e83SPiotr Jasiukajtis float f; 4625c28e83SPiotr Jasiukajtis } tt; 4725c28e83SPiotr Jasiukajtis unsigned hx, sx, frac, l; 4825c28e83SPiotr Jasiukajtis int j; 4925c28e83SPiotr Jasiukajtis 5025c28e83SPiotr Jasiukajtis xx.q = x; 5125c28e83SPiotr Jasiukajtis sx = xx.i[0] & 0x80000000; 5225c28e83SPiotr Jasiukajtis hx = xx.i[0] & ~0x80000000; 5325c28e83SPiotr Jasiukajtis 5425c28e83SPiotr Jasiukajtis /* handle trivial cases */ 5525c28e83SPiotr Jasiukajtis if (hx > 0x401e0000) { /* |x| > 2^31 + ... or x is nan */ 5625c28e83SPiotr Jasiukajtis /* convert an out-of-range float */ 5725c28e83SPiotr Jasiukajtis tt.i = sx | 0x7f000000; 5825c28e83SPiotr Jasiukajtis return ((long) tt.f); 5925c28e83SPiotr Jasiukajtis } 6025c28e83SPiotr Jasiukajtis 6125c28e83SPiotr Jasiukajtis /* handle |x| < 1 */ 6225c28e83SPiotr Jasiukajtis if (hx < 0x3fff0000) { 6325c28e83SPiotr Jasiukajtis if (hx >= 0x3ffe0000) 6425c28e83SPiotr Jasiukajtis return (sx ? -1L : 1L); 6525c28e83SPiotr Jasiukajtis return (0L); 6625c28e83SPiotr Jasiukajtis } 6725c28e83SPiotr Jasiukajtis 6825c28e83SPiotr Jasiukajtis /* extract the integer and fractional parts of x */ 6925c28e83SPiotr Jasiukajtis j = 0x406f - (hx >> 16); /* 91 <= j <= 112 */ 7025c28e83SPiotr Jasiukajtis xx.i[0] = 0x10000 | (xx.i[0] & 0xffff); 7125c28e83SPiotr Jasiukajtis if (j >= 96) { /* 96 <= j <= 112 */ 7225c28e83SPiotr Jasiukajtis l = xx.i[0] >> (j - 96); 7325c28e83SPiotr Jasiukajtis frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96)); 7425c28e83SPiotr Jasiukajtis if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3]) 7525c28e83SPiotr Jasiukajtis frac |= 1; 7625c28e83SPiotr Jasiukajtis } else { /* 91 <= j <= 95 */ 7725c28e83SPiotr Jasiukajtis l = (xx.i[0] << (96 - j)) | (xx.i[1] >> (j - 64)); 7825c28e83SPiotr Jasiukajtis frac = (xx.i[1] << (96 - j)) | (xx.i[2] >> (j - 64)); 7925c28e83SPiotr Jasiukajtis if ((xx.i[2] << (96 - j)) | xx.i[3]) 8025c28e83SPiotr Jasiukajtis frac |= 1; 8125c28e83SPiotr Jasiukajtis } 8225c28e83SPiotr Jasiukajtis 8325c28e83SPiotr Jasiukajtis /* round */ 8425c28e83SPiotr Jasiukajtis if (frac >= 0x80000000U) 8525c28e83SPiotr Jasiukajtis l++; 8625c28e83SPiotr Jasiukajtis 8725c28e83SPiotr Jasiukajtis /* check for result out of range (note that z is |x| at this point) */ 8825c28e83SPiotr Jasiukajtis if (l > 0x80000000U || (l == 0x80000000U && !sx)) { 8925c28e83SPiotr Jasiukajtis tt.i = sx | 0x7f000000; 9025c28e83SPiotr Jasiukajtis return ((long) tt.f); 9125c28e83SPiotr Jasiukajtis } 9225c28e83SPiotr Jasiukajtis 9325c28e83SPiotr Jasiukajtis /* negate result if need be */ 9425c28e83SPiotr Jasiukajtis if (sx) 9525c28e83SPiotr Jasiukajtis l = -l; 9625c28e83SPiotr Jasiukajtis return ((long) l); 9725c28e83SPiotr Jasiukajtis } 9825c28e83SPiotr Jasiukajtis #elif defined(__x86) 9925c28e83SPiotr Jasiukajtis long 10025c28e83SPiotr Jasiukajtis lroundl(long double x) { 10125c28e83SPiotr Jasiukajtis union { 10225c28e83SPiotr Jasiukajtis unsigned i[3]; 10325c28e83SPiotr Jasiukajtis long double e; 10425c28e83SPiotr Jasiukajtis } xx; 10525c28e83SPiotr Jasiukajtis int ex, sx, i; 10625c28e83SPiotr Jasiukajtis 10725c28e83SPiotr Jasiukajtis xx.e = x; 10825c28e83SPiotr Jasiukajtis ex = xx.i[2] & 0x7fff; 10925c28e83SPiotr Jasiukajtis sx = xx.i[2] & 0x8000; 11025c28e83SPiotr Jasiukajtis if (ex < 0x403e) { /* |x| < 2^63 */ 11125c28e83SPiotr Jasiukajtis if (ex < 0x3fff) { /* |x| < 1 */ 11225c28e83SPiotr Jasiukajtis if (ex >= 0x3ffe) 11325c28e83SPiotr Jasiukajtis return (sx ? -1L : 1L); 11425c28e83SPiotr Jasiukajtis return (0L); 11525c28e83SPiotr Jasiukajtis } 11625c28e83SPiotr Jasiukajtis 11725c28e83SPiotr Jasiukajtis /* round x at the integer bit */ 11825c28e83SPiotr Jasiukajtis if (ex < 0x401e) { 11925c28e83SPiotr Jasiukajtis i = 1 << (0x401d - ex); 12025c28e83SPiotr Jasiukajtis xx.i[1] = (xx.i[1] + i) & ~(i | (i - 1)); 12125c28e83SPiotr Jasiukajtis xx.i[0] = 0; 12225c28e83SPiotr Jasiukajtis } else { 12325c28e83SPiotr Jasiukajtis i = 1 << (0x403d - ex); 12425c28e83SPiotr Jasiukajtis xx.i[0] += i; 12525c28e83SPiotr Jasiukajtis if (xx.i[0] < i) 12625c28e83SPiotr Jasiukajtis xx.i[1]++; 12725c28e83SPiotr Jasiukajtis xx.i[0] &= ~(i | (i - 1)); 12825c28e83SPiotr Jasiukajtis } 12925c28e83SPiotr Jasiukajtis if (xx.i[1] == 0) { 13025c28e83SPiotr Jasiukajtis xx.i[2] = sx | ++ex; 13125c28e83SPiotr Jasiukajtis xx.i[1] = 0x80000000U; 13225c28e83SPiotr Jasiukajtis } 13325c28e83SPiotr Jasiukajtis } 13425c28e83SPiotr Jasiukajtis 13525c28e83SPiotr Jasiukajtis /* now x is nan, inf, or integral */ 13625c28e83SPiotr Jasiukajtis return ((long) xx.e); 13725c28e83SPiotr Jasiukajtis } 13825c28e83SPiotr Jasiukajtis #else 13925c28e83SPiotr Jasiukajtis #error Unknown architecture 14025c28e83SPiotr Jasiukajtis #endif /* defined(__sparc) || defined(__x86) */ 14125c28e83SPiotr Jasiukajtis #else 14225c28e83SPiotr Jasiukajtis #error Unsupported architecture 14325c28e83SPiotr Jasiukajtis #endif /* defined(_ILP32) */ 144