4 * The code in this source file is derived from release 2a of the SoftFloat
5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6 * some later contributions) are provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
13 * Any future contributions to this file after December 1st 2014 will be
14 * taken to be licensed under the Softfloat-2a license unless specifically
15 * indicated otherwise.
18 static void partsN(return_nan)(FloatPartsN *a, float_status *s)
21 case float_class_snan:
22 float_raise(float_flag_invalid, s);
23 if (s->default_nan_mode) {
24 parts_default_nan(a, s);
26 parts_silence_nan(a, s);
29 case float_class_qnan:
30 if (s->default_nan_mode) {
31 parts_default_nan(a, s);
35 g_assert_not_reached();
39 static FloatPartsN *partsN(pick_nan)(FloatPartsN *a, FloatPartsN *b,
42 if (is_snan(a->cls) || is_snan(b->cls)) {
43 float_raise(float_flag_invalid, s);
46 if (s->default_nan_mode) {
47 parts_default_nan(a, s);
49 int cmp = frac_cmp(a, b);
51 cmp = a->sign < b->sign;
54 if (pickNaN(a->cls, b->cls, cmp > 0, s)) {
57 if (is_snan(a->cls)) {
58 parts_silence_nan(a, s);
64 static FloatPartsN *partsN(pick_nan_muladd)(FloatPartsN *a, FloatPartsN *b,
65 FloatPartsN *c, float_status *s,
66 int ab_mask, int abc_mask)
70 if (unlikely(abc_mask & float_cmask_snan)) {
71 float_raise(float_flag_invalid, s);
74 which = pickNaNMulAdd(a->cls, b->cls, c->cls,
75 ab_mask == float_cmask_infzero, s);
77 if (s->default_nan_mode || which == 3) {
79 * Note that this check is after pickNaNMulAdd so that function
80 * has an opportunity to set the Invalid flag for infzero.
82 parts_default_nan(a, s);
96 g_assert_not_reached();
98 if (is_snan(a->cls)) {
99 parts_silence_nan(a, s);
105 * Canonicalize the FloatParts structure. Determine the class,
106 * unbias the exponent, and normalize the fraction.
108 static void partsN(canonicalize)(FloatPartsN *p, float_status *status,
111 if (unlikely(p->exp == 0)) {
112 if (likely(frac_eqz(p))) {
113 p->cls = float_class_zero;
114 } else if (status->flush_inputs_to_zero) {
115 float_raise(float_flag_input_denormal, status);
116 p->cls = float_class_zero;
119 int shift = frac_normalize(p);
120 p->cls = float_class_normal;
121 p->exp = fmt->frac_shift - fmt->exp_bias - shift + 1;
123 } else if (likely(p->exp < fmt->exp_max) || fmt->arm_althp) {
124 p->cls = float_class_normal;
125 p->exp -= fmt->exp_bias;
126 frac_shl(p, fmt->frac_shift);
127 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
128 } else if (likely(frac_eqz(p))) {
129 p->cls = float_class_inf;
131 frac_shl(p, fmt->frac_shift);
132 p->cls = (parts_is_snan_frac(p->frac_hi, status)
133 ? float_class_snan : float_class_qnan);
138 * Round and uncanonicalize a floating-point number by parts. There
139 * are FRAC_SHIFT bits that may require rounding at the bottom of the
140 * fraction; these bits will be removed. The exponent will be biased
141 * by EXP_BIAS and must be bounded by [EXP_MAX-1, 0].
143 static void partsN(uncanon)(FloatPartsN *p, float_status *s,
146 const int exp_max = fmt->exp_max;
147 const int frac_shift = fmt->frac_shift;
148 const uint64_t frac_lsb = fmt->frac_lsb;
149 const uint64_t frac_lsbm1 = fmt->frac_lsbm1;
150 const uint64_t round_mask = fmt->round_mask;
151 const uint64_t roundeven_mask = fmt->roundeven_mask;
156 if (unlikely(p->cls != float_class_normal)) {
158 case float_class_zero:
162 case float_class_inf:
163 g_assert(!fmt->arm_althp);
164 p->exp = fmt->exp_max;
167 case float_class_qnan:
168 case float_class_snan:
169 g_assert(!fmt->arm_althp);
170 p->exp = fmt->exp_max;
171 frac_shr(p, fmt->frac_shift);
176 g_assert_not_reached();
179 switch (s->float_rounding_mode) {
180 case float_round_nearest_even:
181 overflow_norm = false;
182 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1 ? frac_lsbm1 : 0);
184 case float_round_ties_away:
185 overflow_norm = false;
188 case float_round_to_zero:
189 overflow_norm = true;
193 inc = p->sign ? 0 : round_mask;
194 overflow_norm = p->sign;
196 case float_round_down:
197 inc = p->sign ? round_mask : 0;
198 overflow_norm = !p->sign;
200 case float_round_to_odd:
201 overflow_norm = true;
202 inc = p->frac_lo & frac_lsb ? 0 : round_mask;
205 g_assert_not_reached();
208 exp = p->exp + fmt->exp_bias;
209 if (likely(exp > 0)) {
210 if (p->frac_lo & round_mask) {
211 flags |= float_flag_inexact;
212 if (frac_addi(p, p, inc)) {
214 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
218 frac_shr(p, frac_shift);
220 if (fmt->arm_althp) {
221 /* ARM Alt HP eschews Inf and NaN for a wider exponent. */
222 if (unlikely(exp > exp_max)) {
223 /* Overflow. Return the maximum normal. */
224 flags = float_flag_invalid;
228 } else if (unlikely(exp >= exp_max)) {
229 flags |= float_flag_overflow | float_flag_inexact;
234 p->cls = float_class_inf;
239 } else if (s->flush_to_zero) {
240 flags |= float_flag_output_denormal;
241 p->cls = float_class_zero;
245 bool is_tiny = s->tininess_before_rounding || exp < 0;
249 is_tiny = !frac_addi(&discard, p, inc);
252 frac_shrjam(p, 1 - exp);
254 if (p->frac_lo & round_mask) {
255 /* Need to recompute round-to-even/round-to-odd. */
256 switch (s->float_rounding_mode) {
257 case float_round_nearest_even:
258 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1
261 case float_round_to_odd:
262 inc = p->frac_lo & frac_lsb ? 0 : round_mask;
267 flags |= float_flag_inexact;
268 frac_addi(p, p, inc);
271 exp = (p->frac_hi & DECOMPOSED_IMPLICIT_BIT) != 0;
272 frac_shr(p, frac_shift);
274 if (is_tiny && (flags & float_flag_inexact)) {
275 flags |= float_flag_underflow;
277 if (exp == 0 && frac_eqz(p)) {
278 p->cls = float_class_zero;
282 float_raise(flags, s);
286 * Returns the result of adding or subtracting the values of the
287 * floating-point values `a' and `b'. The operation is performed
288 * according to the IEC/IEEE Standard for Binary Floating-Point
291 static FloatPartsN *partsN(addsub)(FloatPartsN *a, FloatPartsN *b,
292 float_status *s, bool subtract)
294 bool b_sign = b->sign ^ subtract;
295 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
297 if (a->sign != b_sign) {
299 if (likely(ab_mask == float_cmask_normal)) {
300 if (parts_sub_normal(a, b)) {
303 /* Subtract was exact, fall through to set sign. */
304 ab_mask = float_cmask_zero;
307 if (ab_mask == float_cmask_zero) {
308 a->sign = s->float_rounding_mode == float_round_down;
312 if (unlikely(ab_mask & float_cmask_anynan)) {
316 if (ab_mask & float_cmask_inf) {
317 if (a->cls != float_class_inf) {
321 if (b->cls != float_class_inf) {
326 float_raise(float_flag_invalid, s);
327 parts_default_nan(a, s);
332 if (likely(ab_mask == float_cmask_normal)) {
333 parts_add_normal(a, b);
337 if (ab_mask == float_cmask_zero) {
341 if (unlikely(ab_mask & float_cmask_anynan)) {
345 if (ab_mask & float_cmask_inf) {
346 a->cls = float_class_inf;
351 if (b->cls == float_class_zero) {
352 g_assert(a->cls == float_class_normal);
356 g_assert(a->cls == float_class_zero);
357 g_assert(b->cls == float_class_normal);
363 return parts_pick_nan(a, b, s);