2 * Copyright (c) 2013, Andreas Sandberg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 #define FP80_FRAC_BITS 63
36 #define FP80_INT_BIT 0x8000000000000000ULL
37 #define FP80_QNAN_BIT 0x4000000000000000ULL
38 #define FP80_FRAC_MASK 0x7fffffffffffffffULL
39 #define FP80_EXP_MASK 0x7fff
40 #define FP80_SIGN_BIT 0x8000
41 #define FP80_EXP_BIAS 0x3fff
43 #define FP80_EXP_SPECIAL 0x7fff
44 #define FP80_FRAC_SNAN 0x3fffffffffffffffULL
45 #define FP80_FRAC_QNAN 0x7fffffffffffffffULL
46 #define FP80_FRAC_QNANI 0x4000000000000000ULL
49 #define FP64_EXP_SHIFT 52
50 #define FP64_FRAC_BITS 52
51 #define FP64_SIGN_BIT 0x8000000000000000ULL
52 #define FP64_EXP_MASK 0x7ff0000000000000ULL
53 #define FP64_FRAC_MASK 0x000fffffffffffffULL
54 #define FP64_EXP_BIAS 0x3ff
56 #define FP64_EXP_SPECIAL 0x7ff
57 #define FP64_FRAC_SNAN 0x0007ffffffffffffULL
58 #define FP64_FRAC_QNAN 0x000fffffffffffffULL
59 #define FP64_FRAC_QNANI 0x0008000000000000ULL
61 #define BUILD_IFP64(sign, frac, exp) \
62 ((sign) ? FP64_SIGN_BIT : 0) | \
63 (((uint64_t)(exp) << FP64_EXP_SHIFT) & FP64_EXP_MASK) | \
64 ((frac) & FP64_FRAC_MASK)
66 #define BUILD_FP64(sign, frac, exp) \
67 { .bits = BUILD_IFP64(sign, frac, exp) }
70 build_fp64(int sign
, uint64_t frac
, int exp
)
72 const fp64_t f
= BUILD_FP64(sign
, frac
, exp
);
77 #define BUILD_FP80_SE(sign, exp) \
78 ((sign) ? FP80_SIGN_BIT : 0) | \
79 ((exp) & FP80_EXP_MASK)
81 #define BUILD_FP80_FI(frac, exp) \
82 ((exp) ? FP80_INT_BIT : 0) | \
83 ((frac) & FP80_FRAC_MASK)
85 #define BUILD_FP80(sign, frac, exp) \
87 .repr.se = BUILD_FP80_SE(sign, exp), \
88 .repr.fi = BUILD_FP80_FI(frac, exp) \
92 build_fp80(int sign
, uint64_t frac
, int exp
)
94 const fp80_t f
= BUILD_FP80(sign
, frac
, exp
);
99 #define FP80_FRAC(fp80) \
100 (fp80.repr.fi & FP80_FRAC_MASK)
102 #define FP80_EXP(fp80) \
103 (fp80.repr.se & FP80_EXP_MASK)
105 #define FP64_FRAC(fp64) \
106 (fp64.bits & FP64_FRAC_MASK)
108 #define FP64_EXP(fp80) \
109 ((fp64.bits & FP64_EXP_MASK) >> FP64_EXP_SHIFT)