fpbits.h revision 11708:0bb9093e4bbd
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 69static inline fp64_t 70build_fp64(int sign, uint64_t frac, int exp) 71{ 72 const fp64_t f = BUILD_FP64(sign, frac, exp); 73 74 return f; 75} 76 77#define BUILD_FP80_SE(sign, exp) \ 78 ((sign) ? FP80_SIGN_BIT : 0) | \ 79 ((exp) & FP80_EXP_MASK) 80 81#define BUILD_FP80_FI(frac, exp) \ 82 ((exp) ? FP80_INT_BIT : 0) | \ 83 ((frac) & FP80_FRAC_MASK) 84 85#define BUILD_FP80(sign, frac, exp) \ 86 { \ 87 .repr.pad = { 0 }, \ 88 .repr.se = BUILD_FP80_SE(sign, exp), \ 89 .repr.fi = BUILD_FP80_FI(frac, exp) \ 90 } 91 92static inline fp80_t 93build_fp80(int sign, uint64_t frac, int exp) 94{ 95 const fp80_t f = BUILD_FP80(sign, frac, exp); 96 97 return f; 98} 99 100#define FP80_FRAC(fp80) \ 101 (fp80.repr.fi & FP80_FRAC_MASK) 102 103#define FP80_EXP(fp80) \ 104 (fp80.repr.se & FP80_EXP_MASK) 105 106#define FP64_FRAC(fp64) \ 107 (fp64.bits & FP64_FRAC_MASK) 108 109#define FP64_EXP(fp80) \ 110 ((fp64.bits & FP64_EXP_MASK) >> FP64_EXP_SHIFT) 111 112 113#endif 114