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 *

--- 22 unchanged lines hidden (view full) ---

31#include "fpbits.h"
32
33#include <assert.h>
34#include <stdint.h>
35
36#include <stdio.h>
37
38typedef union {
39 union {
40 uint64_t bits;
41 double value;
42 };
39 uint64_t bits;
40 double value;
41} fp64_t;
42
43
44const fp80_t fp80_pinf = BUILD_FP80(0, 0, FP80_EXP_SPECIAL);
45const fp80_t fp80_ninf = BUILD_FP80(1, 0, FP80_EXP_SPECIAL);
46const fp80_t fp80_qnan = BUILD_FP80(0, FP80_FRAC_QNAN, FP80_EXP_SPECIAL);
47const fp80_t fp80_qnani = BUILD_FP80(1, FP80_FRAC_QNANI, FP80_EXP_SPECIAL);
48const fp80_t fp80_snan = BUILD_FP80(0, FP80_FRAC_SNAN, FP80_EXP_SPECIAL);

--- 18 unchanged lines hidden (view full) ---

67 const fp64_t f = BUILD_FP64(sign, frac, exp);
68
69 return f.value;
70}
71
72int
73fp80_sgn(fp80_t fp80)
74{
77 return (fp80.u.repr.se & FP80_SIGN_BIT) ? -1 : 1;
75 return (fp80.repr.se & FP80_SIGN_BIT) ? -1 : 1;
76}
77
78int
79fp80_isspecial(fp80_t fp80)
80{
81 const int exp = FP80_EXP(fp80);
82
83 return exp == FP80_EXP_SPECIAL;

--- 14 unchanged lines hidden (view full) ---

98 const uint64_t frac = FP80_FRAC(fp80);
99
100 return fp80_isspecial(fp80) && (frac & FP80_QNAN_BIT);
101}
102
103int
104fp80_isqnani(fp80_t fp80)
105{
108 const uint64_t frac_low = fp80.u.repr.fi & (FP80_FRAC_MASK >> 1);
106 const uint64_t frac_low = fp80.repr.fi & (FP80_FRAC_MASK >> 1);
107
110 return fp80_isqnan(fp80) && (fp80.u.repr.se & FP80_SIGN_BIT) && !frac_low;
108 return fp80_isqnan(fp80) && (fp80.repr.se & FP80_SIGN_BIT) && !frac_low;
109}
110
111int
112fp80_issnan(fp80_t fp80)
113{
114 const uint64_t frac = FP80_FRAC(fp80);
115
116 return fp80_isspecial(fp80) && !(frac & FP80_QNAN_BIT) && frac;

--- 9 unchanged lines hidden (view full) ---

126fp80_isnan(fp80_t fp80)
127{
128 return fp80_issnan(fp80) || fp80_isqnan(fp80) ? fp80_sgn(fp80) : 0;
129}
130
131int
132fp80_iszero(fp80_t fp80)
133{
136 return fp80.u.repr.fi == 0 && FP80_EXP(fp80) == 0 ? fp80_sgn(fp80) : 0;
134 return fp80.repr.fi == 0 && FP80_EXP(fp80) == 0 ? fp80_sgn(fp80) : 0;
135}
136
137int
138fp80_isnormal(fp80_t fp80)
139{
140 return FP80_EXP(fp80) != 0 && !fp80_isspecial(fp80) ?
141 fp80_sgn(fp80) : 0;
142}

--- 19 unchanged lines hidden (view full) ---

162 assert(fp80_isfinite(fp80));
163 return FP_NORMAL;
164 }
165}
166
167double
168fp80_cvtd(fp80_t fp80)
169{
172 const int sign = fp80.u.repr.se & FP80_SIGN_BIT;
170 const int sign = fp80.repr.se & FP80_SIGN_BIT;
171
172 if (!fp80_isspecial(fp80)) {
175 const uint64_t frac = fp80.u.repr.fi;
173 const uint64_t frac = fp80.repr.fi;
174 const int unb_exp = FP80_EXP(fp80) - FP80_EXP_BIAS;
175 const int fp64_exp = unb_exp + FP64_EXP_BIAS;
176 const uint64_t fp64_frac = frac >> (FP80_FRAC_BITS - FP64_FRAC_BITS);
177
178 if (fp64_exp > 0 && fp64_exp < FP64_EXP_SPECIAL) {
179 /* These numbers fall in the range of what we can express
180 * as normals */
181 return build_fp64(sign, fp64_frac, fp64_exp);

--- 53 unchanged lines hidden (view full) ---

235 return fp80;
236 }
237}
238
239void
240fp80_debug_dump(FILE *fout, fp80_t fp80)
241{
242 fprintf(fout, "sgn: %i, int: %i, frac: 0x%llx, exp: 0x%x (%i)\n",
245 fp80_sgn(fp80), !!(fp80.u.repr.fi & FP80_INT_BIT), FP80_FRAC(fp80),
243 fp80_sgn(fp80), !!(fp80.repr.fi & FP80_INT_BIT), FP80_FRAC(fp80),
244 FP80_EXP(fp80), FP80_EXP(fp80) - FP80_EXP_BIAS);
245}