1 | /* |
2 | * ==================================================== |
3 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
4 | * |
5 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
6 | * Permission to use, copy, modify, and distribute this |
7 | * software is freely granted, provided that this notice |
8 | * is preserved. |
9 | * ==================================================== |
10 | */ |
11 | |
12 | /* |
13 | * modf(double x, double *iptr) |
14 | * return fraction part of x, and return x's integral part in *iptr. |
15 | * Method: |
16 | * Bit twiddling. |
17 | * |
18 | * Exception: |
19 | * No exception. |
20 | */ |
21 | |
22 | #include <math.h> |
23 | #include <math_private.h> |
24 | #include <libm-alias-double.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 | #ifndef __modf |
63 | libm_alias_double (__modf, modf) |
64 | #endif |
65 | |