Deleted Added
sdiff udiff text old ( 9888:68d6b600d51f ) new ( 9899:0392ef94d766 )
full compact
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 };
43} fp64_t;
44
45
46const fp80_t fp80_pinf = BUILD_FP80(0, 0, FP80_EXP_SPECIAL);
47const fp80_t fp80_ninf = BUILD_FP80(1, 0, FP80_EXP_SPECIAL);
48const fp80_t fp80_qnan = BUILD_FP80(0, FP80_FRAC_QNAN, FP80_EXP_SPECIAL);
49const fp80_t fp80_qnani = BUILD_FP80(1, FP80_FRAC_QNANI, FP80_EXP_SPECIAL);
50const fp80_t fp80_snan = BUILD_FP80(0, FP80_FRAC_SNAN, FP80_EXP_SPECIAL);

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

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

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

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

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

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

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

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

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

237 return fp80;
238 }
239}
240
241void
242fp80_debug_dump(FILE *fout, fp80_t fp80)
243{
244 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),
246 FP80_EXP(fp80), FP80_EXP(fp80) - FP80_EXP_BIAS);
247}