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 <stdint.h> |
26 | |
27 | static const double one = 1.0; |
28 | |
29 | double |
30 | __modf(double x, double *iptr) |
31 | { |
32 | int64_t i0; |
33 | int32_t j0; |
34 | EXTRACT_WORDS64(i0,x); |
35 | j0 = ((i0>>52)&0x7ff)-0x3ff; /* exponent of x */ |
36 | if(j0<52) { /* integer part in x */ |
37 | if(j0<0) { /* |x|<1 */ |
38 | /* *iptr = +-0 */ |
39 | INSERT_WORDS64(*iptr,i0&UINT64_C(0x8000000000000000)); |
40 | return x; |
41 | } else { |
42 | uint64_t i = UINT64_C(0x000fffffffffffff)>>j0; |
43 | if((i0&i)==0) { /* x is integral */ |
44 | *iptr = x; |
45 | /* return +-0 */ |
46 | INSERT_WORDS64(x,i0&UINT64_C(0x8000000000000000)); |
47 | return x; |
48 | } else { |
49 | INSERT_WORDS64(*iptr,i0&(~i)); |
50 | return x - *iptr; |
51 | } |
52 | } |
53 | } else { /* no fraction part */ |
54 | *iptr = x*one; |
55 | /* We must handle NaNs separately. */ |
56 | if (j0 == 0x400 && (i0 & UINT64_C(0xfffffffffffff))) |
57 | return x*one; |
58 | INSERT_WORDS64(x,i0&UINT64_C(0x8000000000000000)); /* return +-0 */ |
59 | return x; |
60 | } |
61 | } |
62 | weak_alias (__modf, modf) |
63 | #ifdef NO_LONG_DOUBLE |
64 | strong_alias (__modf, __modfl) |
65 | weak_alias (__modf, modfl) |
66 | #endif |
67 | |