Print this page
11210 libm should be cstyle(1ONBLD) clean


   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 #pragma weak nearbyintl = __nearbyintl
  31 
  32 #include "libm.h"
  33 #include "fma.h"
  34 #include "fenv_inlines.h"
  35 
  36 #if defined(__sparc)
  37 
  38 static union {
  39         unsigned i;
  40         float f;
  41 } snan = { 0x7f800001 };
  42 
  43 long double
  44 __nearbyintl(long double x) {

  45         union {
  46                 unsigned i[4];
  47                 long double q;
  48         } xx;

  49         unsigned hx, sx, i, frac;
  50         unsigned int fsr;
  51         int rm, j;
  52         volatile float  dummy;
  53 
  54         xx.q = x;
  55         sx = xx.i[0] & 0x80000000;
  56         hx = xx.i[0] & ~0x80000000;
  57 
  58         /* handle trivial cases */
  59         if (hx >= 0x406f0000) {      /* x is nan, inf, or already integral */
  60                 /* check for signaling nan */
  61                 if ((hx > 0x7fff0000 || (hx == 0x7fff0000 &&
  62                         (xx.i[1] | xx.i[2] | xx.i[3]))) && !(hx & 0x8000)) {
  63                         dummy = snan.f;
  64                         dummy += snan.f;
  65                         xx.i[0] = sx | hx | 0x8000;
  66                 }

  67                 return (xx.q);
  68         } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0)     /* x is zero */
  69                 return (x);

  70 
  71         /* get the rounding mode */
  72         __fenv_getfsr32(&fsr);
  73         rm = fsr >> 30;
  74 
  75         /* flip the sense of directed roundings if x is negative */
  76         if (sx)
  77                 rm ^= rm >> 1;
  78 
  79         /* handle |x| < 1 */
  80         if (hx < 0x3fff0000) {
  81                 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 &&
  82                         ((hx & 0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))
  83                         xx.i[0] = sx | 0x3fff0000;
  84                 else
  85                         xx.i[0] = sx;

  86                 xx.i[1] = xx.i[2] = xx.i[3] = 0;
  87                 return (xx.q);
  88         }
  89 
  90         /* round x at the integer bit */
  91         j = 0x406f - (hx >> 16);

  92         if (j >= 96) {
  93                 i = 1 << (j - 96);
  94                 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));

  95                 if ((xx.i[1] & (i - 1)) | xx.i[2] | xx.i[3])
  96                         frac |= 1;

  97                 if (!frac)
  98                         return (x);

  99                 xx.i[1] = xx.i[2] = xx.i[3] = 0;
 100                 xx.i[0] &= ~(i - 1);

 101                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 102                         (frac == 0x80000000 && (xx.i[0] & i)))))
 103                         xx.i[0] += i;
 104         } else if (j >= 64) {
 105                 i = 1 << (j - 64);
 106                 frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));

 107                 if ((xx.i[2] & (i - 1)) | xx.i[3])
 108                         frac |= 1;

 109                 if (!frac)
 110                         return (x);

 111                 xx.i[2] = xx.i[3] = 0;
 112                 xx.i[1] &= ~(i - 1);

 113                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 114                         (frac == 0x80000000 && (xx.i[1] & i))))) {
 115                         xx.i[1] += i;

 116                         if (xx.i[1] == 0)
 117                                 xx.i[0]++;
 118                 }
 119         } else if (j >= 32) {
 120                 i = 1 << (j - 32);
 121                 frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));

 122                 if (xx.i[3] & (i - 1))
 123                         frac |= 1;

 124                 if (!frac)
 125                         return (x);

 126                 xx.i[3] = 0;
 127                 xx.i[2] &= ~(i - 1);

 128                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 129                         (frac == 0x80000000 && (xx.i[2] & i))))) {
 130                         xx.i[2] += i;

 131                         if (xx.i[2] == 0)
 132                                 if (++xx.i[1] == 0)
 133                                         xx.i[0]++;
 134                 }
 135         } else {
 136                 i = 1 << j;
 137                 frac = (xx.i[3] << 1) << (31 - j);

 138                 if (!frac)
 139                         return (x);

 140                 xx.i[3] &= ~(i - 1);

 141                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 142                         (frac == 0x80000000 && (xx.i[3] & i))))) {
 143                         xx.i[3] += i;

 144                         if (xx.i[3] == 0)
 145                                 if (++xx.i[2] == 0)
 146                                         if (++xx.i[1] == 0)
 147                                                 xx.i[0]++;
 148                 }
 149         }
 150 
 151         return (xx.q);
 152 }
 153 
 154 #elif defined(__x86)
 155 
 156 /* inline template */
 157 extern long double frndint(long double);
 158 
 159 long double
 160 __nearbyintl(long double x) {

 161         long double z;
 162         unsigned oldcwsw, cwsw;
 163 
 164         /* save the control and status words, mask the inexact exception */
 165         __fenv_getcwsw(&oldcwsw);
 166         cwsw = oldcwsw | 0x00200000;
 167         __fenv_setcwsw(&cwsw);
 168 
 169         z = frndint(x);
 170 
 171         /*
 172          * restore the control and status words, preserving all but the
 173          * inexact flag
 174          */
 175         __fenv_getcwsw(&cwsw);
 176         oldcwsw |= (cwsw & 0x1f);
 177         __fenv_setcwsw(&oldcwsw);
 178 
 179         return (z);
 180 }
 181 
 182 #else
 183 #error Unknown architecture
 184 #endif


   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 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak nearbyintl = __nearbyintl
  32 
  33 #include "libm.h"
  34 #include "fma.h"
  35 #include "fenv_inlines.h"
  36 
  37 #if defined(__sparc)

  38 static union {
  39         unsigned i;
  40         float f;
  41 } snan = { 0x7f800001 };

  42 long double
  43 __nearbyintl(long double x)
  44 {
  45         union {
  46                 unsigned i[4];
  47                 long double q;
  48         } xx;
  49 
  50         unsigned hx, sx, i, frac;
  51         unsigned int fsr;
  52         int rm, j;
  53         volatile float dummy;
  54 
  55         xx.q = x;
  56         sx = xx.i[0] & 0x80000000;
  57         hx = xx.i[0] & ~0x80000000;
  58 
  59         /* handle trivial cases */
  60         if (hx >= 0x406f0000) {              /* x is nan, inf, or already integral */
  61                 /* check for signaling nan */
  62                 if ((hx > 0x7fff0000 || (hx == 0x7fff0000 && (xx.i[1] |
  63                     xx.i[2] | xx.i[3]))) && !(hx & 0x8000)) {
  64                         dummy = snan.f;
  65                         dummy += snan.f;
  66                         xx.i[0] = sx | hx | 0x8000;
  67                 }
  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 
  74         /* get the rounding mode */
  75         __fenv_getfsr32(&fsr);
  76         rm = fsr >> 30;
  77 
  78         /* flip the sense of directed roundings if x is negative */
  79         if (sx)
  80                 rm ^= rm >> 1;
  81 
  82         /* handle |x| < 1 */
  83         if (hx < 0x3fff0000) {
  84                 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 && ((hx &
  85                     0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))
  86                         xx.i[0] = sx | 0x3fff0000;
  87                 else
  88                         xx.i[0] = sx;
  89 
  90                 xx.i[1] = xx.i[2] = xx.i[3] = 0;
  91                 return (xx.q);
  92         }
  93 
  94         /* round x at the integer bit */
  95         j = 0x406f - (hx >> 16);
  96 
  97         if (j >= 96) {
  98                 i = 1 << (j - 96);
  99                 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
 100 
 101                 if ((xx.i[1] & (i - 1)) | xx.i[2] | xx.i[3])
 102                         frac |= 1;
 103 
 104                 if (!frac)
 105                         return (x);
 106 
 107                 xx.i[1] = xx.i[2] = xx.i[3] = 0;
 108                 xx.i[0] &= ~(i - 1);
 109 
 110                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 111                     (frac == 0x80000000 && (xx.i[0] & i)))))
 112                         xx.i[0] += i;
 113         } else if (j >= 64) {
 114                 i = 1 << (j - 64);
 115                 frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
 116 
 117                 if ((xx.i[2] & (i - 1)) | xx.i[3])
 118                         frac |= 1;
 119 
 120                 if (!frac)
 121                         return (x);
 122 
 123                 xx.i[2] = xx.i[3] = 0;
 124                 xx.i[1] &= ~(i - 1);
 125 
 126                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 127                     (frac == 0x80000000 && (xx.i[1] & i))))) {
 128                         xx.i[1] += i;
 129 
 130                         if (xx.i[1] == 0)
 131                                 xx.i[0]++;
 132                 }
 133         } else if (j >= 32) {
 134                 i = 1 << (j - 32);
 135                 frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
 136 
 137                 if (xx.i[3] & (i - 1))
 138                         frac |= 1;
 139 
 140                 if (!frac)
 141                         return (x);
 142 
 143                 xx.i[3] = 0;
 144                 xx.i[2] &= ~(i - 1);
 145 
 146                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 147                     (frac == 0x80000000 && (xx.i[2] & i))))) {
 148                         xx.i[2] += i;
 149 
 150                         if (xx.i[2] == 0)
 151                                 if (++xx.i[1] == 0)
 152                                         xx.i[0]++;
 153                 }
 154         } else {
 155                 i = 1 << j;
 156                 frac = (xx.i[3] << 1) << (31 - j);
 157 
 158                 if (!frac)
 159                         return (x);
 160 
 161                 xx.i[3] &= ~(i - 1);
 162 
 163                 if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
 164                     (frac == 0x80000000 && (xx.i[3] & i))))) {
 165                         xx.i[3] += i;
 166 
 167                         if (xx.i[3] == 0)
 168                                 if (++xx.i[2] == 0)
 169                                         if (++xx.i[1] == 0)
 170                                                 xx.i[0]++;
 171                 }
 172         }
 173 
 174         return (xx.q);
 175 }

 176 #elif defined(__x86)

 177 /* inline template */
 178 extern long double frndint(long double);

 179 long double
 180 __nearbyintl(long double x)
 181 {
 182         long double z;
 183         unsigned oldcwsw, cwsw;
 184 
 185         /* save the control and status words, mask the inexact exception */
 186         __fenv_getcwsw(&oldcwsw);
 187         cwsw = oldcwsw | 0x00200000;
 188         __fenv_setcwsw(&cwsw);
 189 
 190         z = frndint(x);
 191 
 192         /*
 193          * restore the control and status words, preserving all but the
 194          * inexact flag
 195          */
 196         __fenv_getcwsw(&cwsw);
 197         oldcwsw |= (cwsw & 0x1f);
 198         __fenv_setcwsw(&oldcwsw);
 199 
 200         return (z);
 201 }

 202 #else
 203 #error Unknown architecture
 204 #endif