fpbits.h revision 9899:0392ef94d766
1/*
2 * Copyright (c) 2013, Andreas Sandberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
15 *
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.
28 */
29
30#ifndef _FPBITS_H
31#define _FPBITS_H
32
33#include <stdint.h>
34
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
42
43#define FP80_EXP_SPECIAL 0x7fff
44#define FP80_FRAC_SNAN  0x3fffffffffffffffULL
45#define FP80_FRAC_QNAN  0x7fffffffffffffffULL
46#define FP80_FRAC_QNANI 0x4000000000000000ULL
47
48
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
55
56#define FP64_EXP_SPECIAL 0x7ff
57#define FP64_FRAC_SNAN  0x0007ffffffffffffULL
58#define FP64_FRAC_QNAN  0x000fffffffffffffULL
59#define FP64_FRAC_QNANI 0x0008000000000000ULL
60
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)
65
66#define BUILD_FP64(sign, frac, exp)                             \
67    { .bits = BUILD_IFP64(sign, frac, exp) }
68
69#define BUILD_FP80_SE(sign, exp)                                \
70    ((sign) ? FP80_SIGN_BIT : 0) |                              \
71    ((exp) & FP80_EXP_MASK)
72
73#define BUILD_FP80_FI(frac, exp)                                \
74    ((exp) ? FP80_INT_BIT : 0) |                                \
75    ((frac) & FP80_FRAC_MASK)
76
77#define BUILD_FP80(sign, frac, exp)                             \
78    {                                                           \
79        .repr.se = BUILD_FP80_SE(sign, exp),                    \
80        .repr.fi = BUILD_FP80_FI(frac, exp)                     \
81    }
82
83#define FP80_FRAC(fp80)                                         \
84    (fp80.repr.fi & FP80_FRAC_MASK)
85
86#define FP80_EXP(fp80)                                          \
87    (fp80.repr.se & FP80_EXP_MASK)
88
89#define FP64_FRAC(fp64)                                         \
90    (fp64.bits & FP64_FRAC_MASK)
91
92#define FP64_EXP(fp80)                                          \
93    ((fp64.bits & FP64_EXP_MASK) >> FP64_EXP_SHIFT)
94
95#endif
96