]> git.saurik.com Git - apple/libc.git/blob - gdtoa/_ldbl_util.c
Libc-498.1.7.tar.gz
[apple/libc.git] / gdtoa / _ldbl_util.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http:www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/cdefs.h>
25 #include <stdint.h>
26 #include <strings.h>
27 #include <float.h>
28 #include <math.h>
29 #include <alloca.h>
30
31 #include "fpmath.h"
32
33 #define BITS64 64
34 #define DBL_BIAS (DBL_MAX_EXP - 1)
35 #define DBL_SUBEXP (-DBL_BIAS + 1)
36 #define LL_BITS (8 * sizeof(int64_t))
37 #define LL_HIGHBIT (1LL << 63)
38
39 __private_extern__ void
40 _ldbl2array32dd(union IEEEl2bits u, uint32_t *a)
41 {
42 int bit, shift, highbit, dexp;
43 uint64_t a64[2];
44 int64_t t64;
45
46 bzero(a64, sizeof(a64));
47
48 switch (__fpclassifyd(u.d[0])) {
49 case FP_NORMAL:
50 a64[1] = (1LL << (LDBL_MANT_DIG - BITS64 - 1));
51 /* drop through */
52 case FP_SUBNORMAL:
53 a64[1] |= ((uint64_t)u.bits.manh >> (BITS64 - LDBL_MANL_SIZE));
54 a64[0] = ((uint64_t)u.bits.manh << LDBL_MANL_SIZE);
55 break;
56 default:
57 goto done;
58 }
59
60 dexp = (int)u.bits.exp - (int)u.bits.exp2;
61 /*
62 * if the tail double is so small to not fit in LDBL_MANT_DIG bits,
63 * then just skip it.
64 */
65 if (dexp >= LDBL_MANT_DIG)
66 goto done;
67
68 switch (__fpclassifyd(u.d[1])) {
69 case FP_NORMAL:
70 bit = LDBL_MANT_DIG - dexp - 1;
71 t64 = (1LL << bit);
72 break;
73 case FP_SUBNORMAL:
74 bit = LDBL_MANT_DIG - (int)u.bits.exp;
75 t64 = 0;
76 break;
77 default:
78 goto done;
79 }
80 shift = LDBL_MANL_SIZE - bit - 1;
81 if (shift >= 0)
82 t64 |= (u.bits.manl >> shift);
83 else
84 t64 |= (u.bits.manl << (-shift));
85 highbit = ((a64[0] & LL_HIGHBIT) != 0);
86 if (u.bits.sign == u.bits.sign2) {
87 a64[0] += t64;
88 if (highbit && !(a64[0] & LL_HIGHBIT)) /* carry */
89 a64[1]++;
90 } else {
91 a64[0] -= t64;
92 if (!highbit && (a64[0] & LL_HIGHBIT)) /* borrow */
93 a64[1]--;
94 }
95
96 done:
97 a[0] = (uint32_t)a64[0];
98 a[1] = (uint32_t)(a64[0] >> 32);
99 a[2] = (uint32_t)a64[1];
100 a[3] = (uint32_t)(a64[1] >> 32);
101 }