15b2ba9d3SPiotr Jasiukajtis /*
25b2ba9d3SPiotr Jasiukajtis * CDDL HEADER START
35b2ba9d3SPiotr Jasiukajtis *
45b2ba9d3SPiotr Jasiukajtis * The contents of this file are subject to the terms of the
55b2ba9d3SPiotr Jasiukajtis * Common Development and Distribution License (the "License").
65b2ba9d3SPiotr Jasiukajtis * You may not use this file except in compliance with the License.
75b2ba9d3SPiotr Jasiukajtis *
85b2ba9d3SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95b2ba9d3SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
105b2ba9d3SPiotr Jasiukajtis * See the License for the specific language governing permissions
115b2ba9d3SPiotr Jasiukajtis * and limitations under the License.
125b2ba9d3SPiotr Jasiukajtis *
135b2ba9d3SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
145b2ba9d3SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155b2ba9d3SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
165b2ba9d3SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
175b2ba9d3SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
185b2ba9d3SPiotr Jasiukajtis *
195b2ba9d3SPiotr Jasiukajtis * CDDL HEADER END
205b2ba9d3SPiotr Jasiukajtis */
215b2ba9d3SPiotr Jasiukajtis
225b2ba9d3SPiotr Jasiukajtis /*
235b2ba9d3SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
245b2ba9d3SPiotr Jasiukajtis */
255b2ba9d3SPiotr Jasiukajtis /*
265b2ba9d3SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
275b2ba9d3SPiotr Jasiukajtis * Use is subject to license terms.
285b2ba9d3SPiotr Jasiukajtis */
295b2ba9d3SPiotr Jasiukajtis
30*a9d3dcd5SRichard Lowe #pragma weak __llroundl = llroundl
315b2ba9d3SPiotr Jasiukajtis #if defined(__sparcv9) || defined(__amd64)
32*a9d3dcd5SRichard Lowe #pragma weak lroundl = llroundl
33*a9d3dcd5SRichard Lowe #pragma weak __lroundl = llroundl
345b2ba9d3SPiotr Jasiukajtis #endif
355b2ba9d3SPiotr Jasiukajtis
365b2ba9d3SPiotr Jasiukajtis #include "libm.h"
375b2ba9d3SPiotr Jasiukajtis
385b2ba9d3SPiotr Jasiukajtis #if defined(__sparc)
395b2ba9d3SPiotr Jasiukajtis long long
llroundl(long double x)405b2ba9d3SPiotr Jasiukajtis llroundl(long double x) {
415b2ba9d3SPiotr Jasiukajtis union {
425b2ba9d3SPiotr Jasiukajtis unsigned i[4];
435b2ba9d3SPiotr Jasiukajtis long double q;
445b2ba9d3SPiotr Jasiukajtis } xx;
455b2ba9d3SPiotr Jasiukajtis union {
465b2ba9d3SPiotr Jasiukajtis unsigned i[2];
475b2ba9d3SPiotr Jasiukajtis long long l;
485b2ba9d3SPiotr Jasiukajtis } zz;
495b2ba9d3SPiotr Jasiukajtis union {
505b2ba9d3SPiotr Jasiukajtis unsigned i;
515b2ba9d3SPiotr Jasiukajtis float f;
525b2ba9d3SPiotr Jasiukajtis } tt;
535b2ba9d3SPiotr Jasiukajtis unsigned hx, sx, frac;
545b2ba9d3SPiotr Jasiukajtis int j;
555b2ba9d3SPiotr Jasiukajtis
565b2ba9d3SPiotr Jasiukajtis xx.q = x;
575b2ba9d3SPiotr Jasiukajtis sx = xx.i[0] & 0x80000000;
585b2ba9d3SPiotr Jasiukajtis hx = xx.i[0] & ~0x80000000;
595b2ba9d3SPiotr Jasiukajtis
605b2ba9d3SPiotr Jasiukajtis /* handle trivial cases */
615b2ba9d3SPiotr Jasiukajtis if (hx > 0x403e0000) { /* |x| > 2^63 + ... or x is nan */
625b2ba9d3SPiotr Jasiukajtis /* convert an out-of-range float */
635b2ba9d3SPiotr Jasiukajtis tt.i = sx | 0x7f000000;
645b2ba9d3SPiotr Jasiukajtis return ((long long) tt.f);
655b2ba9d3SPiotr Jasiukajtis }
665b2ba9d3SPiotr Jasiukajtis
675b2ba9d3SPiotr Jasiukajtis /* handle |x| < 1 */
685b2ba9d3SPiotr Jasiukajtis if (hx < 0x3fff0000) {
695b2ba9d3SPiotr Jasiukajtis if (hx >= 0x3ffe0000)
705b2ba9d3SPiotr Jasiukajtis return (sx ? -1LL : 1LL);
715b2ba9d3SPiotr Jasiukajtis return (0LL);
725b2ba9d3SPiotr Jasiukajtis }
735b2ba9d3SPiotr Jasiukajtis
745b2ba9d3SPiotr Jasiukajtis /* extract the integer and fractional parts of x */
755b2ba9d3SPiotr Jasiukajtis j = 0x406f - (hx >> 16);
765b2ba9d3SPiotr Jasiukajtis xx.i[0] = 0x10000 | (xx.i[0] & 0xffff);
775b2ba9d3SPiotr Jasiukajtis if (j >= 96) {
785b2ba9d3SPiotr Jasiukajtis zz.i[0] = 0;
795b2ba9d3SPiotr Jasiukajtis zz.i[1] = xx.i[0] >> (j - 96);
805b2ba9d3SPiotr Jasiukajtis frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
815b2ba9d3SPiotr Jasiukajtis if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3])
825b2ba9d3SPiotr Jasiukajtis frac |= 1;
835b2ba9d3SPiotr Jasiukajtis } else if (j >= 64) {
845b2ba9d3SPiotr Jasiukajtis zz.i[0] = xx.i[0] >> (j - 64);
855b2ba9d3SPiotr Jasiukajtis zz.i[1] = ((xx.i[0] << 1) << (95 - j)) | (xx.i[1] >> (j - 64));
865b2ba9d3SPiotr Jasiukajtis frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
875b2ba9d3SPiotr Jasiukajtis if (((xx.i[2] << 1) << (95 - j)) | xx.i[3])
885b2ba9d3SPiotr Jasiukajtis frac |= 1;
895b2ba9d3SPiotr Jasiukajtis } else {
905b2ba9d3SPiotr Jasiukajtis zz.i[0] = ((xx.i[0] << 1) << (63 - j)) | (xx.i[1] >> (j - 32));
915b2ba9d3SPiotr Jasiukajtis zz.i[1] = ((xx.i[1] << 1) << (63 - j)) | (xx.i[2] >> (j - 32));
925b2ba9d3SPiotr Jasiukajtis frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
935b2ba9d3SPiotr Jasiukajtis if ((xx.i[3] << 1) << (63 - j))
945b2ba9d3SPiotr Jasiukajtis frac |= 1;
955b2ba9d3SPiotr Jasiukajtis }
965b2ba9d3SPiotr Jasiukajtis
975b2ba9d3SPiotr Jasiukajtis /* round */
985b2ba9d3SPiotr Jasiukajtis if (frac >= 0x80000000u) {
995b2ba9d3SPiotr Jasiukajtis if (++zz.i[1] == 0)
1005b2ba9d3SPiotr Jasiukajtis zz.i[0]++;
1015b2ba9d3SPiotr Jasiukajtis }
1025b2ba9d3SPiotr Jasiukajtis
1035b2ba9d3SPiotr Jasiukajtis /* check for result out of range (note that z is |x| at this point) */
1045b2ba9d3SPiotr Jasiukajtis if (zz.i[0] > 0x80000000u || (zz.i[0] == 0x80000000 && (zz.i[1] ||
1055b2ba9d3SPiotr Jasiukajtis !sx))) {
1065b2ba9d3SPiotr Jasiukajtis tt.i = sx | 0x7f000000;
1075b2ba9d3SPiotr Jasiukajtis return ((long long) tt.f);
1085b2ba9d3SPiotr Jasiukajtis }
1095b2ba9d3SPiotr Jasiukajtis
1105b2ba9d3SPiotr Jasiukajtis /* negate result if need be */
1115b2ba9d3SPiotr Jasiukajtis if (sx) {
1125b2ba9d3SPiotr Jasiukajtis zz.i[0] = ~zz.i[0];
1135b2ba9d3SPiotr Jasiukajtis zz.i[1] = -zz.i[1];
1145b2ba9d3SPiotr Jasiukajtis if (zz.i[1] == 0)
1155b2ba9d3SPiotr Jasiukajtis zz.i[0]++;
1165b2ba9d3SPiotr Jasiukajtis }
1175b2ba9d3SPiotr Jasiukajtis
1185b2ba9d3SPiotr Jasiukajtis return (zz.l);
1195b2ba9d3SPiotr Jasiukajtis }
1205b2ba9d3SPiotr Jasiukajtis #elif defined(__x86)
1215b2ba9d3SPiotr Jasiukajtis long long
llroundl(long double x)1225b2ba9d3SPiotr Jasiukajtis llroundl(long double x) {
1235b2ba9d3SPiotr Jasiukajtis union {
1245b2ba9d3SPiotr Jasiukajtis unsigned i[3];
1255b2ba9d3SPiotr Jasiukajtis long double e;
1265b2ba9d3SPiotr Jasiukajtis } xx;
1275b2ba9d3SPiotr Jasiukajtis int ex, sx, i;
1285b2ba9d3SPiotr Jasiukajtis
1295b2ba9d3SPiotr Jasiukajtis xx.e = x;
1305b2ba9d3SPiotr Jasiukajtis ex = xx.i[2] & 0x7fff;
1315b2ba9d3SPiotr Jasiukajtis sx = xx.i[2] & 0x8000;
1325b2ba9d3SPiotr Jasiukajtis
1335b2ba9d3SPiotr Jasiukajtis if (ex < 0x403e) { /* |x| < 2^63 */
1345b2ba9d3SPiotr Jasiukajtis /* handle |x| < 1 */
1355b2ba9d3SPiotr Jasiukajtis if (ex < 0x3fff) {
1365b2ba9d3SPiotr Jasiukajtis if (ex >= 0x3ffe)
1375b2ba9d3SPiotr Jasiukajtis return (sx ? -1LL : 1LL);
1385b2ba9d3SPiotr Jasiukajtis return (0LL);
1395b2ba9d3SPiotr Jasiukajtis }
1405b2ba9d3SPiotr Jasiukajtis
1415b2ba9d3SPiotr Jasiukajtis /* round x at the integer bit */
1425b2ba9d3SPiotr Jasiukajtis if (ex < 0x401e) {
1435b2ba9d3SPiotr Jasiukajtis i = 1 << (0x401d - ex);
1445b2ba9d3SPiotr Jasiukajtis xx.i[1] = (xx.i[1] + i) & ~(i | (i - 1));
1455b2ba9d3SPiotr Jasiukajtis xx.i[0] = 0;
1465b2ba9d3SPiotr Jasiukajtis } else {
1475b2ba9d3SPiotr Jasiukajtis i = 1 << (0x403d - ex);
1485b2ba9d3SPiotr Jasiukajtis xx.i[0] += i;
1495b2ba9d3SPiotr Jasiukajtis if (xx.i[0] < i)
1505b2ba9d3SPiotr Jasiukajtis xx.i[1]++;
1515b2ba9d3SPiotr Jasiukajtis xx.i[0] &= ~(i | (i - 1));
1525b2ba9d3SPiotr Jasiukajtis }
1535b2ba9d3SPiotr Jasiukajtis if (xx.i[1] == 0) {
1545b2ba9d3SPiotr Jasiukajtis xx.i[2] = sx | ++ex;
1555b2ba9d3SPiotr Jasiukajtis xx.i[1] = 0x80000000U;
1565b2ba9d3SPiotr Jasiukajtis }
1575b2ba9d3SPiotr Jasiukajtis }
1585b2ba9d3SPiotr Jasiukajtis
1595b2ba9d3SPiotr Jasiukajtis /* now x is nan, inf, or integral */
1605b2ba9d3SPiotr Jasiukajtis return ((long long) xx.e);
1615b2ba9d3SPiotr Jasiukajtis }
1625b2ba9d3SPiotr Jasiukajtis #else
1635b2ba9d3SPiotr Jasiukajtis #error Unknown architecture
1645b2ba9d3SPiotr Jasiukajtis #endif
165