| 1 | /* Rewritten for 64-bit machines by Ulrich Drepper <drepper@gmail.com>. */ |
| 2 | /* |
| 3 | * ==================================================== |
| 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 5 | * |
| 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 7 | * Permission to use, copy, modify, and distribute this |
| 8 | * software is freely granted, provided that this notice |
| 9 | * is preserved. |
| 10 | * ==================================================== |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * modf(double x, double *iptr) |
| 15 | * return fraction part of x, and return x's integral part in *iptr. |
| 16 | * Method: |
| 17 | * Bit twiddling. |
| 18 | * |
| 19 | * Exception: |
| 20 | * No exception. |
| 21 | */ |
| 22 | |
| 23 | #include <math.h> |
| 24 | #include <math_private.h> |
| 25 | #include <libm-alias-double.h> |
| 26 | #include <stdint.h> |
| 27 | |
| 28 | static const double one = 1.0; |
| 29 | |
| 30 | double |
| 31 | __modf(double x, double *iptr) |
| 32 | { |
| 33 | int64_t i0; |
| 34 | int32_t j0; |
| 35 | EXTRACT_WORDS64(i0,x); |
| 36 | j0 = ((i0>>52)&0x7ff)-0x3ff; /* exponent of x */ |
| 37 | if(j0<52) { /* integer part in x */ |
| 38 | if(j0<0) { /* |x|<1 */ |
| 39 | /* *iptr = +-0 */ |
| 40 | INSERT_WORDS64(*iptr,i0&UINT64_C(0x8000000000000000)); |
| 41 | return x; |
| 42 | } else { |
| 43 | uint64_t i = UINT64_C(0x000fffffffffffff)>>j0; |
| 44 | if((i0&i)==0) { /* x is integral */ |
| 45 | *iptr = x; |
| 46 | /* return +-0 */ |
| 47 | INSERT_WORDS64(x,i0&UINT64_C(0x8000000000000000)); |
| 48 | return x; |
| 49 | } else { |
| 50 | INSERT_WORDS64(*iptr,i0&(~i)); |
| 51 | return x - *iptr; |
| 52 | } |
| 53 | } |
| 54 | } else { /* no fraction part */ |
| 55 | *iptr = x*one; |
| 56 | /* We must handle NaNs separately. */ |
| 57 | if (j0 == 0x400 && (i0 & UINT64_C(0xfffffffffffff))) |
| 58 | return x*one; |
| 59 | INSERT_WORDS64(x,i0&UINT64_C(0x8000000000000000)); /* return +-0 */ |
| 60 | return x; |
| 61 | } |
| 62 | } |
| 63 | #ifndef __modf |
| 64 | libm_alias_double (__modf, modf) |
| 65 | #endif |
| 66 | |