]> git.lizzy.rs Git - rust.git/blob - src/librustc_apfloat/tests/ieee.rs
Rollup merge of #58075 - asettouf:master, r=varkor
[rust.git] / src / librustc_apfloat / tests / ieee.rs
1 use rustc_apfloat::{Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
2 use rustc_apfloat::{Float, FloatConvert, ParseError, Round, Status};
3 use rustc_apfloat::ieee::{Half, Single, Double, Quad, X87DoubleExtended};
4 use rustc_apfloat::unpack;
5
6 trait SingleExt {
7     fn from_f32(input: f32) -> Self;
8     fn to_f32(self) -> f32;
9 }
10
11 impl SingleExt for Single {
12     fn from_f32(input: f32) -> Self {
13         Self::from_bits(input.to_bits() as u128)
14     }
15
16     fn to_f32(self) -> f32 {
17         f32::from_bits(self.to_bits() as u32)
18     }
19 }
20
21 trait DoubleExt {
22     fn from_f64(input: f64) -> Self;
23     fn to_f64(self) -> f64;
24 }
25
26 impl DoubleExt for Double {
27     fn from_f64(input: f64) -> Self {
28         Self::from_bits(input.to_bits() as u128)
29     }
30
31     fn to_f64(self) -> f64 {
32         f64::from_bits(self.to_bits() as u64)
33     }
34 }
35
36 #[test]
37 fn is_signaling() {
38     // We test qNaN, -qNaN, +sNaN, -sNaN with and without payloads.
39     let payload = 4;
40     assert!(!Single::qnan(None).is_signaling());
41     assert!(!(-Single::qnan(None)).is_signaling());
42     assert!(!Single::qnan(Some(payload)).is_signaling());
43     assert!(!(-Single::qnan(Some(payload))).is_signaling());
44     assert!(Single::snan(None).is_signaling());
45     assert!((-Single::snan(None)).is_signaling());
46     assert!(Single::snan(Some(payload)).is_signaling());
47     assert!((-Single::snan(Some(payload))).is_signaling());
48 }
49
50 #[test]
51 fn next() {
52     // 1. Test Special Cases Values.
53     //
54     // Test all special values for nextUp and nextDown perscribed by IEEE-754R
55     // 2008. These are:
56     //   1. +inf
57     //   2. -inf
58     //   3. largest
59     //   4. -largest
60     //   5. smallest
61     //   6. -smallest
62     //   7. qNaN
63     //   8. sNaN
64     //   9. +0
65     //   10. -0
66
67     let mut status;
68
69     // nextUp(+inf) = +inf.
70     let test = unpack!(status=, Quad::INFINITY.next_up());
71     let expected = Quad::INFINITY;
72     assert_eq!(status, Status::OK);
73     assert!(test.is_infinite());
74     assert!(!test.is_negative());
75     assert!(test.bitwise_eq(expected));
76
77     // nextDown(+inf) = -nextUp(-inf) = -(-largest) = largest
78     let test = unpack!(status=, Quad::INFINITY.next_down());
79     let expected = Quad::largest();
80     assert_eq!(status, Status::OK);
81     assert!(!test.is_negative());
82     assert!(test.bitwise_eq(expected));
83
84     // nextUp(-inf) = -largest
85     let test = unpack!(status=, (-Quad::INFINITY).next_up());
86     let expected = -Quad::largest();
87     assert_eq!(status, Status::OK);
88     assert!(test.is_negative());
89     assert!(test.bitwise_eq(expected));
90
91     // nextDown(-inf) = -nextUp(+inf) = -(+inf) = -inf.
92     let test = unpack!(status=, (-Quad::INFINITY).next_down());
93     let expected = -Quad::INFINITY;
94     assert_eq!(status, Status::OK);
95     assert!(test.is_infinite() && test.is_negative());
96     assert!(test.bitwise_eq(expected));
97
98     // nextUp(largest) = +inf
99     let test = unpack!(status=, Quad::largest().next_up());
100     let expected = Quad::INFINITY;
101     assert_eq!(status, Status::OK);
102     assert!(test.is_infinite() && !test.is_negative());
103     assert!(test.bitwise_eq(expected));
104
105     // nextDown(largest) = -nextUp(-largest)
106     //                        = -(-largest + inc)
107     //                        = largest - inc.
108     let test = unpack!(status=, Quad::largest().next_down());
109     let expected = "0x1.fffffffffffffffffffffffffffep+16383"
110         .parse::<Quad>()
111         .unwrap();
112     assert_eq!(status, Status::OK);
113     assert!(!test.is_infinite() && !test.is_negative());
114     assert!(test.bitwise_eq(expected));
115
116     // nextUp(-largest) = -largest + inc.
117     let test = unpack!(status=, (-Quad::largest()).next_up());
118     let expected = "-0x1.fffffffffffffffffffffffffffep+16383"
119         .parse::<Quad>()
120         .unwrap();
121     assert_eq!(status, Status::OK);
122     assert!(test.bitwise_eq(expected));
123
124     // nextDown(-largest) = -nextUp(largest) = -(inf) = -inf.
125     let test = unpack!(status=, (-Quad::largest()).next_down());
126     let expected = -Quad::INFINITY;
127     assert_eq!(status, Status::OK);
128     assert!(test.is_infinite() && test.is_negative());
129     assert!(test.bitwise_eq(expected));
130
131     // nextUp(smallest) = smallest + inc.
132     let test = unpack!(status=, "0x0.0000000000000000000000000001p-16382"
133         .parse::<Quad>()
134         .unwrap()
135         .next_up());
136     let expected = "0x0.0000000000000000000000000002p-16382"
137         .parse::<Quad>()
138         .unwrap();
139     assert_eq!(status, Status::OK);
140     assert!(test.bitwise_eq(expected));
141
142     // nextDown(smallest) = -nextUp(-smallest) = -(-0) = +0.
143     let test = unpack!(status=, "0x0.0000000000000000000000000001p-16382"
144         .parse::<Quad>()
145         .unwrap()
146         .next_down());
147     let expected = Quad::ZERO;
148     assert_eq!(status, Status::OK);
149     assert!(test.is_pos_zero());
150     assert!(test.bitwise_eq(expected));
151
152     // nextUp(-smallest) = -0.
153     let test = unpack!(status=, "-0x0.0000000000000000000000000001p-16382"
154         .parse::<Quad>()
155         .unwrap()
156         .next_up());
157     let expected = -Quad::ZERO;
158     assert_eq!(status, Status::OK);
159     assert!(test.is_neg_zero());
160     assert!(test.bitwise_eq(expected));
161
162     // nextDown(-smallest) = -nextUp(smallest) = -smallest - inc.
163     let test = unpack!(status=, "-0x0.0000000000000000000000000001p-16382"
164         .parse::<Quad>()
165         .unwrap()
166         .next_down());
167     let expected = "-0x0.0000000000000000000000000002p-16382"
168         .parse::<Quad>()
169         .unwrap();
170     assert_eq!(status, Status::OK);
171     assert!(test.bitwise_eq(expected));
172
173     // nextUp(qNaN) = qNaN
174     let test = unpack!(status=, Quad::qnan(None).next_up());
175     let expected = Quad::qnan(None);
176     assert_eq!(status, Status::OK);
177     assert!(test.bitwise_eq(expected));
178
179     // nextDown(qNaN) = qNaN
180     let test = unpack!(status=, Quad::qnan(None).next_down());
181     let expected = Quad::qnan(None);
182     assert_eq!(status, Status::OK);
183     assert!(test.bitwise_eq(expected));
184
185     // nextUp(sNaN) = qNaN
186     let test = unpack!(status=, Quad::snan(None).next_up());
187     let expected = Quad::qnan(None);
188     assert_eq!(status, Status::INVALID_OP);
189     assert!(test.bitwise_eq(expected));
190
191     // nextDown(sNaN) = qNaN
192     let test = unpack!(status=, Quad::snan(None).next_down());
193     let expected = Quad::qnan(None);
194     assert_eq!(status, Status::INVALID_OP);
195     assert!(test.bitwise_eq(expected));
196
197     // nextUp(+0) = +smallest
198     let test = unpack!(status=, Quad::ZERO.next_up());
199     let expected = Quad::SMALLEST;
200     assert_eq!(status, Status::OK);
201     assert!(test.bitwise_eq(expected));
202
203     // nextDown(+0) = -nextUp(-0) = -smallest
204     let test = unpack!(status=, Quad::ZERO.next_down());
205     let expected = -Quad::SMALLEST;
206     assert_eq!(status, Status::OK);
207     assert!(test.bitwise_eq(expected));
208
209     // nextUp(-0) = +smallest
210     let test = unpack!(status=, (-Quad::ZERO).next_up());
211     let expected = Quad::SMALLEST;
212     assert_eq!(status, Status::OK);
213     assert!(test.bitwise_eq(expected));
214
215     // nextDown(-0) = -nextUp(0) = -smallest
216     let test = unpack!(status=, (-Quad::ZERO).next_down());
217     let expected = -Quad::SMALLEST;
218     assert_eq!(status, Status::OK);
219     assert!(test.bitwise_eq(expected));
220
221     // 2. Binade Boundary Tests.
222
223     // 2a. Test denormal <-> normal binade boundaries.
224     //     * nextUp(+Largest Denormal) -> +Smallest Normal.
225     //     * nextDown(-Largest Denormal) -> -Smallest Normal.
226     //     * nextUp(-Smallest Normal) -> -Largest Denormal.
227     //     * nextDown(+Smallest Normal) -> +Largest Denormal.
228
229     // nextUp(+Largest Denormal) -> +Smallest Normal.
230     let test = unpack!(status=, "0x0.ffffffffffffffffffffffffffffp-16382"
231         .parse::<Quad>()
232         .unwrap()
233         .next_up());
234     let expected = "0x1.0000000000000000000000000000p-16382"
235         .parse::<Quad>()
236         .unwrap();
237     assert_eq!(status, Status::OK);
238     assert!(!test.is_denormal());
239     assert!(test.bitwise_eq(expected));
240
241     // nextDown(-Largest Denormal) -> -Smallest Normal.
242     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffffffffp-16382"
243         .parse::<Quad>()
244         .unwrap()
245         .next_down());
246     let expected = "-0x1.0000000000000000000000000000p-16382"
247         .parse::<Quad>()
248         .unwrap();
249     assert_eq!(status, Status::OK);
250     assert!(!test.is_denormal());
251     assert!(test.bitwise_eq(expected));
252
253     // nextUp(-Smallest Normal) -> -Largest Denormal.
254     let test = unpack!(status=, "-0x1.0000000000000000000000000000p-16382"
255         .parse::<Quad>()
256         .unwrap()
257         .next_up());
258     let expected = "-0x0.ffffffffffffffffffffffffffffp-16382"
259         .parse::<Quad>()
260         .unwrap();
261     assert_eq!(status, Status::OK);
262     assert!(test.is_denormal());
263     assert!(test.bitwise_eq(expected));
264
265     // nextDown(+Smallest Normal) -> +Largest Denormal.
266     let test = unpack!(status=, "+0x1.0000000000000000000000000000p-16382"
267         .parse::<Quad>()
268         .unwrap()
269         .next_down());
270     let expected = "+0x0.ffffffffffffffffffffffffffffp-16382"
271         .parse::<Quad>()
272         .unwrap();
273     assert_eq!(status, Status::OK);
274     assert!(test.is_denormal());
275     assert!(test.bitwise_eq(expected));
276
277     // 2b. Test normal <-> normal binade boundaries.
278     //     * nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
279     //     * nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
280     //     * nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
281     //     * nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
282
283     // nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
284     let test = unpack!(status=, "-0x1p+1".parse::<Quad>().unwrap().next_up());
285     let expected = "-0x1.ffffffffffffffffffffffffffffp+0"
286         .parse::<Quad>()
287         .unwrap();
288     assert_eq!(status, Status::OK);
289     assert!(test.bitwise_eq(expected));
290
291     // nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
292     let test = unpack!(status=, "0x1p+1".parse::<Quad>().unwrap().next_down());
293     let expected = "0x1.ffffffffffffffffffffffffffffp+0"
294         .parse::<Quad>()
295         .unwrap();
296     assert_eq!(status, Status::OK);
297     assert!(test.bitwise_eq(expected));
298
299     // nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
300     let test = unpack!(status=, "0x1.ffffffffffffffffffffffffffffp+0"
301         .parse::<Quad>()
302         .unwrap()
303         .next_up());
304     let expected = "0x1p+1".parse::<Quad>().unwrap();
305     assert_eq!(status, Status::OK);
306     assert!(test.bitwise_eq(expected));
307
308     // nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
309     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffffffffp+0"
310         .parse::<Quad>()
311         .unwrap()
312         .next_down());
313     let expected = "-0x1p+1".parse::<Quad>().unwrap();
314     assert_eq!(status, Status::OK);
315     assert!(test.bitwise_eq(expected));
316
317     // 2c. Test using next at binade boundaries with a direction away from the
318     // binade boundary. Away from denormal <-> normal boundaries.
319     //
320     // This is to make sure that even though we are at a binade boundary, since
321     // we are rounding away, we do not trigger the binade boundary code. Thus we
322     // test:
323     //   * nextUp(-Largest Denormal) -> -Largest Denormal + inc.
324     //   * nextDown(+Largest Denormal) -> +Largest Denormal - inc.
325     //   * nextUp(+Smallest Normal) -> +Smallest Normal + inc.
326     //   * nextDown(-Smallest Normal) -> -Smallest Normal - inc.
327
328     // nextUp(-Largest Denormal) -> -Largest Denormal + inc.
329     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffffffffp-16382"
330         .parse::<Quad>()
331         .unwrap()
332         .next_up());
333     let expected = "-0x0.fffffffffffffffffffffffffffep-16382"
334         .parse::<Quad>()
335         .unwrap();
336     assert_eq!(status, Status::OK);
337     assert!(test.is_denormal());
338     assert!(test.is_negative());
339     assert!(test.bitwise_eq(expected));
340
341     // nextDown(+Largest Denormal) -> +Largest Denormal - inc.
342     let test = unpack!(status=, "0x0.ffffffffffffffffffffffffffffp-16382"
343         .parse::<Quad>()
344         .unwrap()
345         .next_down());
346     let expected = "0x0.fffffffffffffffffffffffffffep-16382"
347         .parse::<Quad>()
348         .unwrap();
349     assert_eq!(status, Status::OK);
350     assert!(test.is_denormal());
351     assert!(!test.is_negative());
352     assert!(test.bitwise_eq(expected));
353
354     // nextUp(+Smallest Normal) -> +Smallest Normal + inc.
355     let test = unpack!(status=, "0x1.0000000000000000000000000000p-16382"
356         .parse::<Quad>()
357         .unwrap()
358         .next_up());
359     let expected = "0x1.0000000000000000000000000001p-16382"
360         .parse::<Quad>()
361         .unwrap();
362     assert_eq!(status, Status::OK);
363     assert!(!test.is_denormal());
364     assert!(!test.is_negative());
365     assert!(test.bitwise_eq(expected));
366
367     // nextDown(-Smallest Normal) -> -Smallest Normal - inc.
368     let test = unpack!(status=, "-0x1.0000000000000000000000000000p-16382"
369         .parse::<Quad>()
370         .unwrap()
371         .next_down());
372     let expected = "-0x1.0000000000000000000000000001p-16382"
373         .parse::<Quad>()
374         .unwrap();
375     assert_eq!(status, Status::OK);
376     assert!(!test.is_denormal());
377     assert!(test.is_negative());
378     assert!(test.bitwise_eq(expected));
379
380     // 2d. Test values which cause our exponent to go to min exponent. This
381     // is to ensure that guards in the code to check for min exponent
382     // trigger properly.
383     //     * nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
384     //     * nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
385     //         -0x1p-16381
386     //     * nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16382
387     //     * nextDown(0x1p-16382) -> 0x1.ffffffffffffffffffffffffffffp-16382
388
389     // nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
390     let test = unpack!(status=, "-0x1p-16381".parse::<Quad>().unwrap().next_up());
391     let expected = "-0x1.ffffffffffffffffffffffffffffp-16382"
392         .parse::<Quad>()
393         .unwrap();
394     assert_eq!(status, Status::OK);
395     assert!(test.bitwise_eq(expected));
396
397     // nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
398     //         -0x1p-16381
399     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffffffffp-16382"
400         .parse::<Quad>()
401         .unwrap()
402         .next_down());
403     let expected = "-0x1p-16381".parse::<Quad>().unwrap();
404     assert_eq!(status, Status::OK);
405     assert!(test.bitwise_eq(expected));
406
407     // nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16381
408     let test = unpack!(status=, "0x1.ffffffffffffffffffffffffffffp-16382"
409         .parse::<Quad>()
410         .unwrap()
411         .next_up());
412     let expected = "0x1p-16381".parse::<Quad>().unwrap();
413     assert_eq!(status, Status::OK);
414     assert!(test.bitwise_eq(expected));
415
416     // nextDown(0x1p-16381) -> 0x1.ffffffffffffffffffffffffffffp-16382
417     let test = unpack!(status=, "0x1p-16381".parse::<Quad>().unwrap().next_down());
418     let expected = "0x1.ffffffffffffffffffffffffffffp-16382"
419         .parse::<Quad>()
420         .unwrap();
421     assert_eq!(status, Status::OK);
422     assert!(test.bitwise_eq(expected));
423
424     // 3. Now we test both denormal/normal computation which will not cause us
425     // to go across binade boundaries. Specifically we test:
426     //   * nextUp(+Denormal) -> +Denormal.
427     //   * nextDown(+Denormal) -> +Denormal.
428     //   * nextUp(-Denormal) -> -Denormal.
429     //   * nextDown(-Denormal) -> -Denormal.
430     //   * nextUp(+Normal) -> +Normal.
431     //   * nextDown(+Normal) -> +Normal.
432     //   * nextUp(-Normal) -> -Normal.
433     //   * nextDown(-Normal) -> -Normal.
434
435     // nextUp(+Denormal) -> +Denormal.
436     let test = unpack!(status=, "0x0.ffffffffffffffffffffffff000cp-16382"
437         .parse::<Quad>()
438         .unwrap()
439         .next_up());
440     let expected = "0x0.ffffffffffffffffffffffff000dp-16382"
441         .parse::<Quad>()
442         .unwrap();
443     assert_eq!(status, Status::OK);
444     assert!(test.is_denormal());
445     assert!(!test.is_negative());
446     assert!(test.bitwise_eq(expected));
447
448     // nextDown(+Denormal) -> +Denormal.
449     let test = unpack!(status=, "0x0.ffffffffffffffffffffffff000cp-16382"
450         .parse::<Quad>()
451         .unwrap()
452         .next_down());
453     let expected = "0x0.ffffffffffffffffffffffff000bp-16382"
454         .parse::<Quad>()
455         .unwrap();
456     assert_eq!(status, Status::OK);
457     assert!(test.is_denormal());
458     assert!(!test.is_negative());
459     assert!(test.bitwise_eq(expected));
460
461     // nextUp(-Denormal) -> -Denormal.
462     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffff000cp-16382"
463         .parse::<Quad>()
464         .unwrap()
465         .next_up());
466     let expected = "-0x0.ffffffffffffffffffffffff000bp-16382"
467         .parse::<Quad>()
468         .unwrap();
469     assert_eq!(status, Status::OK);
470     assert!(test.is_denormal());
471     assert!(test.is_negative());
472     assert!(test.bitwise_eq(expected));
473
474     // nextDown(-Denormal) -> -Denormal
475     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffff000cp-16382"
476         .parse::<Quad>()
477         .unwrap()
478         .next_down());
479     let expected = "-0x0.ffffffffffffffffffffffff000dp-16382"
480         .parse::<Quad>()
481         .unwrap();
482     assert_eq!(status, Status::OK);
483     assert!(test.is_denormal());
484     assert!(test.is_negative());
485     assert!(test.bitwise_eq(expected));
486
487     // nextUp(+Normal) -> +Normal.
488     let test = unpack!(status=, "0x1.ffffffffffffffffffffffff000cp-16000"
489         .parse::<Quad>()
490         .unwrap()
491         .next_up());
492     let expected = "0x1.ffffffffffffffffffffffff000dp-16000"
493         .parse::<Quad>()
494         .unwrap();
495     assert_eq!(status, Status::OK);
496     assert!(!test.is_denormal());
497     assert!(!test.is_negative());
498     assert!(test.bitwise_eq(expected));
499
500     // nextDown(+Normal) -> +Normal.
501     let test = unpack!(status=, "0x1.ffffffffffffffffffffffff000cp-16000"
502         .parse::<Quad>()
503         .unwrap()
504         .next_down());
505     let expected = "0x1.ffffffffffffffffffffffff000bp-16000"
506         .parse::<Quad>()
507         .unwrap();
508     assert_eq!(status, Status::OK);
509     assert!(!test.is_denormal());
510     assert!(!test.is_negative());
511     assert!(test.bitwise_eq(expected));
512
513     // nextUp(-Normal) -> -Normal.
514     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffff000cp-16000"
515         .parse::<Quad>()
516         .unwrap()
517         .next_up());
518     let expected = "-0x1.ffffffffffffffffffffffff000bp-16000"
519         .parse::<Quad>()
520         .unwrap();
521     assert_eq!(status, Status::OK);
522     assert!(!test.is_denormal());
523     assert!(test.is_negative());
524     assert!(test.bitwise_eq(expected));
525
526     // nextDown(-Normal) -> -Normal.
527     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffff000cp-16000"
528         .parse::<Quad>()
529         .unwrap()
530         .next_down());
531     let expected = "-0x1.ffffffffffffffffffffffff000dp-16000"
532         .parse::<Quad>()
533         .unwrap();
534     assert_eq!(status, Status::OK);
535     assert!(!test.is_denormal());
536     assert!(test.is_negative());
537     assert!(test.bitwise_eq(expected));
538 }
539
540 #[test]
541 fn fma() {
542     {
543         let mut f1 = Single::from_f32(14.5);
544         let f2 = Single::from_f32(-14.5);
545         let f3 = Single::from_f32(225.0);
546         f1 = f1.mul_add(f2, f3).value;
547         assert_eq!(14.75, f1.to_f32());
548     }
549
550     {
551         let val2 = Single::from_f32(2.0);
552         let mut f1 = Single::from_f32(1.17549435e-38);
553         let mut f2 = Single::from_f32(1.17549435e-38);
554         f1 /= val2;
555         f2 /= val2;
556         let f3 = Single::from_f32(12.0);
557         f1 = f1.mul_add(f2, f3).value;
558         assert_eq!(12.0, f1.to_f32());
559     }
560
561     // Test for correct zero sign when answer is exactly zero.
562     // fma(1.0, -1.0, 1.0) -> +ve 0.
563     {
564         let mut f1 = Double::from_f64(1.0);
565         let f2 = Double::from_f64(-1.0);
566         let f3 = Double::from_f64(1.0);
567         f1 = f1.mul_add(f2, f3).value;
568         assert!(!f1.is_negative() && f1.is_zero());
569     }
570
571     // Test for correct zero sign when answer is exactly zero and rounding towards
572     // negative.
573     // fma(1.0, -1.0, 1.0) -> +ve 0.
574     {
575         let mut f1 = Double::from_f64(1.0);
576         let f2 = Double::from_f64(-1.0);
577         let f3 = Double::from_f64(1.0);
578         f1 = f1.mul_add_r(f2, f3, Round::TowardNegative).value;
579         assert!(f1.is_negative() && f1.is_zero());
580     }
581
582     // Test for correct (in this case -ve) sign when adding like signed zeros.
583     // Test fma(0.0, -0.0, -0.0) -> -ve 0.
584     {
585         let mut f1 = Double::from_f64(0.0);
586         let f2 = Double::from_f64(-0.0);
587         let f3 = Double::from_f64(-0.0);
588         f1 = f1.mul_add(f2, f3).value;
589         assert!(f1.is_negative() && f1.is_zero());
590     }
591
592     // Test -ve sign preservation when small negative results underflow.
593     {
594         let mut f1 = "-0x1p-1074".parse::<Double>().unwrap();
595         let f2 = "+0x1p-1074".parse::<Double>().unwrap();
596         let f3 = Double::from_f64(0.0);
597         f1 = f1.mul_add(f2, f3).value;
598         assert!(f1.is_negative() && f1.is_zero());
599     }
600
601     // Test x87 extended precision case from http://llvm.org/PR20728.
602     {
603         let mut m1 = X87DoubleExtended::from_u128(1).value;
604         let m2 = X87DoubleExtended::from_u128(1).value;
605         let a = X87DoubleExtended::from_u128(3).value;
606
607         let mut loses_info = false;
608         m1 = m1.mul_add(m2, a).value;
609         let r: Single = m1.convert(&mut loses_info).value;
610         assert!(!loses_info);
611         assert_eq!(4.0, r.to_f32());
612     }
613 }
614
615 #[test]
616 fn min_num() {
617     let f1 = Double::from_f64(1.0);
618     let f2 = Double::from_f64(2.0);
619     let nan = Double::NAN;
620
621     assert_eq!(1.0, f1.min(f2).to_f64());
622     assert_eq!(1.0, f2.min(f1).to_f64());
623     assert_eq!(1.0, f1.min(nan).to_f64());
624     assert_eq!(1.0, nan.min(f1).to_f64());
625 }
626
627 #[test]
628 fn max_num() {
629     let f1 = Double::from_f64(1.0);
630     let f2 = Double::from_f64(2.0);
631     let nan = Double::NAN;
632
633     assert_eq!(2.0, f1.max(f2).to_f64());
634     assert_eq!(2.0, f2.max(f1).to_f64());
635     assert_eq!(1.0, f1.max(nan).to_f64());
636     assert_eq!(1.0, nan.max(f1).to_f64());
637 }
638
639 #[test]
640 fn denormal() {
641     // Test single precision
642     {
643         assert!(!Single::from_f32(0.0).is_denormal());
644
645         let mut t = "1.17549435082228750797e-38".parse::<Single>().unwrap();
646         assert!(!t.is_denormal());
647
648         let val2 = Single::from_f32(2.0e0);
649         t /= val2;
650         assert!(t.is_denormal());
651     }
652
653     // Test double precision
654     {
655         assert!(!Double::from_f64(0.0).is_denormal());
656
657         let mut t = "2.22507385850720138309e-308".parse::<Double>().unwrap();
658         assert!(!t.is_denormal());
659
660         let val2 = Double::from_f64(2.0e0);
661         t /= val2;
662         assert!(t.is_denormal());
663     }
664
665     // Test Intel double-ext
666     {
667         assert!(!X87DoubleExtended::from_u128(0).value.is_denormal());
668
669         let mut t = "3.36210314311209350626e-4932"
670             .parse::<X87DoubleExtended>()
671             .unwrap();
672         assert!(!t.is_denormal());
673
674         t /= X87DoubleExtended::from_u128(2).value;
675         assert!(t.is_denormal());
676     }
677
678     // Test quadruple precision
679     {
680         assert!(!Quad::from_u128(0).value.is_denormal());
681
682         let mut t = "3.36210314311209350626267781732175260e-4932"
683             .parse::<Quad>()
684             .unwrap();
685         assert!(!t.is_denormal());
686
687         t /= Quad::from_u128(2).value;
688         assert!(t.is_denormal());
689     }
690 }
691
692 #[test]
693 fn decimal_strings_without_null_terminators() {
694     // Make sure that we can parse strings without null terminators.
695     // rdar://14323230.
696     let val = "0.00"[..3].parse::<Double>().unwrap();
697     assert_eq!(val.to_f64(), 0.0);
698     let val = "0.01"[..3].parse::<Double>().unwrap();
699     assert_eq!(val.to_f64(), 0.0);
700     let val = "0.09"[..3].parse::<Double>().unwrap();
701     assert_eq!(val.to_f64(), 0.0);
702     let val = "0.095"[..4].parse::<Double>().unwrap();
703     assert_eq!(val.to_f64(), 0.09);
704     let val = "0.00e+3"[..7].parse::<Double>().unwrap();
705     assert_eq!(val.to_f64(), 0.00);
706     let val = "0e+3"[..4].parse::<Double>().unwrap();
707     assert_eq!(val.to_f64(), 0.00);
708
709 }
710
711 #[test]
712 fn from_zero_decimal_string() {
713     assert_eq!(0.0, "0".parse::<Double>().unwrap().to_f64());
714     assert_eq!(0.0, "+0".parse::<Double>().unwrap().to_f64());
715     assert_eq!(-0.0, "-0".parse::<Double>().unwrap().to_f64());
716
717     assert_eq!(0.0, "0.".parse::<Double>().unwrap().to_f64());
718     assert_eq!(0.0, "+0.".parse::<Double>().unwrap().to_f64());
719     assert_eq!(-0.0, "-0.".parse::<Double>().unwrap().to_f64());
720
721     assert_eq!(0.0, ".0".parse::<Double>().unwrap().to_f64());
722     assert_eq!(0.0, "+.0".parse::<Double>().unwrap().to_f64());
723     assert_eq!(-0.0, "-.0".parse::<Double>().unwrap().to_f64());
724
725     assert_eq!(0.0, "0.0".parse::<Double>().unwrap().to_f64());
726     assert_eq!(0.0, "+0.0".parse::<Double>().unwrap().to_f64());
727     assert_eq!(-0.0, "-0.0".parse::<Double>().unwrap().to_f64());
728
729     assert_eq!(0.0, "00000.".parse::<Double>().unwrap().to_f64());
730     assert_eq!(0.0, "+00000.".parse::<Double>().unwrap().to_f64());
731     assert_eq!(-0.0, "-00000.".parse::<Double>().unwrap().to_f64());
732
733     assert_eq!(0.0, ".00000".parse::<Double>().unwrap().to_f64());
734     assert_eq!(0.0, "+.00000".parse::<Double>().unwrap().to_f64());
735     assert_eq!(-0.0, "-.00000".parse::<Double>().unwrap().to_f64());
736
737     assert_eq!(0.0, "0000.00000".parse::<Double>().unwrap().to_f64());
738     assert_eq!(0.0, "+0000.00000".parse::<Double>().unwrap().to_f64());
739     assert_eq!(-0.0, "-0000.00000".parse::<Double>().unwrap().to_f64());
740 }
741
742 #[test]
743 fn from_zero_decimal_single_exponent_string() {
744     assert_eq!(0.0, "0e1".parse::<Double>().unwrap().to_f64());
745     assert_eq!(0.0, "+0e1".parse::<Double>().unwrap().to_f64());
746     assert_eq!(-0.0, "-0e1".parse::<Double>().unwrap().to_f64());
747
748     assert_eq!(0.0, "0e+1".parse::<Double>().unwrap().to_f64());
749     assert_eq!(0.0, "+0e+1".parse::<Double>().unwrap().to_f64());
750     assert_eq!(-0.0, "-0e+1".parse::<Double>().unwrap().to_f64());
751
752     assert_eq!(0.0, "0e-1".parse::<Double>().unwrap().to_f64());
753     assert_eq!(0.0, "+0e-1".parse::<Double>().unwrap().to_f64());
754     assert_eq!(-0.0, "-0e-1".parse::<Double>().unwrap().to_f64());
755
756
757     assert_eq!(0.0, "0.e1".parse::<Double>().unwrap().to_f64());
758     assert_eq!(0.0, "+0.e1".parse::<Double>().unwrap().to_f64());
759     assert_eq!(-0.0, "-0.e1".parse::<Double>().unwrap().to_f64());
760
761     assert_eq!(0.0, "0.e+1".parse::<Double>().unwrap().to_f64());
762     assert_eq!(0.0, "+0.e+1".parse::<Double>().unwrap().to_f64());
763     assert_eq!(-0.0, "-0.e+1".parse::<Double>().unwrap().to_f64());
764
765     assert_eq!(0.0, "0.e-1".parse::<Double>().unwrap().to_f64());
766     assert_eq!(0.0, "+0.e-1".parse::<Double>().unwrap().to_f64());
767     assert_eq!(-0.0, "-0.e-1".parse::<Double>().unwrap().to_f64());
768
769     assert_eq!(0.0, ".0e1".parse::<Double>().unwrap().to_f64());
770     assert_eq!(0.0, "+.0e1".parse::<Double>().unwrap().to_f64());
771     assert_eq!(-0.0, "-.0e1".parse::<Double>().unwrap().to_f64());
772
773     assert_eq!(0.0, ".0e+1".parse::<Double>().unwrap().to_f64());
774     assert_eq!(0.0, "+.0e+1".parse::<Double>().unwrap().to_f64());
775     assert_eq!(-0.0, "-.0e+1".parse::<Double>().unwrap().to_f64());
776
777     assert_eq!(0.0, ".0e-1".parse::<Double>().unwrap().to_f64());
778     assert_eq!(0.0, "+.0e-1".parse::<Double>().unwrap().to_f64());
779     assert_eq!(-0.0, "-.0e-1".parse::<Double>().unwrap().to_f64());
780
781
782     assert_eq!(0.0, "0.0e1".parse::<Double>().unwrap().to_f64());
783     assert_eq!(0.0, "+0.0e1".parse::<Double>().unwrap().to_f64());
784     assert_eq!(-0.0, "-0.0e1".parse::<Double>().unwrap().to_f64());
785
786     assert_eq!(0.0, "0.0e+1".parse::<Double>().unwrap().to_f64());
787     assert_eq!(0.0, "+0.0e+1".parse::<Double>().unwrap().to_f64());
788     assert_eq!(-0.0, "-0.0e+1".parse::<Double>().unwrap().to_f64());
789
790     assert_eq!(0.0, "0.0e-1".parse::<Double>().unwrap().to_f64());
791     assert_eq!(0.0, "+0.0e-1".parse::<Double>().unwrap().to_f64());
792     assert_eq!(-0.0, "-0.0e-1".parse::<Double>().unwrap().to_f64());
793
794
795     assert_eq!(0.0, "000.0000e1".parse::<Double>().unwrap().to_f64());
796     assert_eq!(0.0, "+000.0000e+1".parse::<Double>().unwrap().to_f64());
797     assert_eq!(-0.0, "-000.0000e+1".parse::<Double>().unwrap().to_f64());
798 }
799
800 #[test]
801 fn from_zero_decimal_large_exponent_string() {
802     assert_eq!(0.0, "0e1234".parse::<Double>().unwrap().to_f64());
803     assert_eq!(0.0, "+0e1234".parse::<Double>().unwrap().to_f64());
804     assert_eq!(-0.0, "-0e1234".parse::<Double>().unwrap().to_f64());
805
806     assert_eq!(0.0, "0e+1234".parse::<Double>().unwrap().to_f64());
807     assert_eq!(0.0, "+0e+1234".parse::<Double>().unwrap().to_f64());
808     assert_eq!(-0.0, "-0e+1234".parse::<Double>().unwrap().to_f64());
809
810     assert_eq!(0.0, "0e-1234".parse::<Double>().unwrap().to_f64());
811     assert_eq!(0.0, "+0e-1234".parse::<Double>().unwrap().to_f64());
812     assert_eq!(-0.0, "-0e-1234".parse::<Double>().unwrap().to_f64());
813
814     assert_eq!(0.0, "000.0000e1234".parse::<Double>().unwrap().to_f64());
815     assert_eq!(0.0, "000.0000e-1234".parse::<Double>().unwrap().to_f64());
816 }
817
818 #[test]
819 fn from_zero_hexadecimal_string() {
820     assert_eq!(0.0, "0x0p1".parse::<Double>().unwrap().to_f64());
821     assert_eq!(0.0, "+0x0p1".parse::<Double>().unwrap().to_f64());
822     assert_eq!(-0.0, "-0x0p1".parse::<Double>().unwrap().to_f64());
823
824     assert_eq!(0.0, "0x0p+1".parse::<Double>().unwrap().to_f64());
825     assert_eq!(0.0, "+0x0p+1".parse::<Double>().unwrap().to_f64());
826     assert_eq!(-0.0, "-0x0p+1".parse::<Double>().unwrap().to_f64());
827
828     assert_eq!(0.0, "0x0p-1".parse::<Double>().unwrap().to_f64());
829     assert_eq!(0.0, "+0x0p-1".parse::<Double>().unwrap().to_f64());
830     assert_eq!(-0.0, "-0x0p-1".parse::<Double>().unwrap().to_f64());
831
832
833     assert_eq!(0.0, "0x0.p1".parse::<Double>().unwrap().to_f64());
834     assert_eq!(0.0, "+0x0.p1".parse::<Double>().unwrap().to_f64());
835     assert_eq!(-0.0, "-0x0.p1".parse::<Double>().unwrap().to_f64());
836
837     assert_eq!(0.0, "0x0.p+1".parse::<Double>().unwrap().to_f64());
838     assert_eq!(0.0, "+0x0.p+1".parse::<Double>().unwrap().to_f64());
839     assert_eq!(-0.0, "-0x0.p+1".parse::<Double>().unwrap().to_f64());
840
841     assert_eq!(0.0, "0x0.p-1".parse::<Double>().unwrap().to_f64());
842     assert_eq!(0.0, "+0x0.p-1".parse::<Double>().unwrap().to_f64());
843     assert_eq!(-0.0, "-0x0.p-1".parse::<Double>().unwrap().to_f64());
844
845
846     assert_eq!(0.0, "0x.0p1".parse::<Double>().unwrap().to_f64());
847     assert_eq!(0.0, "+0x.0p1".parse::<Double>().unwrap().to_f64());
848     assert_eq!(-0.0, "-0x.0p1".parse::<Double>().unwrap().to_f64());
849
850     assert_eq!(0.0, "0x.0p+1".parse::<Double>().unwrap().to_f64());
851     assert_eq!(0.0, "+0x.0p+1".parse::<Double>().unwrap().to_f64());
852     assert_eq!(-0.0, "-0x.0p+1".parse::<Double>().unwrap().to_f64());
853
854     assert_eq!(0.0, "0x.0p-1".parse::<Double>().unwrap().to_f64());
855     assert_eq!(0.0, "+0x.0p-1".parse::<Double>().unwrap().to_f64());
856     assert_eq!(-0.0, "-0x.0p-1".parse::<Double>().unwrap().to_f64());
857
858
859     assert_eq!(0.0, "0x0.0p1".parse::<Double>().unwrap().to_f64());
860     assert_eq!(0.0, "+0x0.0p1".parse::<Double>().unwrap().to_f64());
861     assert_eq!(-0.0, "-0x0.0p1".parse::<Double>().unwrap().to_f64());
862
863     assert_eq!(0.0, "0x0.0p+1".parse::<Double>().unwrap().to_f64());
864     assert_eq!(0.0, "+0x0.0p+1".parse::<Double>().unwrap().to_f64());
865     assert_eq!(-0.0, "-0x0.0p+1".parse::<Double>().unwrap().to_f64());
866
867     assert_eq!(0.0, "0x0.0p-1".parse::<Double>().unwrap().to_f64());
868     assert_eq!(0.0, "+0x0.0p-1".parse::<Double>().unwrap().to_f64());
869     assert_eq!(-0.0, "-0x0.0p-1".parse::<Double>().unwrap().to_f64());
870
871
872     assert_eq!(0.0, "0x00000.p1".parse::<Double>().unwrap().to_f64());
873     assert_eq!(0.0, "0x0000.00000p1".parse::<Double>().unwrap().to_f64());
874     assert_eq!(0.0, "0x.00000p1".parse::<Double>().unwrap().to_f64());
875     assert_eq!(0.0, "0x0.p1".parse::<Double>().unwrap().to_f64());
876     assert_eq!(0.0, "0x0p1234".parse::<Double>().unwrap().to_f64());
877     assert_eq!(-0.0, "-0x0p1234".parse::<Double>().unwrap().to_f64());
878     assert_eq!(0.0, "0x00000.p1234".parse::<Double>().unwrap().to_f64());
879     assert_eq!(0.0, "0x0000.00000p1234".parse::<Double>().unwrap().to_f64());
880     assert_eq!(0.0, "0x.00000p1234".parse::<Double>().unwrap().to_f64());
881     assert_eq!(0.0, "0x0.p1234".parse::<Double>().unwrap().to_f64());
882 }
883
884 #[test]
885 fn from_decimal_string() {
886     assert_eq!(1.0, "1".parse::<Double>().unwrap().to_f64());
887     assert_eq!(2.0, "2.".parse::<Double>().unwrap().to_f64());
888     assert_eq!(0.5, ".5".parse::<Double>().unwrap().to_f64());
889     assert_eq!(1.0, "1.0".parse::<Double>().unwrap().to_f64());
890     assert_eq!(-2.0, "-2".parse::<Double>().unwrap().to_f64());
891     assert_eq!(-4.0, "-4.".parse::<Double>().unwrap().to_f64());
892     assert_eq!(-0.5, "-.5".parse::<Double>().unwrap().to_f64());
893     assert_eq!(-1.5, "-1.5".parse::<Double>().unwrap().to_f64());
894     assert_eq!(1.25e12, "1.25e12".parse::<Double>().unwrap().to_f64());
895     assert_eq!(1.25e+12, "1.25e+12".parse::<Double>().unwrap().to_f64());
896     assert_eq!(1.25e-12, "1.25e-12".parse::<Double>().unwrap().to_f64());
897     assert_eq!(1024.0, "1024.".parse::<Double>().unwrap().to_f64());
898     assert_eq!(1024.05, "1024.05000".parse::<Double>().unwrap().to_f64());
899     assert_eq!(0.05, ".05000".parse::<Double>().unwrap().to_f64());
900     assert_eq!(2.0, "2.".parse::<Double>().unwrap().to_f64());
901     assert_eq!(2.0e2, "2.e2".parse::<Double>().unwrap().to_f64());
902     assert_eq!(2.0e+2, "2.e+2".parse::<Double>().unwrap().to_f64());
903     assert_eq!(2.0e-2, "2.e-2".parse::<Double>().unwrap().to_f64());
904     assert_eq!(2.05e2, "002.05000e2".parse::<Double>().unwrap().to_f64());
905     assert_eq!(2.05e+2, "002.05000e+2".parse::<Double>().unwrap().to_f64());
906     assert_eq!(2.05e-2, "002.05000e-2".parse::<Double>().unwrap().to_f64());
907     assert_eq!(2.05e12, "002.05000e12".parse::<Double>().unwrap().to_f64());
908     assert_eq!(
909         2.05e+12,
910         "002.05000e+12".parse::<Double>().unwrap().to_f64()
911     );
912     assert_eq!(
913         2.05e-12,
914         "002.05000e-12".parse::<Double>().unwrap().to_f64()
915     );
916
917     // These are "carefully selected" to overflow the fast log-base
918     // calculations in the implementation.
919     assert!("99e99999".parse::<Double>().unwrap().is_infinite());
920     assert!("-99e99999".parse::<Double>().unwrap().is_infinite());
921     assert!("1e-99999".parse::<Double>().unwrap().is_pos_zero());
922     assert!("-1e-99999".parse::<Double>().unwrap().is_neg_zero());
923
924     assert_eq!(2.71828, "2.71828".parse::<Double>().unwrap().to_f64());
925 }
926
927 #[test]
928 fn from_hexadecimal_string() {
929     assert_eq!(1.0, "0x1p0".parse::<Double>().unwrap().to_f64());
930     assert_eq!(1.0, "+0x1p0".parse::<Double>().unwrap().to_f64());
931     assert_eq!(-1.0, "-0x1p0".parse::<Double>().unwrap().to_f64());
932
933     assert_eq!(1.0, "0x1p+0".parse::<Double>().unwrap().to_f64());
934     assert_eq!(1.0, "+0x1p+0".parse::<Double>().unwrap().to_f64());
935     assert_eq!(-1.0, "-0x1p+0".parse::<Double>().unwrap().to_f64());
936
937     assert_eq!(1.0, "0x1p-0".parse::<Double>().unwrap().to_f64());
938     assert_eq!(1.0, "+0x1p-0".parse::<Double>().unwrap().to_f64());
939     assert_eq!(-1.0, "-0x1p-0".parse::<Double>().unwrap().to_f64());
940
941
942     assert_eq!(2.0, "0x1p1".parse::<Double>().unwrap().to_f64());
943     assert_eq!(2.0, "+0x1p1".parse::<Double>().unwrap().to_f64());
944     assert_eq!(-2.0, "-0x1p1".parse::<Double>().unwrap().to_f64());
945
946     assert_eq!(2.0, "0x1p+1".parse::<Double>().unwrap().to_f64());
947     assert_eq!(2.0, "+0x1p+1".parse::<Double>().unwrap().to_f64());
948     assert_eq!(-2.0, "-0x1p+1".parse::<Double>().unwrap().to_f64());
949
950     assert_eq!(0.5, "0x1p-1".parse::<Double>().unwrap().to_f64());
951     assert_eq!(0.5, "+0x1p-1".parse::<Double>().unwrap().to_f64());
952     assert_eq!(-0.5, "-0x1p-1".parse::<Double>().unwrap().to_f64());
953
954
955     assert_eq!(3.0, "0x1.8p1".parse::<Double>().unwrap().to_f64());
956     assert_eq!(3.0, "+0x1.8p1".parse::<Double>().unwrap().to_f64());
957     assert_eq!(-3.0, "-0x1.8p1".parse::<Double>().unwrap().to_f64());
958
959     assert_eq!(3.0, "0x1.8p+1".parse::<Double>().unwrap().to_f64());
960     assert_eq!(3.0, "+0x1.8p+1".parse::<Double>().unwrap().to_f64());
961     assert_eq!(-3.0, "-0x1.8p+1".parse::<Double>().unwrap().to_f64());
962
963     assert_eq!(0.75, "0x1.8p-1".parse::<Double>().unwrap().to_f64());
964     assert_eq!(0.75, "+0x1.8p-1".parse::<Double>().unwrap().to_f64());
965     assert_eq!(-0.75, "-0x1.8p-1".parse::<Double>().unwrap().to_f64());
966
967
968     assert_eq!(8192.0, "0x1000.000p1".parse::<Double>().unwrap().to_f64());
969     assert_eq!(8192.0, "+0x1000.000p1".parse::<Double>().unwrap().to_f64());
970     assert_eq!(-8192.0, "-0x1000.000p1".parse::<Double>().unwrap().to_f64());
971
972     assert_eq!(8192.0, "0x1000.000p+1".parse::<Double>().unwrap().to_f64());
973     assert_eq!(8192.0, "+0x1000.000p+1".parse::<Double>().unwrap().to_f64());
974     assert_eq!(
975         -8192.0,
976         "-0x1000.000p+1".parse::<Double>().unwrap().to_f64()
977     );
978
979     assert_eq!(2048.0, "0x1000.000p-1".parse::<Double>().unwrap().to_f64());
980     assert_eq!(2048.0, "+0x1000.000p-1".parse::<Double>().unwrap().to_f64());
981     assert_eq!(
982         -2048.0,
983         "-0x1000.000p-1".parse::<Double>().unwrap().to_f64()
984     );
985
986
987     assert_eq!(8192.0, "0x1000p1".parse::<Double>().unwrap().to_f64());
988     assert_eq!(8192.0, "+0x1000p1".parse::<Double>().unwrap().to_f64());
989     assert_eq!(-8192.0, "-0x1000p1".parse::<Double>().unwrap().to_f64());
990
991     assert_eq!(8192.0, "0x1000p+1".parse::<Double>().unwrap().to_f64());
992     assert_eq!(8192.0, "+0x1000p+1".parse::<Double>().unwrap().to_f64());
993     assert_eq!(-8192.0, "-0x1000p+1".parse::<Double>().unwrap().to_f64());
994
995     assert_eq!(2048.0, "0x1000p-1".parse::<Double>().unwrap().to_f64());
996     assert_eq!(2048.0, "+0x1000p-1".parse::<Double>().unwrap().to_f64());
997     assert_eq!(-2048.0, "-0x1000p-1".parse::<Double>().unwrap().to_f64());
998
999
1000     assert_eq!(16384.0, "0x10p10".parse::<Double>().unwrap().to_f64());
1001     assert_eq!(16384.0, "+0x10p10".parse::<Double>().unwrap().to_f64());
1002     assert_eq!(-16384.0, "-0x10p10".parse::<Double>().unwrap().to_f64());
1003
1004     assert_eq!(16384.0, "0x10p+10".parse::<Double>().unwrap().to_f64());
1005     assert_eq!(16384.0, "+0x10p+10".parse::<Double>().unwrap().to_f64());
1006     assert_eq!(-16384.0, "-0x10p+10".parse::<Double>().unwrap().to_f64());
1007
1008     assert_eq!(0.015625, "0x10p-10".parse::<Double>().unwrap().to_f64());
1009     assert_eq!(0.015625, "+0x10p-10".parse::<Double>().unwrap().to_f64());
1010     assert_eq!(-0.015625, "-0x10p-10".parse::<Double>().unwrap().to_f64());
1011
1012     assert_eq!(1.0625, "0x1.1p0".parse::<Double>().unwrap().to_f64());
1013     assert_eq!(1.0, "0x1p0".parse::<Double>().unwrap().to_f64());
1014
1015     assert_eq!(
1016         "0x1p-150".parse::<Double>().unwrap().to_f64(),
1017         "+0x800000000000000001.p-221"
1018             .parse::<Double>()
1019             .unwrap()
1020             .to_f64()
1021     );
1022     assert_eq!(
1023         2251799813685248.5,
1024         "0x80000000000004000000.010p-28"
1025             .parse::<Double>()
1026             .unwrap()
1027             .to_f64()
1028     );
1029 }
1030
1031 #[test]
1032 fn to_string() {
1033     let to_string = |d: f64, precision: usize, width: usize| {
1034         let x = Double::from_f64(d);
1035         if precision == 0 {
1036             format!("{:1$}", x, width)
1037         } else {
1038             format!("{:2$.1$}", x, precision, width)
1039         }
1040     };
1041     assert_eq!("10", to_string(10.0, 6, 3));
1042     assert_eq!("1.0E+1", to_string(10.0, 6, 0));
1043     assert_eq!("10100", to_string(1.01E+4, 5, 2));
1044     assert_eq!("1.01E+4", to_string(1.01E+4, 4, 2));
1045     assert_eq!("1.01E+4", to_string(1.01E+4, 5, 1));
1046     assert_eq!("0.0101", to_string(1.01E-2, 5, 2));
1047     assert_eq!("0.0101", to_string(1.01E-2, 4, 2));
1048     assert_eq!("1.01E-2", to_string(1.01E-2, 5, 1));
1049     assert_eq!(
1050         "0.78539816339744828",
1051         to_string(0.78539816339744830961, 0, 3)
1052     );
1053     assert_eq!(
1054         "4.9406564584124654E-324",
1055         to_string(4.9406564584124654e-324, 0, 3)
1056     );
1057     assert_eq!("873.18340000000001", to_string(873.1834, 0, 1));
1058     assert_eq!("8.7318340000000001E+2", to_string(873.1834, 0, 0));
1059     assert_eq!(
1060         "1.7976931348623157E+308",
1061         to_string(1.7976931348623157E+308, 0, 0)
1062     );
1063
1064     let to_string = |d: f64, precision: usize, width: usize| {
1065         let x = Double::from_f64(d);
1066         if precision == 0 {
1067             format!("{:#1$}", x, width)
1068         } else {
1069             format!("{:#2$.1$}", x, precision, width)
1070         }
1071     };
1072     assert_eq!("10", to_string(10.0, 6, 3));
1073     assert_eq!("1.000000e+01", to_string(10.0, 6, 0));
1074     assert_eq!("10100", to_string(1.01E+4, 5, 2));
1075     assert_eq!("1.0100e+04", to_string(1.01E+4, 4, 2));
1076     assert_eq!("1.01000e+04", to_string(1.01E+4, 5, 1));
1077     assert_eq!("0.0101", to_string(1.01E-2, 5, 2));
1078     assert_eq!("0.0101", to_string(1.01E-2, 4, 2));
1079     assert_eq!("1.01000e-02", to_string(1.01E-2, 5, 1));
1080     assert_eq!(
1081         "0.78539816339744828",
1082         to_string(0.78539816339744830961, 0, 3)
1083     );
1084     assert_eq!(
1085         "4.94065645841246540e-324",
1086         to_string(4.9406564584124654e-324, 0, 3)
1087     );
1088     assert_eq!("873.18340000000001", to_string(873.1834, 0, 1));
1089     assert_eq!("8.73183400000000010e+02", to_string(873.1834, 0, 0));
1090     assert_eq!(
1091         "1.79769313486231570e+308",
1092         to_string(1.7976931348623157E+308, 0, 0)
1093     );
1094 }
1095
1096 #[test]
1097 fn to_integer() {
1098     let mut is_exact = false;
1099
1100     assert_eq!(
1101         Status::OK.and(10),
1102         "10".parse::<Double>().unwrap().to_u128_r(
1103             5,
1104             Round::TowardZero,
1105             &mut is_exact,
1106         )
1107     );
1108     assert!(is_exact);
1109
1110     assert_eq!(
1111         Status::INVALID_OP.and(0),
1112         "-10".parse::<Double>().unwrap().to_u128_r(
1113             5,
1114             Round::TowardZero,
1115             &mut is_exact,
1116         )
1117     );
1118     assert!(!is_exact);
1119
1120     assert_eq!(
1121         Status::INVALID_OP.and(31),
1122         "32".parse::<Double>().unwrap().to_u128_r(
1123             5,
1124             Round::TowardZero,
1125             &mut is_exact,
1126         )
1127     );
1128     assert!(!is_exact);
1129
1130     assert_eq!(
1131         Status::INEXACT.and(7),
1132         "7.9".parse::<Double>().unwrap().to_u128_r(
1133             5,
1134             Round::TowardZero,
1135             &mut is_exact,
1136         )
1137     );
1138     assert!(!is_exact);
1139
1140     assert_eq!(
1141         Status::OK.and(-10),
1142         "-10".parse::<Double>().unwrap().to_i128_r(
1143             5,
1144             Round::TowardZero,
1145             &mut is_exact,
1146         )
1147     );
1148     assert!(is_exact);
1149
1150     assert_eq!(
1151         Status::INVALID_OP.and(-16),
1152         "-17".parse::<Double>().unwrap().to_i128_r(
1153             5,
1154             Round::TowardZero,
1155             &mut is_exact,
1156         )
1157     );
1158     assert!(!is_exact);
1159
1160     assert_eq!(
1161         Status::INVALID_OP.and(15),
1162         "16".parse::<Double>().unwrap().to_i128_r(
1163             5,
1164             Round::TowardZero,
1165             &mut is_exact,
1166         )
1167     );
1168     assert!(!is_exact);
1169 }
1170
1171 #[test]
1172 fn nan() {
1173     fn nanbits<T: Float>(signaling: bool, negative: bool, fill: u128) -> u128 {
1174         let x = if signaling {
1175             T::snan(Some(fill))
1176         } else {
1177             T::qnan(Some(fill))
1178         };
1179         if negative {
1180             (-x).to_bits()
1181         } else {
1182             x.to_bits()
1183         }
1184     }
1185
1186     assert_eq!(0x7fc00000, nanbits::<Single>(false, false, 0));
1187     assert_eq!(0xffc00000, nanbits::<Single>(false, true, 0));
1188     assert_eq!(0x7fc0ae72, nanbits::<Single>(false, false, 0xae72));
1189     assert_eq!(0x7fffae72, nanbits::<Single>(false, false, 0xffffae72));
1190     assert_eq!(0x7fa00000, nanbits::<Single>(true, false, 0));
1191     assert_eq!(0xffa00000, nanbits::<Single>(true, true, 0));
1192     assert_eq!(0x7f80ae72, nanbits::<Single>(true, false, 0xae72));
1193     assert_eq!(0x7fbfae72, nanbits::<Single>(true, false, 0xffffae72));
1194
1195     assert_eq!(0x7ff8000000000000, nanbits::<Double>(false, false, 0));
1196     assert_eq!(0xfff8000000000000, nanbits::<Double>(false, true, 0));
1197     assert_eq!(0x7ff800000000ae72, nanbits::<Double>(false, false, 0xae72));
1198     assert_eq!(
1199         0x7fffffffffffae72,
1200         nanbits::<Double>(false, false, 0xffffffffffffae72)
1201     );
1202     assert_eq!(0x7ff4000000000000, nanbits::<Double>(true, false, 0));
1203     assert_eq!(0xfff4000000000000, nanbits::<Double>(true, true, 0));
1204     assert_eq!(0x7ff000000000ae72, nanbits::<Double>(true, false, 0xae72));
1205     assert_eq!(
1206         0x7ff7ffffffffae72,
1207         nanbits::<Double>(true, false, 0xffffffffffffae72)
1208     );
1209 }
1210
1211 #[test]
1212 fn string_decimal_death() {
1213     assert_eq!(
1214         "".parse::<Double>(),
1215         Err(ParseError("Invalid string length"))
1216     );
1217     assert_eq!(
1218         "+".parse::<Double>(),
1219         Err(ParseError("String has no digits"))
1220     );
1221     assert_eq!(
1222         "-".parse::<Double>(),
1223         Err(ParseError("String has no digits"))
1224     );
1225
1226     assert_eq!(
1227         "\0".parse::<Double>(),
1228         Err(ParseError("Invalid character in significand"))
1229     );
1230     assert_eq!(
1231         "1\0".parse::<Double>(),
1232         Err(ParseError("Invalid character in significand"))
1233     );
1234     assert_eq!(
1235         "1\02".parse::<Double>(),
1236         Err(ParseError("Invalid character in significand"))
1237     );
1238     assert_eq!(
1239         "1\02e1".parse::<Double>(),
1240         Err(ParseError("Invalid character in significand"))
1241     );
1242     assert_eq!(
1243         "1e\0".parse::<Double>(),
1244         Err(ParseError("Invalid character in exponent"))
1245     );
1246     assert_eq!(
1247         "1e1\0".parse::<Double>(),
1248         Err(ParseError("Invalid character in exponent"))
1249     );
1250     assert_eq!(
1251         "1e1\02".parse::<Double>(),
1252         Err(ParseError("Invalid character in exponent"))
1253     );
1254
1255     assert_eq!(
1256         "1.0f".parse::<Double>(),
1257         Err(ParseError("Invalid character in significand"))
1258     );
1259
1260     assert_eq!(
1261         "..".parse::<Double>(),
1262         Err(ParseError("String contains multiple dots"))
1263     );
1264     assert_eq!(
1265         "..0".parse::<Double>(),
1266         Err(ParseError("String contains multiple dots"))
1267     );
1268     assert_eq!(
1269         "1.0.0".parse::<Double>(),
1270         Err(ParseError("String contains multiple dots"))
1271     );
1272 }
1273
1274 #[test]
1275 fn string_decimal_significand_death() {
1276     assert_eq!(
1277         ".".parse::<Double>(),
1278         Err(ParseError("Significand has no digits"))
1279     );
1280     assert_eq!(
1281         "+.".parse::<Double>(),
1282         Err(ParseError("Significand has no digits"))
1283     );
1284     assert_eq!(
1285         "-.".parse::<Double>(),
1286         Err(ParseError("Significand has no digits"))
1287     );
1288
1289
1290     assert_eq!(
1291         "e".parse::<Double>(),
1292         Err(ParseError("Significand has no digits"))
1293     );
1294     assert_eq!(
1295         "+e".parse::<Double>(),
1296         Err(ParseError("Significand has no digits"))
1297     );
1298     assert_eq!(
1299         "-e".parse::<Double>(),
1300         Err(ParseError("Significand has no digits"))
1301     );
1302
1303     assert_eq!(
1304         "e1".parse::<Double>(),
1305         Err(ParseError("Significand has no digits"))
1306     );
1307     assert_eq!(
1308         "+e1".parse::<Double>(),
1309         Err(ParseError("Significand has no digits"))
1310     );
1311     assert_eq!(
1312         "-e1".parse::<Double>(),
1313         Err(ParseError("Significand has no digits"))
1314     );
1315
1316     assert_eq!(
1317         ".e1".parse::<Double>(),
1318         Err(ParseError("Significand has no digits"))
1319     );
1320     assert_eq!(
1321         "+.e1".parse::<Double>(),
1322         Err(ParseError("Significand has no digits"))
1323     );
1324     assert_eq!(
1325         "-.e1".parse::<Double>(),
1326         Err(ParseError("Significand has no digits"))
1327     );
1328
1329
1330     assert_eq!(
1331         ".e".parse::<Double>(),
1332         Err(ParseError("Significand has no digits"))
1333     );
1334     assert_eq!(
1335         "+.e".parse::<Double>(),
1336         Err(ParseError("Significand has no digits"))
1337     );
1338     assert_eq!(
1339         "-.e".parse::<Double>(),
1340         Err(ParseError("Significand has no digits"))
1341     );
1342 }
1343
1344 #[test]
1345 fn string_decimal_exponent_death() {
1346     assert_eq!(
1347         "1e".parse::<Double>(),
1348         Err(ParseError("Exponent has no digits"))
1349     );
1350     assert_eq!(
1351         "+1e".parse::<Double>(),
1352         Err(ParseError("Exponent has no digits"))
1353     );
1354     assert_eq!(
1355         "-1e".parse::<Double>(),
1356         Err(ParseError("Exponent has no digits"))
1357     );
1358
1359     assert_eq!(
1360         "1.e".parse::<Double>(),
1361         Err(ParseError("Exponent has no digits"))
1362     );
1363     assert_eq!(
1364         "+1.e".parse::<Double>(),
1365         Err(ParseError("Exponent has no digits"))
1366     );
1367     assert_eq!(
1368         "-1.e".parse::<Double>(),
1369         Err(ParseError("Exponent has no digits"))
1370     );
1371
1372     assert_eq!(
1373         ".1e".parse::<Double>(),
1374         Err(ParseError("Exponent has no digits"))
1375     );
1376     assert_eq!(
1377         "+.1e".parse::<Double>(),
1378         Err(ParseError("Exponent has no digits"))
1379     );
1380     assert_eq!(
1381         "-.1e".parse::<Double>(),
1382         Err(ParseError("Exponent has no digits"))
1383     );
1384
1385     assert_eq!(
1386         "1.1e".parse::<Double>(),
1387         Err(ParseError("Exponent has no digits"))
1388     );
1389     assert_eq!(
1390         "+1.1e".parse::<Double>(),
1391         Err(ParseError("Exponent has no digits"))
1392     );
1393     assert_eq!(
1394         "-1.1e".parse::<Double>(),
1395         Err(ParseError("Exponent has no digits"))
1396     );
1397
1398
1399     assert_eq!(
1400         "1e+".parse::<Double>(),
1401         Err(ParseError("Exponent has no digits"))
1402     );
1403     assert_eq!(
1404         "1e-".parse::<Double>(),
1405         Err(ParseError("Exponent has no digits"))
1406     );
1407
1408     assert_eq!(
1409         ".1e".parse::<Double>(),
1410         Err(ParseError("Exponent has no digits"))
1411     );
1412     assert_eq!(
1413         ".1e+".parse::<Double>(),
1414         Err(ParseError("Exponent has no digits"))
1415     );
1416     assert_eq!(
1417         ".1e-".parse::<Double>(),
1418         Err(ParseError("Exponent has no digits"))
1419     );
1420
1421     assert_eq!(
1422         "1.0e".parse::<Double>(),
1423         Err(ParseError("Exponent has no digits"))
1424     );
1425     assert_eq!(
1426         "1.0e+".parse::<Double>(),
1427         Err(ParseError("Exponent has no digits"))
1428     );
1429     assert_eq!(
1430         "1.0e-".parse::<Double>(),
1431         Err(ParseError("Exponent has no digits"))
1432     );
1433 }
1434
1435 #[test]
1436 fn string_hexadecimal_death() {
1437     assert_eq!("0x".parse::<Double>(), Err(ParseError("Invalid string")));
1438     assert_eq!("+0x".parse::<Double>(), Err(ParseError("Invalid string")));
1439     assert_eq!("-0x".parse::<Double>(), Err(ParseError("Invalid string")));
1440
1441     assert_eq!(
1442         "0x0".parse::<Double>(),
1443         Err(ParseError("Hex strings require an exponent"))
1444     );
1445     assert_eq!(
1446         "+0x0".parse::<Double>(),
1447         Err(ParseError("Hex strings require an exponent"))
1448     );
1449     assert_eq!(
1450         "-0x0".parse::<Double>(),
1451         Err(ParseError("Hex strings require an exponent"))
1452     );
1453
1454     assert_eq!(
1455         "0x0.".parse::<Double>(),
1456         Err(ParseError("Hex strings require an exponent"))
1457     );
1458     assert_eq!(
1459         "+0x0.".parse::<Double>(),
1460         Err(ParseError("Hex strings require an exponent"))
1461     );
1462     assert_eq!(
1463         "-0x0.".parse::<Double>(),
1464         Err(ParseError("Hex strings require an exponent"))
1465     );
1466
1467     assert_eq!(
1468         "0x.0".parse::<Double>(),
1469         Err(ParseError("Hex strings require an exponent"))
1470     );
1471     assert_eq!(
1472         "+0x.0".parse::<Double>(),
1473         Err(ParseError("Hex strings require an exponent"))
1474     );
1475     assert_eq!(
1476         "-0x.0".parse::<Double>(),
1477         Err(ParseError("Hex strings require an exponent"))
1478     );
1479
1480     assert_eq!(
1481         "0x0.0".parse::<Double>(),
1482         Err(ParseError("Hex strings require an exponent"))
1483     );
1484     assert_eq!(
1485         "+0x0.0".parse::<Double>(),
1486         Err(ParseError("Hex strings require an exponent"))
1487     );
1488     assert_eq!(
1489         "-0x0.0".parse::<Double>(),
1490         Err(ParseError("Hex strings require an exponent"))
1491     );
1492
1493     assert_eq!(
1494         "0x\0".parse::<Double>(),
1495         Err(ParseError("Invalid character in significand"))
1496     );
1497     assert_eq!(
1498         "0x1\0".parse::<Double>(),
1499         Err(ParseError("Invalid character in significand"))
1500     );
1501     assert_eq!(
1502         "0x1\02".parse::<Double>(),
1503         Err(ParseError("Invalid character in significand"))
1504     );
1505     assert_eq!(
1506         "0x1\02p1".parse::<Double>(),
1507         Err(ParseError("Invalid character in significand"))
1508     );
1509     assert_eq!(
1510         "0x1p\0".parse::<Double>(),
1511         Err(ParseError("Invalid character in exponent"))
1512     );
1513     assert_eq!(
1514         "0x1p1\0".parse::<Double>(),
1515         Err(ParseError("Invalid character in exponent"))
1516     );
1517     assert_eq!(
1518         "0x1p1\02".parse::<Double>(),
1519         Err(ParseError("Invalid character in exponent"))
1520     );
1521
1522     assert_eq!(
1523         "0x1p0f".parse::<Double>(),
1524         Err(ParseError("Invalid character in exponent"))
1525     );
1526
1527     assert_eq!(
1528         "0x..p1".parse::<Double>(),
1529         Err(ParseError("String contains multiple dots"))
1530     );
1531     assert_eq!(
1532         "0x..0p1".parse::<Double>(),
1533         Err(ParseError("String contains multiple dots"))
1534     );
1535     assert_eq!(
1536         "0x1.0.0p1".parse::<Double>(),
1537         Err(ParseError("String contains multiple dots"))
1538     );
1539 }
1540
1541 #[test]
1542 fn string_hexadecimal_significand_death() {
1543     assert_eq!(
1544         "0x.".parse::<Double>(),
1545         Err(ParseError("Significand has no digits"))
1546     );
1547     assert_eq!(
1548         "+0x.".parse::<Double>(),
1549         Err(ParseError("Significand has no digits"))
1550     );
1551     assert_eq!(
1552         "-0x.".parse::<Double>(),
1553         Err(ParseError("Significand has no digits"))
1554     );
1555
1556     assert_eq!(
1557         "0xp".parse::<Double>(),
1558         Err(ParseError("Significand has no digits"))
1559     );
1560     assert_eq!(
1561         "+0xp".parse::<Double>(),
1562         Err(ParseError("Significand has no digits"))
1563     );
1564     assert_eq!(
1565         "-0xp".parse::<Double>(),
1566         Err(ParseError("Significand has no digits"))
1567     );
1568
1569     assert_eq!(
1570         "0xp+".parse::<Double>(),
1571         Err(ParseError("Significand has no digits"))
1572     );
1573     assert_eq!(
1574         "+0xp+".parse::<Double>(),
1575         Err(ParseError("Significand has no digits"))
1576     );
1577     assert_eq!(
1578         "-0xp+".parse::<Double>(),
1579         Err(ParseError("Significand has no digits"))
1580     );
1581
1582     assert_eq!(
1583         "0xp-".parse::<Double>(),
1584         Err(ParseError("Significand has no digits"))
1585     );
1586     assert_eq!(
1587         "+0xp-".parse::<Double>(),
1588         Err(ParseError("Significand has no digits"))
1589     );
1590     assert_eq!(
1591         "-0xp-".parse::<Double>(),
1592         Err(ParseError("Significand has no digits"))
1593     );
1594
1595
1596     assert_eq!(
1597         "0x.p".parse::<Double>(),
1598         Err(ParseError("Significand has no digits"))
1599     );
1600     assert_eq!(
1601         "+0x.p".parse::<Double>(),
1602         Err(ParseError("Significand has no digits"))
1603     );
1604     assert_eq!(
1605         "-0x.p".parse::<Double>(),
1606         Err(ParseError("Significand has no digits"))
1607     );
1608
1609     assert_eq!(
1610         "0x.p+".parse::<Double>(),
1611         Err(ParseError("Significand has no digits"))
1612     );
1613     assert_eq!(
1614         "+0x.p+".parse::<Double>(),
1615         Err(ParseError("Significand has no digits"))
1616     );
1617     assert_eq!(
1618         "-0x.p+".parse::<Double>(),
1619         Err(ParseError("Significand has no digits"))
1620     );
1621
1622     assert_eq!(
1623         "0x.p-".parse::<Double>(),
1624         Err(ParseError("Significand has no digits"))
1625     );
1626     assert_eq!(
1627         "+0x.p-".parse::<Double>(),
1628         Err(ParseError("Significand has no digits"))
1629     );
1630     assert_eq!(
1631         "-0x.p-".parse::<Double>(),
1632         Err(ParseError("Significand has no digits"))
1633     );
1634 }
1635
1636 #[test]
1637 fn string_hexadecimal_exponent_death() {
1638     assert_eq!(
1639         "0x1p".parse::<Double>(),
1640         Err(ParseError("Exponent has no digits"))
1641     );
1642     assert_eq!(
1643         "+0x1p".parse::<Double>(),
1644         Err(ParseError("Exponent has no digits"))
1645     );
1646     assert_eq!(
1647         "-0x1p".parse::<Double>(),
1648         Err(ParseError("Exponent has no digits"))
1649     );
1650
1651     assert_eq!(
1652         "0x1p+".parse::<Double>(),
1653         Err(ParseError("Exponent has no digits"))
1654     );
1655     assert_eq!(
1656         "+0x1p+".parse::<Double>(),
1657         Err(ParseError("Exponent has no digits"))
1658     );
1659     assert_eq!(
1660         "-0x1p+".parse::<Double>(),
1661         Err(ParseError("Exponent has no digits"))
1662     );
1663
1664     assert_eq!(
1665         "0x1p-".parse::<Double>(),
1666         Err(ParseError("Exponent has no digits"))
1667     );
1668     assert_eq!(
1669         "+0x1p-".parse::<Double>(),
1670         Err(ParseError("Exponent has no digits"))
1671     );
1672     assert_eq!(
1673         "-0x1p-".parse::<Double>(),
1674         Err(ParseError("Exponent has no digits"))
1675     );
1676
1677
1678     assert_eq!(
1679         "0x1.p".parse::<Double>(),
1680         Err(ParseError("Exponent has no digits"))
1681     );
1682     assert_eq!(
1683         "+0x1.p".parse::<Double>(),
1684         Err(ParseError("Exponent has no digits"))
1685     );
1686     assert_eq!(
1687         "-0x1.p".parse::<Double>(),
1688         Err(ParseError("Exponent has no digits"))
1689     );
1690
1691     assert_eq!(
1692         "0x1.p+".parse::<Double>(),
1693         Err(ParseError("Exponent has no digits"))
1694     );
1695     assert_eq!(
1696         "+0x1.p+".parse::<Double>(),
1697         Err(ParseError("Exponent has no digits"))
1698     );
1699     assert_eq!(
1700         "-0x1.p+".parse::<Double>(),
1701         Err(ParseError("Exponent has no digits"))
1702     );
1703
1704     assert_eq!(
1705         "0x1.p-".parse::<Double>(),
1706         Err(ParseError("Exponent has no digits"))
1707     );
1708     assert_eq!(
1709         "+0x1.p-".parse::<Double>(),
1710         Err(ParseError("Exponent has no digits"))
1711     );
1712     assert_eq!(
1713         "-0x1.p-".parse::<Double>(),
1714         Err(ParseError("Exponent has no digits"))
1715     );
1716
1717
1718     assert_eq!(
1719         "0x.1p".parse::<Double>(),
1720         Err(ParseError("Exponent has no digits"))
1721     );
1722     assert_eq!(
1723         "+0x.1p".parse::<Double>(),
1724         Err(ParseError("Exponent has no digits"))
1725     );
1726     assert_eq!(
1727         "-0x.1p".parse::<Double>(),
1728         Err(ParseError("Exponent has no digits"))
1729     );
1730
1731     assert_eq!(
1732         "0x.1p+".parse::<Double>(),
1733         Err(ParseError("Exponent has no digits"))
1734     );
1735     assert_eq!(
1736         "+0x.1p+".parse::<Double>(),
1737         Err(ParseError("Exponent has no digits"))
1738     );
1739     assert_eq!(
1740         "-0x.1p+".parse::<Double>(),
1741         Err(ParseError("Exponent has no digits"))
1742     );
1743
1744     assert_eq!(
1745         "0x.1p-".parse::<Double>(),
1746         Err(ParseError("Exponent has no digits"))
1747     );
1748     assert_eq!(
1749         "+0x.1p-".parse::<Double>(),
1750         Err(ParseError("Exponent has no digits"))
1751     );
1752     assert_eq!(
1753         "-0x.1p-".parse::<Double>(),
1754         Err(ParseError("Exponent has no digits"))
1755     );
1756
1757
1758     assert_eq!(
1759         "0x1.1p".parse::<Double>(),
1760         Err(ParseError("Exponent has no digits"))
1761     );
1762     assert_eq!(
1763         "+0x1.1p".parse::<Double>(),
1764         Err(ParseError("Exponent has no digits"))
1765     );
1766     assert_eq!(
1767         "-0x1.1p".parse::<Double>(),
1768         Err(ParseError("Exponent has no digits"))
1769     );
1770
1771     assert_eq!(
1772         "0x1.1p+".parse::<Double>(),
1773         Err(ParseError("Exponent has no digits"))
1774     );
1775     assert_eq!(
1776         "+0x1.1p+".parse::<Double>(),
1777         Err(ParseError("Exponent has no digits"))
1778     );
1779     assert_eq!(
1780         "-0x1.1p+".parse::<Double>(),
1781         Err(ParseError("Exponent has no digits"))
1782     );
1783
1784     assert_eq!(
1785         "0x1.1p-".parse::<Double>(),
1786         Err(ParseError("Exponent has no digits"))
1787     );
1788     assert_eq!(
1789         "+0x1.1p-".parse::<Double>(),
1790         Err(ParseError("Exponent has no digits"))
1791     );
1792     assert_eq!(
1793         "-0x1.1p-".parse::<Double>(),
1794         Err(ParseError("Exponent has no digits"))
1795     );
1796 }
1797
1798 #[test]
1799 fn exact_inverse() {
1800     // Trivial operation.
1801     assert!(
1802         Double::from_f64(2.0)
1803             .get_exact_inverse()
1804             .unwrap()
1805             .bitwise_eq(Double::from_f64(0.5))
1806     );
1807     assert!(
1808         Single::from_f32(2.0)
1809             .get_exact_inverse()
1810             .unwrap()
1811             .bitwise_eq(Single::from_f32(0.5))
1812     );
1813     assert!(
1814         "2.0"
1815             .parse::<Quad>()
1816             .unwrap()
1817             .get_exact_inverse()
1818             .unwrap()
1819             .bitwise_eq("0.5".parse::<Quad>().unwrap())
1820     );
1821     assert!(
1822         "2.0"
1823             .parse::<X87DoubleExtended>()
1824             .unwrap()
1825             .get_exact_inverse()
1826             .unwrap()
1827             .bitwise_eq("0.5".parse::<X87DoubleExtended>().unwrap())
1828     );
1829
1830     // FLT_MIN
1831     assert!(
1832         Single::from_f32(1.17549435e-38)
1833             .get_exact_inverse()
1834             .unwrap()
1835             .bitwise_eq(Single::from_f32(8.5070592e+37))
1836     );
1837
1838     // Large float, inverse is a denormal.
1839     assert!(Single::from_f32(1.7014118e38).get_exact_inverse().is_none());
1840     // Zero
1841     assert!(Double::from_f64(0.0).get_exact_inverse().is_none());
1842     // Denormalized float
1843     assert!(
1844         Single::from_f32(1.40129846e-45)
1845             .get_exact_inverse()
1846             .is_none()
1847     );
1848 }
1849
1850 #[test]
1851 fn round_to_integral() {
1852     let t = Double::from_f64(-0.5);
1853     assert_eq!(-0.0, t.round_to_integral(Round::TowardZero).value.to_f64());
1854     assert_eq!(
1855         -1.0,
1856         t.round_to_integral(Round::TowardNegative).value.to_f64()
1857     );
1858     assert_eq!(
1859         -0.0,
1860         t.round_to_integral(Round::TowardPositive).value.to_f64()
1861     );
1862     assert_eq!(
1863         -0.0,
1864         t.round_to_integral(Round::NearestTiesToEven).value.to_f64()
1865     );
1866
1867     let s = Double::from_f64(3.14);
1868     assert_eq!(3.0, s.round_to_integral(Round::TowardZero).value.to_f64());
1869     assert_eq!(
1870         3.0,
1871         s.round_to_integral(Round::TowardNegative).value.to_f64()
1872     );
1873     assert_eq!(
1874         4.0,
1875         s.round_to_integral(Round::TowardPositive).value.to_f64()
1876     );
1877     assert_eq!(
1878         3.0,
1879         s.round_to_integral(Round::NearestTiesToEven).value.to_f64()
1880     );
1881
1882     let r = Double::largest();
1883     assert_eq!(
1884         r.to_f64(),
1885         r.round_to_integral(Round::TowardZero).value.to_f64()
1886     );
1887     assert_eq!(
1888         r.to_f64(),
1889         r.round_to_integral(Round::TowardNegative).value.to_f64()
1890     );
1891     assert_eq!(
1892         r.to_f64(),
1893         r.round_to_integral(Round::TowardPositive).value.to_f64()
1894     );
1895     assert_eq!(
1896         r.to_f64(),
1897         r.round_to_integral(Round::NearestTiesToEven).value.to_f64()
1898     );
1899
1900     let p = Double::ZERO.round_to_integral(Round::TowardZero).value;
1901     assert_eq!(0.0, p.to_f64());
1902     let p = (-Double::ZERO).round_to_integral(Round::TowardZero).value;
1903     assert_eq!(-0.0, p.to_f64());
1904     let p = Double::NAN.round_to_integral(Round::TowardZero).value;
1905     assert!(p.to_f64().is_nan());
1906     let p = Double::INFINITY.round_to_integral(Round::TowardZero).value;
1907     assert!(p.to_f64().is_infinite() && p.to_f64() > 0.0);
1908     let p = (-Double::INFINITY)
1909         .round_to_integral(Round::TowardZero)
1910         .value;
1911     assert!(p.to_f64().is_infinite() && p.to_f64() < 0.0);
1912 }
1913
1914 #[test]
1915 fn is_integer() {
1916     let t = Double::from_f64(-0.0);
1917     assert!(t.is_integer());
1918     let t = Double::from_f64(3.14159);
1919     assert!(!t.is_integer());
1920     let t = Double::NAN;
1921     assert!(!t.is_integer());
1922     let t = Double::INFINITY;
1923     assert!(!t.is_integer());
1924     let t = -Double::INFINITY;
1925     assert!(!t.is_integer());
1926     let t = Double::largest();
1927     assert!(t.is_integer());
1928 }
1929
1930 #[test]
1931 fn largest() {
1932     assert_eq!(3.402823466e+38, Single::largest().to_f32());
1933     assert_eq!(1.7976931348623158e+308, Double::largest().to_f64());
1934 }
1935
1936 #[test]
1937 fn smallest() {
1938     let test = Single::SMALLEST;
1939     let expected = "0x0.000002p-126".parse::<Single>().unwrap();
1940     assert!(!test.is_negative());
1941     assert!(test.is_finite_non_zero());
1942     assert!(test.is_denormal());
1943     assert!(test.bitwise_eq(expected));
1944
1945     let test = -Single::SMALLEST;
1946     let expected = "-0x0.000002p-126".parse::<Single>().unwrap();
1947     assert!(test.is_negative());
1948     assert!(test.is_finite_non_zero());
1949     assert!(test.is_denormal());
1950     assert!(test.bitwise_eq(expected));
1951
1952     let test = Quad::SMALLEST;
1953     let expected = "0x0.0000000000000000000000000001p-16382"
1954         .parse::<Quad>()
1955         .unwrap();
1956     assert!(!test.is_negative());
1957     assert!(test.is_finite_non_zero());
1958     assert!(test.is_denormal());
1959     assert!(test.bitwise_eq(expected));
1960
1961     let test = -Quad::SMALLEST;
1962     let expected = "-0x0.0000000000000000000000000001p-16382"
1963         .parse::<Quad>()
1964         .unwrap();
1965     assert!(test.is_negative());
1966     assert!(test.is_finite_non_zero());
1967     assert!(test.is_denormal());
1968     assert!(test.bitwise_eq(expected));
1969 }
1970
1971 #[test]
1972 fn smallest_normalized() {
1973     let test = Single::smallest_normalized();
1974     let expected = "0x1p-126".parse::<Single>().unwrap();
1975     assert!(!test.is_negative());
1976     assert!(test.is_finite_non_zero());
1977     assert!(!test.is_denormal());
1978     assert!(test.bitwise_eq(expected));
1979
1980     let test = -Single::smallest_normalized();
1981     let expected = "-0x1p-126".parse::<Single>().unwrap();
1982     assert!(test.is_negative());
1983     assert!(test.is_finite_non_zero());
1984     assert!(!test.is_denormal());
1985     assert!(test.bitwise_eq(expected));
1986
1987     let test = Quad::smallest_normalized();
1988     let expected = "0x1p-16382".parse::<Quad>().unwrap();
1989     assert!(!test.is_negative());
1990     assert!(test.is_finite_non_zero());
1991     assert!(!test.is_denormal());
1992     assert!(test.bitwise_eq(expected));
1993
1994     let test = -Quad::smallest_normalized();
1995     let expected = "-0x1p-16382".parse::<Quad>().unwrap();
1996     assert!(test.is_negative());
1997     assert!(test.is_finite_non_zero());
1998     assert!(!test.is_denormal());
1999     assert!(test.bitwise_eq(expected));
2000 }
2001
2002 #[test]
2003 fn zero() {
2004     assert_eq!(0.0, Single::from_f32(0.0).to_f32());
2005     assert_eq!(-0.0, Single::from_f32(-0.0).to_f32());
2006     assert!(Single::from_f32(-0.0).is_negative());
2007
2008     assert_eq!(0.0, Double::from_f64(0.0).to_f64());
2009     assert_eq!(-0.0, Double::from_f64(-0.0).to_f64());
2010     assert!(Double::from_f64(-0.0).is_negative());
2011
2012     fn test<T: Float>(sign: bool, bits: u128) {
2013         let test = if sign { -T::ZERO } else { T::ZERO };
2014         let pattern = if sign { "-0x0p+0" } else { "0x0p+0" };
2015         let expected = pattern.parse::<T>().unwrap();
2016         assert!(test.is_zero());
2017         assert_eq!(sign, test.is_negative());
2018         assert!(test.bitwise_eq(expected));
2019         assert_eq!(bits, test.to_bits());
2020     }
2021     test::<Half>(false, 0);
2022     test::<Half>(true, 0x8000);
2023     test::<Single>(false, 0);
2024     test::<Single>(true, 0x80000000);
2025     test::<Double>(false, 0);
2026     test::<Double>(true, 0x8000000000000000);
2027     test::<Quad>(false, 0);
2028     test::<Quad>(true, 0x8000000000000000_0000000000000000);
2029     test::<X87DoubleExtended>(false, 0);
2030     test::<X87DoubleExtended>(true, 0x8000_0000000000000000);
2031 }
2032
2033 #[test]
2034 fn copy_sign() {
2035     assert!(Double::from_f64(-42.0).bitwise_eq(
2036         Double::from_f64(42.0).copy_sign(
2037             Double::from_f64(-1.0),
2038         ),
2039     ));
2040     assert!(Double::from_f64(42.0).bitwise_eq(
2041         Double::from_f64(-42.0).copy_sign(
2042             Double::from_f64(1.0),
2043         ),
2044     ));
2045     assert!(Double::from_f64(-42.0).bitwise_eq(
2046         Double::from_f64(-42.0).copy_sign(
2047             Double::from_f64(-1.0),
2048         ),
2049     ));
2050     assert!(Double::from_f64(42.0).bitwise_eq(
2051         Double::from_f64(42.0).copy_sign(
2052             Double::from_f64(1.0),
2053         ),
2054     ));
2055 }
2056
2057 #[test]
2058 fn convert() {
2059     let mut loses_info = false;
2060     let test = "1.0".parse::<Double>().unwrap();
2061     let test: Single = test.convert(&mut loses_info).value;
2062     assert_eq!(1.0, test.to_f32());
2063     assert!(!loses_info);
2064
2065     let mut test = "0x1p-53".parse::<X87DoubleExtended>().unwrap();
2066     let one = "1.0".parse::<X87DoubleExtended>().unwrap();
2067     test += one;
2068     let test: Double = test.convert(&mut loses_info).value;
2069     assert_eq!(1.0, test.to_f64());
2070     assert!(loses_info);
2071
2072     let mut test = "0x1p-53".parse::<Quad>().unwrap();
2073     let one = "1.0".parse::<Quad>().unwrap();
2074     test += one;
2075     let test: Double = test.convert(&mut loses_info).value;
2076     assert_eq!(1.0, test.to_f64());
2077     assert!(loses_info);
2078
2079     let test = "0xf.fffffffp+28".parse::<X87DoubleExtended>().unwrap();
2080     let test: Double = test.convert(&mut loses_info).value;
2081     assert_eq!(4294967295.0, test.to_f64());
2082     assert!(!loses_info);
2083
2084     let test = Single::snan(None);
2085     let x87_snan = X87DoubleExtended::snan(None);
2086     let test: X87DoubleExtended = test.convert(&mut loses_info).value;
2087     assert!(test.bitwise_eq(x87_snan));
2088     assert!(!loses_info);
2089
2090     let test = Single::qnan(None);
2091     let x87_qnan = X87DoubleExtended::qnan(None);
2092     let test: X87DoubleExtended = test.convert(&mut loses_info).value;
2093     assert!(test.bitwise_eq(x87_qnan));
2094     assert!(!loses_info);
2095
2096     let test = X87DoubleExtended::snan(None);
2097     let test: X87DoubleExtended = test.convert(&mut loses_info).value;
2098     assert!(test.bitwise_eq(x87_snan));
2099     assert!(!loses_info);
2100
2101     let test = X87DoubleExtended::qnan(None);
2102     let test: X87DoubleExtended = test.convert(&mut loses_info).value;
2103     assert!(test.bitwise_eq(x87_qnan));
2104     assert!(!loses_info);
2105 }
2106
2107 #[test]
2108 fn is_negative() {
2109     let t = "0x1p+0".parse::<Single>().unwrap();
2110     assert!(!t.is_negative());
2111     let t = "-0x1p+0".parse::<Single>().unwrap();
2112     assert!(t.is_negative());
2113
2114     assert!(!Single::INFINITY.is_negative());
2115     assert!((-Single::INFINITY).is_negative());
2116
2117     assert!(!Single::ZERO.is_negative());
2118     assert!((-Single::ZERO).is_negative());
2119
2120     assert!(!Single::NAN.is_negative());
2121     assert!((-Single::NAN).is_negative());
2122
2123     assert!(!Single::snan(None).is_negative());
2124     assert!((-Single::snan(None)).is_negative());
2125 }
2126
2127 #[test]
2128 fn is_normal() {
2129     let t = "0x1p+0".parse::<Single>().unwrap();
2130     assert!(t.is_normal());
2131
2132     assert!(!Single::INFINITY.is_normal());
2133     assert!(!Single::ZERO.is_normal());
2134     assert!(!Single::NAN.is_normal());
2135     assert!(!Single::snan(None).is_normal());
2136     assert!(!"0x1p-149".parse::<Single>().unwrap().is_normal());
2137 }
2138
2139 #[test]
2140 fn is_finite() {
2141     let t = "0x1p+0".parse::<Single>().unwrap();
2142     assert!(t.is_finite());
2143     assert!(!Single::INFINITY.is_finite());
2144     assert!(Single::ZERO.is_finite());
2145     assert!(!Single::NAN.is_finite());
2146     assert!(!Single::snan(None).is_finite());
2147     assert!("0x1p-149".parse::<Single>().unwrap().is_finite());
2148 }
2149
2150 #[test]
2151 fn is_infinite() {
2152     let t = "0x1p+0".parse::<Single>().unwrap();
2153     assert!(!t.is_infinite());
2154     assert!(Single::INFINITY.is_infinite());
2155     assert!(!Single::ZERO.is_infinite());
2156     assert!(!Single::NAN.is_infinite());
2157     assert!(!Single::snan(None).is_infinite());
2158     assert!(!"0x1p-149".parse::<Single>().unwrap().is_infinite());
2159 }
2160
2161 #[test]
2162 fn is_nan() {
2163     let t = "0x1p+0".parse::<Single>().unwrap();
2164     assert!(!t.is_nan());
2165     assert!(!Single::INFINITY.is_nan());
2166     assert!(!Single::ZERO.is_nan());
2167     assert!(Single::NAN.is_nan());
2168     assert!(Single::snan(None).is_nan());
2169     assert!(!"0x1p-149".parse::<Single>().unwrap().is_nan());
2170 }
2171
2172 #[test]
2173 fn is_finite_non_zero() {
2174     // Test positive/negative normal value.
2175     assert!("0x1p+0".parse::<Single>().unwrap().is_finite_non_zero());
2176     assert!("-0x1p+0".parse::<Single>().unwrap().is_finite_non_zero());
2177
2178     // Test positive/negative denormal value.
2179     assert!("0x1p-149".parse::<Single>().unwrap().is_finite_non_zero());
2180     assert!("-0x1p-149".parse::<Single>().unwrap().is_finite_non_zero());
2181
2182     // Test +/- Infinity.
2183     assert!(!Single::INFINITY.is_finite_non_zero());
2184     assert!(!(-Single::INFINITY).is_finite_non_zero());
2185
2186     // Test +/- Zero.
2187     assert!(!Single::ZERO.is_finite_non_zero());
2188     assert!(!(-Single::ZERO).is_finite_non_zero());
2189
2190     // Test +/- qNaN. +/- don't mean anything with qNaN but paranoia can't hurt in
2191     // this instance.
2192     assert!(!Single::NAN.is_finite_non_zero());
2193     assert!(!(-Single::NAN).is_finite_non_zero());
2194
2195     // Test +/- sNaN. +/- don't mean anything with sNaN but paranoia can't hurt in
2196     // this instance.
2197     assert!(!Single::snan(None).is_finite_non_zero());
2198     assert!(!(-Single::snan(None)).is_finite_non_zero());
2199 }
2200
2201 #[test]
2202 fn add() {
2203     // Test Special Cases against each other and normal values.
2204
2205     // FIXMES/NOTES:
2206     // 1. Since we perform only default exception handling all operations with
2207     // signaling NaNs should have a result that is a quiet NaN. Currently they
2208     // return sNaN.
2209
2210     let p_inf = Single::INFINITY;
2211     let m_inf = -Single::INFINITY;
2212     let p_zero = Single::ZERO;
2213     let m_zero = -Single::ZERO;
2214     let qnan = Single::NAN;
2215     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
2216     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
2217     let p_largest_value = Single::largest();
2218     let m_largest_value = -Single::largest();
2219     let p_smallest_value = Single::SMALLEST;
2220     let m_smallest_value = -Single::SMALLEST;
2221     let p_smallest_normalized = Single::smallest_normalized();
2222     let m_smallest_normalized = -Single::smallest_normalized();
2223
2224     let overflow_status = Status::OVERFLOW | Status::INEXACT;
2225
2226     let special_cases = [
2227         (p_inf, p_inf, "inf", Status::OK, Category::Infinity),
2228         (p_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2229         (p_inf, p_zero, "inf", Status::OK, Category::Infinity),
2230         (p_inf, m_zero, "inf", Status::OK, Category::Infinity),
2231         (p_inf, qnan, "nan", Status::OK, Category::NaN),
2232         /*
2233 // See Note 1.
2234 (p_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
2235         */
2236         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
2237         (p_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
2238         (
2239             p_inf,
2240             p_largest_value,
2241             "inf",
2242             Status::OK,
2243             Category::Infinity,
2244         ),
2245         (
2246             p_inf,
2247             m_largest_value,
2248             "inf",
2249             Status::OK,
2250             Category::Infinity,
2251         ),
2252         (
2253             p_inf,
2254             p_smallest_value,
2255             "inf",
2256             Status::OK,
2257             Category::Infinity,
2258         ),
2259         (
2260             p_inf,
2261             m_smallest_value,
2262             "inf",
2263             Status::OK,
2264             Category::Infinity,
2265         ),
2266         (
2267             p_inf,
2268             p_smallest_normalized,
2269             "inf",
2270             Status::OK,
2271             Category::Infinity,
2272         ),
2273         (
2274             p_inf,
2275             m_smallest_normalized,
2276             "inf",
2277             Status::OK,
2278             Category::Infinity,
2279         ),
2280         (m_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2281         (m_inf, m_inf, "-inf", Status::OK, Category::Infinity),
2282         (m_inf, p_zero, "-inf", Status::OK, Category::Infinity),
2283         (m_inf, m_zero, "-inf", Status::OK, Category::Infinity),
2284         (m_inf, qnan, "nan", Status::OK, Category::NaN),
2285         /*
2286 // See Note 1.
2287 (m_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
2288         */
2289         (
2290             m_inf,
2291             p_normal_value,
2292             "-inf",
2293             Status::OK,
2294             Category::Infinity,
2295         ),
2296         (
2297             m_inf,
2298             m_normal_value,
2299             "-inf",
2300             Status::OK,
2301             Category::Infinity,
2302         ),
2303         (
2304             m_inf,
2305             p_largest_value,
2306             "-inf",
2307             Status::OK,
2308             Category::Infinity,
2309         ),
2310         (
2311             m_inf,
2312             m_largest_value,
2313             "-inf",
2314             Status::OK,
2315             Category::Infinity,
2316         ),
2317         (
2318             m_inf,
2319             p_smallest_value,
2320             "-inf",
2321             Status::OK,
2322             Category::Infinity,
2323         ),
2324         (
2325             m_inf,
2326             m_smallest_value,
2327             "-inf",
2328             Status::OK,
2329             Category::Infinity,
2330         ),
2331         (
2332             m_inf,
2333             p_smallest_normalized,
2334             "-inf",
2335             Status::OK,
2336             Category::Infinity,
2337         ),
2338         (
2339             m_inf,
2340             m_smallest_normalized,
2341             "-inf",
2342             Status::OK,
2343             Category::Infinity,
2344         ),
2345         (p_zero, p_inf, "inf", Status::OK, Category::Infinity),
2346         (p_zero, m_inf, "-inf", Status::OK, Category::Infinity),
2347         (p_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
2348         (p_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
2349         (p_zero, qnan, "nan", Status::OK, Category::NaN),
2350         /*
2351 // See Note 1.
2352 (p_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
2353         */
2354         (
2355             p_zero,
2356             p_normal_value,
2357             "0x1p+0",
2358             Status::OK,
2359             Category::Normal,
2360         ),
2361         (
2362             p_zero,
2363             m_normal_value,
2364             "-0x1p+0",
2365             Status::OK,
2366             Category::Normal,
2367         ),
2368         (
2369             p_zero,
2370             p_largest_value,
2371             "0x1.fffffep+127",
2372             Status::OK,
2373             Category::Normal,
2374         ),
2375         (
2376             p_zero,
2377             m_largest_value,
2378             "-0x1.fffffep+127",
2379             Status::OK,
2380             Category::Normal,
2381         ),
2382         (
2383             p_zero,
2384             p_smallest_value,
2385             "0x1p-149",
2386             Status::OK,
2387             Category::Normal,
2388         ),
2389         (
2390             p_zero,
2391             m_smallest_value,
2392             "-0x1p-149",
2393             Status::OK,
2394             Category::Normal,
2395         ),
2396         (
2397             p_zero,
2398             p_smallest_normalized,
2399             "0x1p-126",
2400             Status::OK,
2401             Category::Normal,
2402         ),
2403         (
2404             p_zero,
2405             m_smallest_normalized,
2406             "-0x1p-126",
2407             Status::OK,
2408             Category::Normal,
2409         ),
2410         (m_zero, p_inf, "inf", Status::OK, Category::Infinity),
2411         (m_zero, m_inf, "-inf", Status::OK, Category::Infinity),
2412         (m_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
2413         (m_zero, m_zero, "-0x0p+0", Status::OK, Category::Zero),
2414         (m_zero, qnan, "nan", Status::OK, Category::NaN),
2415         /*
2416 // See Note 1.
2417 (m_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
2418         */
2419         (
2420             m_zero,
2421             p_normal_value,
2422             "0x1p+0",
2423             Status::OK,
2424             Category::Normal,
2425         ),
2426         (
2427             m_zero,
2428             m_normal_value,
2429             "-0x1p+0",
2430             Status::OK,
2431             Category::Normal,
2432         ),
2433         (
2434             m_zero,
2435             p_largest_value,
2436             "0x1.fffffep+127",
2437             Status::OK,
2438             Category::Normal,
2439         ),
2440         (
2441             m_zero,
2442             m_largest_value,
2443             "-0x1.fffffep+127",
2444             Status::OK,
2445             Category::Normal,
2446         ),
2447         (
2448             m_zero,
2449             p_smallest_value,
2450             "0x1p-149",
2451             Status::OK,
2452             Category::Normal,
2453         ),
2454         (
2455             m_zero,
2456             m_smallest_value,
2457             "-0x1p-149",
2458             Status::OK,
2459             Category::Normal,
2460         ),
2461         (
2462             m_zero,
2463             p_smallest_normalized,
2464             "0x1p-126",
2465             Status::OK,
2466             Category::Normal,
2467         ),
2468         (
2469             m_zero,
2470             m_smallest_normalized,
2471             "-0x1p-126",
2472             Status::OK,
2473             Category::Normal,
2474         ),
2475         (qnan, p_inf, "nan", Status::OK, Category::NaN),
2476         (qnan, m_inf, "nan", Status::OK, Category::NaN),
2477         (qnan, p_zero, "nan", Status::OK, Category::NaN),
2478         (qnan, m_zero, "nan", Status::OK, Category::NaN),
2479         (qnan, qnan, "nan", Status::OK, Category::NaN),
2480         /*
2481 // See Note 1.
2482 (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
2483         */
2484         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
2485         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
2486         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
2487         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
2488         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
2489         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
2490         (
2491             qnan,
2492             p_smallest_normalized,
2493             "nan",
2494             Status::OK,
2495             Category::NaN,
2496         ),
2497         (
2498             qnan,
2499             m_smallest_normalized,
2500             "nan",
2501             Status::OK,
2502             Category::NaN,
2503         ),
2504         /*
2505 // See Note 1.
2506 (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2507 (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2508 (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2509 (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2510 (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
2511 (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
2512 (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2513 (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2514 (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2515 (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2516 (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2517 (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2518 (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2519 (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2520         */
2521         (p_normal_value, p_inf, "inf", Status::OK, Category::Infinity),
2522         (
2523             p_normal_value,
2524             m_inf,
2525             "-inf",
2526             Status::OK,
2527             Category::Infinity,
2528         ),
2529         (
2530             p_normal_value,
2531             p_zero,
2532             "0x1p+0",
2533             Status::OK,
2534             Category::Normal,
2535         ),
2536         (
2537             p_normal_value,
2538             m_zero,
2539             "0x1p+0",
2540             Status::OK,
2541             Category::Normal,
2542         ),
2543         (p_normal_value, qnan, "nan", Status::OK, Category::NaN),
2544         /*
2545 // See Note 1.
2546 (p_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2547         */
2548         (
2549             p_normal_value,
2550             p_normal_value,
2551             "0x1p+1",
2552             Status::OK,
2553             Category::Normal,
2554         ),
2555         (
2556             p_normal_value,
2557             m_normal_value,
2558             "0x0p+0",
2559             Status::OK,
2560             Category::Zero,
2561         ),
2562         (
2563             p_normal_value,
2564             p_largest_value,
2565             "0x1.fffffep+127",
2566             Status::INEXACT,
2567             Category::Normal,
2568         ),
2569         (
2570             p_normal_value,
2571             m_largest_value,
2572             "-0x1.fffffep+127",
2573             Status::INEXACT,
2574             Category::Normal,
2575         ),
2576         (
2577             p_normal_value,
2578             p_smallest_value,
2579             "0x1p+0",
2580             Status::INEXACT,
2581             Category::Normal,
2582         ),
2583         (
2584             p_normal_value,
2585             m_smallest_value,
2586             "0x1p+0",
2587             Status::INEXACT,
2588             Category::Normal,
2589         ),
2590         (
2591             p_normal_value,
2592             p_smallest_normalized,
2593             "0x1p+0",
2594             Status::INEXACT,
2595             Category::Normal,
2596         ),
2597         (
2598             p_normal_value,
2599             m_smallest_normalized,
2600             "0x1p+0",
2601             Status::INEXACT,
2602             Category::Normal,
2603         ),
2604         (m_normal_value, p_inf, "inf", Status::OK, Category::Infinity),
2605         (
2606             m_normal_value,
2607             m_inf,
2608             "-inf",
2609             Status::OK,
2610             Category::Infinity,
2611         ),
2612         (
2613             m_normal_value,
2614             p_zero,
2615             "-0x1p+0",
2616             Status::OK,
2617             Category::Normal,
2618         ),
2619         (
2620             m_normal_value,
2621             m_zero,
2622             "-0x1p+0",
2623             Status::OK,
2624             Category::Normal,
2625         ),
2626         (m_normal_value, qnan, "nan", Status::OK, Category::NaN),
2627         /*
2628 // See Note 1.
2629 (m_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2630         */
2631         (
2632             m_normal_value,
2633             p_normal_value,
2634             "0x0p+0",
2635             Status::OK,
2636             Category::Zero,
2637         ),
2638         (
2639             m_normal_value,
2640             m_normal_value,
2641             "-0x1p+1",
2642             Status::OK,
2643             Category::Normal,
2644         ),
2645         (
2646             m_normal_value,
2647             p_largest_value,
2648             "0x1.fffffep+127",
2649             Status::INEXACT,
2650             Category::Normal,
2651         ),
2652         (
2653             m_normal_value,
2654             m_largest_value,
2655             "-0x1.fffffep+127",
2656             Status::INEXACT,
2657             Category::Normal,
2658         ),
2659         (
2660             m_normal_value,
2661             p_smallest_value,
2662             "-0x1p+0",
2663             Status::INEXACT,
2664             Category::Normal,
2665         ),
2666         (
2667             m_normal_value,
2668             m_smallest_value,
2669             "-0x1p+0",
2670             Status::INEXACT,
2671             Category::Normal,
2672         ),
2673         (
2674             m_normal_value,
2675             p_smallest_normalized,
2676             "-0x1p+0",
2677             Status::INEXACT,
2678             Category::Normal,
2679         ),
2680         (
2681             m_normal_value,
2682             m_smallest_normalized,
2683             "-0x1p+0",
2684             Status::INEXACT,
2685             Category::Normal,
2686         ),
2687         (
2688             p_largest_value,
2689             p_inf,
2690             "inf",
2691             Status::OK,
2692             Category::Infinity,
2693         ),
2694         (
2695             p_largest_value,
2696             m_inf,
2697             "-inf",
2698             Status::OK,
2699             Category::Infinity,
2700         ),
2701         (
2702             p_largest_value,
2703             p_zero,
2704             "0x1.fffffep+127",
2705             Status::OK,
2706             Category::Normal,
2707         ),
2708         (
2709             p_largest_value,
2710             m_zero,
2711             "0x1.fffffep+127",
2712             Status::OK,
2713             Category::Normal,
2714         ),
2715         (p_largest_value, qnan, "nan", Status::OK, Category::NaN),
2716         /*
2717 // See Note 1.
2718 (p_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2719         */
2720         (
2721             p_largest_value,
2722             p_normal_value,
2723             "0x1.fffffep+127",
2724             Status::INEXACT,
2725             Category::Normal,
2726         ),
2727         (
2728             p_largest_value,
2729             m_normal_value,
2730             "0x1.fffffep+127",
2731             Status::INEXACT,
2732             Category::Normal,
2733         ),
2734         (
2735             p_largest_value,
2736             p_largest_value,
2737             "inf",
2738             overflow_status,
2739             Category::Infinity,
2740         ),
2741         (
2742             p_largest_value,
2743             m_largest_value,
2744             "0x0p+0",
2745             Status::OK,
2746             Category::Zero,
2747         ),
2748         (
2749             p_largest_value,
2750             p_smallest_value,
2751             "0x1.fffffep+127",
2752             Status::INEXACT,
2753             Category::Normal,
2754         ),
2755         (
2756             p_largest_value,
2757             m_smallest_value,
2758             "0x1.fffffep+127",
2759             Status::INEXACT,
2760             Category::Normal,
2761         ),
2762         (
2763             p_largest_value,
2764             p_smallest_normalized,
2765             "0x1.fffffep+127",
2766             Status::INEXACT,
2767             Category::Normal,
2768         ),
2769         (
2770             p_largest_value,
2771             m_smallest_normalized,
2772             "0x1.fffffep+127",
2773             Status::INEXACT,
2774             Category::Normal,
2775         ),
2776         (
2777             m_largest_value,
2778             p_inf,
2779             "inf",
2780             Status::OK,
2781             Category::Infinity,
2782         ),
2783         (
2784             m_largest_value,
2785             m_inf,
2786             "-inf",
2787             Status::OK,
2788             Category::Infinity,
2789         ),
2790         (
2791             m_largest_value,
2792             p_zero,
2793             "-0x1.fffffep+127",
2794             Status::OK,
2795             Category::Normal,
2796         ),
2797         (
2798             m_largest_value,
2799             m_zero,
2800             "-0x1.fffffep+127",
2801             Status::OK,
2802             Category::Normal,
2803         ),
2804         (m_largest_value, qnan, "nan", Status::OK, Category::NaN),
2805         /*
2806 // See Note 1.
2807 (m_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2808         */
2809         (
2810             m_largest_value,
2811             p_normal_value,
2812             "-0x1.fffffep+127",
2813             Status::INEXACT,
2814             Category::Normal,
2815         ),
2816         (
2817             m_largest_value,
2818             m_normal_value,
2819             "-0x1.fffffep+127",
2820             Status::INEXACT,
2821             Category::Normal,
2822         ),
2823         (
2824             m_largest_value,
2825             p_largest_value,
2826             "0x0p+0",
2827             Status::OK,
2828             Category::Zero,
2829         ),
2830         (
2831             m_largest_value,
2832             m_largest_value,
2833             "-inf",
2834             overflow_status,
2835             Category::Infinity,
2836         ),
2837         (
2838             m_largest_value,
2839             p_smallest_value,
2840             "-0x1.fffffep+127",
2841             Status::INEXACT,
2842             Category::Normal,
2843         ),
2844         (
2845             m_largest_value,
2846             m_smallest_value,
2847             "-0x1.fffffep+127",
2848             Status::INEXACT,
2849             Category::Normal,
2850         ),
2851         (
2852             m_largest_value,
2853             p_smallest_normalized,
2854             "-0x1.fffffep+127",
2855             Status::INEXACT,
2856             Category::Normal,
2857         ),
2858         (
2859             m_largest_value,
2860             m_smallest_normalized,
2861             "-0x1.fffffep+127",
2862             Status::INEXACT,
2863             Category::Normal,
2864         ),
2865         (
2866             p_smallest_value,
2867             p_inf,
2868             "inf",
2869             Status::OK,
2870             Category::Infinity,
2871         ),
2872         (
2873             p_smallest_value,
2874             m_inf,
2875             "-inf",
2876             Status::OK,
2877             Category::Infinity,
2878         ),
2879         (
2880             p_smallest_value,
2881             p_zero,
2882             "0x1p-149",
2883             Status::OK,
2884             Category::Normal,
2885         ),
2886         (
2887             p_smallest_value,
2888             m_zero,
2889             "0x1p-149",
2890             Status::OK,
2891             Category::Normal,
2892         ),
2893         (p_smallest_value, qnan, "nan", Status::OK, Category::NaN),
2894         /*
2895 // See Note 1.
2896 (p_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2897         */
2898         (
2899             p_smallest_value,
2900             p_normal_value,
2901             "0x1p+0",
2902             Status::INEXACT,
2903             Category::Normal,
2904         ),
2905         (
2906             p_smallest_value,
2907             m_normal_value,
2908             "-0x1p+0",
2909             Status::INEXACT,
2910             Category::Normal,
2911         ),
2912         (
2913             p_smallest_value,
2914             p_largest_value,
2915             "0x1.fffffep+127",
2916             Status::INEXACT,
2917             Category::Normal,
2918         ),
2919         (
2920             p_smallest_value,
2921             m_largest_value,
2922             "-0x1.fffffep+127",
2923             Status::INEXACT,
2924             Category::Normal,
2925         ),
2926         (
2927             p_smallest_value,
2928             p_smallest_value,
2929             "0x1p-148",
2930             Status::OK,
2931             Category::Normal,
2932         ),
2933         (
2934             p_smallest_value,
2935             m_smallest_value,
2936             "0x0p+0",
2937             Status::OK,
2938             Category::Zero,
2939         ),
2940         (
2941             p_smallest_value,
2942             p_smallest_normalized,
2943             "0x1.000002p-126",
2944             Status::OK,
2945             Category::Normal,
2946         ),
2947         (
2948             p_smallest_value,
2949             m_smallest_normalized,
2950             "-0x1.fffffcp-127",
2951             Status::OK,
2952             Category::Normal,
2953         ),
2954         (
2955             m_smallest_value,
2956             p_inf,
2957             "inf",
2958             Status::OK,
2959             Category::Infinity,
2960         ),
2961         (
2962             m_smallest_value,
2963             m_inf,
2964             "-inf",
2965             Status::OK,
2966             Category::Infinity,
2967         ),
2968         (
2969             m_smallest_value,
2970             p_zero,
2971             "-0x1p-149",
2972             Status::OK,
2973             Category::Normal,
2974         ),
2975         (
2976             m_smallest_value,
2977             m_zero,
2978             "-0x1p-149",
2979             Status::OK,
2980             Category::Normal,
2981         ),
2982         (m_smallest_value, qnan, "nan", Status::OK, Category::NaN),
2983         /*
2984 // See Note 1.
2985 (m_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2986         */
2987         (
2988             m_smallest_value,
2989             p_normal_value,
2990             "0x1p+0",
2991             Status::INEXACT,
2992             Category::Normal,
2993         ),
2994         (
2995             m_smallest_value,
2996             m_normal_value,
2997             "-0x1p+0",
2998             Status::INEXACT,
2999             Category::Normal,
3000         ),
3001         (
3002             m_smallest_value,
3003             p_largest_value,
3004             "0x1.fffffep+127",
3005             Status::INEXACT,
3006             Category::Normal,
3007         ),
3008         (
3009             m_smallest_value,
3010             m_largest_value,
3011             "-0x1.fffffep+127",
3012             Status::INEXACT,
3013             Category::Normal,
3014         ),
3015         (
3016             m_smallest_value,
3017             p_smallest_value,
3018             "0x0p+0",
3019             Status::OK,
3020             Category::Zero,
3021         ),
3022         (
3023             m_smallest_value,
3024             m_smallest_value,
3025             "-0x1p-148",
3026             Status::OK,
3027             Category::Normal,
3028         ),
3029         (
3030             m_smallest_value,
3031             p_smallest_normalized,
3032             "0x1.fffffcp-127",
3033             Status::OK,
3034             Category::Normal,
3035         ),
3036         (
3037             m_smallest_value,
3038             m_smallest_normalized,
3039             "-0x1.000002p-126",
3040             Status::OK,
3041             Category::Normal,
3042         ),
3043         (
3044             p_smallest_normalized,
3045             p_inf,
3046             "inf",
3047             Status::OK,
3048             Category::Infinity,
3049         ),
3050         (
3051             p_smallest_normalized,
3052             m_inf,
3053             "-inf",
3054             Status::OK,
3055             Category::Infinity,
3056         ),
3057         (
3058             p_smallest_normalized,
3059             p_zero,
3060             "0x1p-126",
3061             Status::OK,
3062             Category::Normal,
3063         ),
3064         (
3065             p_smallest_normalized,
3066             m_zero,
3067             "0x1p-126",
3068             Status::OK,
3069             Category::Normal,
3070         ),
3071         (
3072             p_smallest_normalized,
3073             qnan,
3074             "nan",
3075             Status::OK,
3076             Category::NaN,
3077         ),
3078         /*
3079 // See Note 1.
3080 (p_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
3081         */
3082         (
3083             p_smallest_normalized,
3084             p_normal_value,
3085             "0x1p+0",
3086             Status::INEXACT,
3087             Category::Normal,
3088         ),
3089         (
3090             p_smallest_normalized,
3091             m_normal_value,
3092             "-0x1p+0",
3093             Status::INEXACT,
3094             Category::Normal,
3095         ),
3096         (
3097             p_smallest_normalized,
3098             p_largest_value,
3099             "0x1.fffffep+127",
3100             Status::INEXACT,
3101             Category::Normal,
3102         ),
3103         (
3104             p_smallest_normalized,
3105             m_largest_value,
3106             "-0x1.fffffep+127",
3107             Status::INEXACT,
3108             Category::Normal,
3109         ),
3110         (
3111             p_smallest_normalized,
3112             p_smallest_value,
3113             "0x1.000002p-126",
3114             Status::OK,
3115             Category::Normal,
3116         ),
3117         (
3118             p_smallest_normalized,
3119             m_smallest_value,
3120             "0x1.fffffcp-127",
3121             Status::OK,
3122             Category::Normal,
3123         ),
3124         (
3125             p_smallest_normalized,
3126             p_smallest_normalized,
3127             "0x1p-125",
3128             Status::OK,
3129             Category::Normal,
3130         ),
3131         (
3132             p_smallest_normalized,
3133             m_smallest_normalized,
3134             "0x0p+0",
3135             Status::OK,
3136             Category::Zero,
3137         ),
3138         (
3139             m_smallest_normalized,
3140             p_inf,
3141             "inf",
3142             Status::OK,
3143             Category::Infinity,
3144         ),
3145         (
3146             m_smallest_normalized,
3147             m_inf,
3148             "-inf",
3149             Status::OK,
3150             Category::Infinity,
3151         ),
3152         (
3153             m_smallest_normalized,
3154             p_zero,
3155             "-0x1p-126",
3156             Status::OK,
3157             Category::Normal,
3158         ),
3159         (
3160             m_smallest_normalized,
3161             m_zero,
3162             "-0x1p-126",
3163             Status::OK,
3164             Category::Normal,
3165         ),
3166         (
3167             m_smallest_normalized,
3168             qnan,
3169             "nan",
3170             Status::OK,
3171             Category::NaN,
3172         ),
3173         /*
3174 // See Note 1.
3175 (m_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
3176         */
3177         (
3178             m_smallest_normalized,
3179             p_normal_value,
3180             "0x1p+0",
3181             Status::INEXACT,
3182             Category::Normal,
3183         ),
3184         (
3185             m_smallest_normalized,
3186             m_normal_value,
3187             "-0x1p+0",
3188             Status::INEXACT,
3189             Category::Normal,
3190         ),
3191         (
3192             m_smallest_normalized,
3193             p_largest_value,
3194             "0x1.fffffep+127",
3195             Status::INEXACT,
3196             Category::Normal,
3197         ),
3198         (
3199             m_smallest_normalized,
3200             m_largest_value,
3201             "-0x1.fffffep+127",
3202             Status::INEXACT,
3203             Category::Normal,
3204         ),
3205         (
3206             m_smallest_normalized,
3207             p_smallest_value,
3208             "-0x1.fffffcp-127",
3209             Status::OK,
3210             Category::Normal,
3211         ),
3212         (
3213             m_smallest_normalized,
3214             m_smallest_value,
3215             "-0x1.000002p-126",
3216             Status::OK,
3217             Category::Normal,
3218         ),
3219         (
3220             m_smallest_normalized,
3221             p_smallest_normalized,
3222             "0x0p+0",
3223             Status::OK,
3224             Category::Zero,
3225         ),
3226         (
3227             m_smallest_normalized,
3228             m_smallest_normalized,
3229             "-0x1p-125",
3230             Status::OK,
3231             Category::Normal,
3232         ),
3233     ];
3234
3235     for &(x, y, e_result, e_status, e_category) in &special_cases[..] {
3236         let status;
3237         let result = unpack!(status=, x + y);
3238         assert_eq!(status, e_status);
3239         assert_eq!(result.category(), e_category);
3240         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
3241     }
3242 }
3243
3244 #[test]
3245 fn subtract() {
3246     // Test Special Cases against each other and normal values.
3247
3248     // FIXMES/NOTES:
3249     // 1. Since we perform only default exception handling all operations with
3250     // signaling NaNs should have a result that is a quiet NaN. Currently they
3251     // return sNaN.
3252
3253     let p_inf = Single::INFINITY;
3254     let m_inf = -Single::INFINITY;
3255     let p_zero = Single::ZERO;
3256     let m_zero = -Single::ZERO;
3257     let qnan = Single::NAN;
3258     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
3259     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
3260     let p_largest_value = Single::largest();
3261     let m_largest_value = -Single::largest();
3262     let p_smallest_value = Single::SMALLEST;
3263     let m_smallest_value = -Single::SMALLEST;
3264     let p_smallest_normalized = Single::smallest_normalized();
3265     let m_smallest_normalized = -Single::smallest_normalized();
3266
3267     let overflow_status = Status::OVERFLOW | Status::INEXACT;
3268
3269     let special_cases = [
3270         (p_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
3271         (p_inf, m_inf, "inf", Status::OK, Category::Infinity),
3272         (p_inf, p_zero, "inf", Status::OK, Category::Infinity),
3273         (p_inf, m_zero, "inf", Status::OK, Category::Infinity),
3274         (p_inf, qnan, "-nan", Status::OK, Category::NaN),
3275         /*
3276 // See Note 1.
3277 (p_inf, snan, "-nan", Status::INVALID_OP, Category::NaN),
3278         */
3279         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
3280         (p_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
3281         (
3282             p_inf,
3283             p_largest_value,
3284             "inf",
3285             Status::OK,
3286             Category::Infinity,
3287         ),
3288         (
3289             p_inf,
3290             m_largest_value,
3291             "inf",
3292             Status::OK,
3293             Category::Infinity,
3294         ),
3295         (
3296             p_inf,
3297             p_smallest_value,
3298             "inf",
3299             Status::OK,
3300             Category::Infinity,
3301         ),
3302         (
3303             p_inf,
3304             m_smallest_value,
3305             "inf",
3306             Status::OK,
3307             Category::Infinity,
3308         ),
3309         (
3310             p_inf,
3311             p_smallest_normalized,
3312             "inf",
3313             Status::OK,
3314             Category::Infinity,
3315         ),
3316         (
3317             p_inf,
3318             m_smallest_normalized,
3319             "inf",
3320             Status::OK,
3321             Category::Infinity,
3322         ),
3323         (m_inf, p_inf, "-inf", Status::OK, Category::Infinity),
3324         (m_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
3325         (m_inf, p_zero, "-inf", Status::OK, Category::Infinity),
3326         (m_inf, m_zero, "-inf", Status::OK, Category::Infinity),
3327         (m_inf, qnan, "-nan", Status::OK, Category::NaN),
3328         /*
3329 // See Note 1.
3330 (m_inf, snan, "-nan", Status::INVALID_OP, Category::NaN),
3331         */
3332         (
3333             m_inf,
3334             p_normal_value,
3335             "-inf",
3336             Status::OK,
3337             Category::Infinity,
3338         ),
3339         (
3340             m_inf,
3341             m_normal_value,
3342             "-inf",
3343             Status::OK,
3344             Category::Infinity,
3345         ),
3346         (
3347             m_inf,
3348             p_largest_value,
3349             "-inf",
3350             Status::OK,
3351             Category::Infinity,
3352         ),
3353         (
3354             m_inf,
3355             m_largest_value,
3356             "-inf",
3357             Status::OK,
3358             Category::Infinity,
3359         ),
3360         (
3361             m_inf,
3362             p_smallest_value,
3363             "-inf",
3364             Status::OK,
3365             Category::Infinity,
3366         ),
3367         (
3368             m_inf,
3369             m_smallest_value,
3370             "-inf",
3371             Status::OK,
3372             Category::Infinity,
3373         ),
3374         (
3375             m_inf,
3376             p_smallest_normalized,
3377             "-inf",
3378             Status::OK,
3379             Category::Infinity,
3380         ),
3381         (
3382             m_inf,
3383             m_smallest_normalized,
3384             "-inf",
3385             Status::OK,
3386             Category::Infinity,
3387         ),
3388         (p_zero, p_inf, "-inf", Status::OK, Category::Infinity),
3389         (p_zero, m_inf, "inf", Status::OK, Category::Infinity),
3390         (p_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
3391         (p_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
3392         (p_zero, qnan, "-nan", Status::OK, Category::NaN),
3393         /*
3394 // See Note 1.
3395 (p_zero, snan, "-nan", Status::INVALID_OP, Category::NaN),
3396         */
3397         (
3398             p_zero,
3399             p_normal_value,
3400             "-0x1p+0",
3401             Status::OK,
3402             Category::Normal,
3403         ),
3404         (
3405             p_zero,
3406             m_normal_value,
3407             "0x1p+0",
3408             Status::OK,
3409             Category::Normal,
3410         ),
3411         (
3412             p_zero,
3413             p_largest_value,
3414             "-0x1.fffffep+127",
3415             Status::OK,
3416             Category::Normal,
3417         ),
3418         (
3419             p_zero,
3420             m_largest_value,
3421             "0x1.fffffep+127",
3422             Status::OK,
3423             Category::Normal,
3424         ),
3425         (
3426             p_zero,
3427             p_smallest_value,
3428             "-0x1p-149",
3429             Status::OK,
3430             Category::Normal,
3431         ),
3432         (
3433             p_zero,
3434             m_smallest_value,
3435             "0x1p-149",
3436             Status::OK,
3437             Category::Normal,
3438         ),
3439         (
3440             p_zero,
3441             p_smallest_normalized,
3442             "-0x1p-126",
3443             Status::OK,
3444             Category::Normal,
3445         ),
3446         (
3447             p_zero,
3448             m_smallest_normalized,
3449             "0x1p-126",
3450             Status::OK,
3451             Category::Normal,
3452         ),
3453         (m_zero, p_inf, "-inf", Status::OK, Category::Infinity),
3454         (m_zero, m_inf, "inf", Status::OK, Category::Infinity),
3455         (m_zero, p_zero, "-0x0p+0", Status::OK, Category::Zero),
3456         (m_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
3457         (m_zero, qnan, "-nan", Status::OK, Category::NaN),
3458         /*
3459 // See Note 1.
3460 (m_zero, snan, "-nan", Status::INVALID_OP, Category::NaN),
3461         */
3462         (
3463             m_zero,
3464             p_normal_value,
3465             "-0x1p+0",
3466             Status::OK,
3467             Category::Normal,
3468         ),
3469         (
3470             m_zero,
3471             m_normal_value,
3472             "0x1p+0",
3473             Status::OK,
3474             Category::Normal,
3475         ),
3476         (
3477             m_zero,
3478             p_largest_value,
3479             "-0x1.fffffep+127",
3480             Status::OK,
3481             Category::Normal,
3482         ),
3483         (
3484             m_zero,
3485             m_largest_value,
3486             "0x1.fffffep+127",
3487             Status::OK,
3488             Category::Normal,
3489         ),
3490         (
3491             m_zero,
3492             p_smallest_value,
3493             "-0x1p-149",
3494             Status::OK,
3495             Category::Normal,
3496         ),
3497         (
3498             m_zero,
3499             m_smallest_value,
3500             "0x1p-149",
3501             Status::OK,
3502             Category::Normal,
3503         ),
3504         (
3505             m_zero,
3506             p_smallest_normalized,
3507             "-0x1p-126",
3508             Status::OK,
3509             Category::Normal,
3510         ),
3511         (
3512             m_zero,
3513             m_smallest_normalized,
3514             "0x1p-126",
3515             Status::OK,
3516             Category::Normal,
3517         ),
3518         (qnan, p_inf, "nan", Status::OK, Category::NaN),
3519         (qnan, m_inf, "nan", Status::OK, Category::NaN),
3520         (qnan, p_zero, "nan", Status::OK, Category::NaN),
3521         (qnan, m_zero, "nan", Status::OK, Category::NaN),
3522         (qnan, qnan, "nan", Status::OK, Category::NaN),
3523         /*
3524 // See Note 1.
3525 (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
3526         */
3527         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
3528         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
3529         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
3530         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
3531         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
3532         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
3533         (
3534             qnan,
3535             p_smallest_normalized,
3536             "nan",
3537             Status::OK,
3538             Category::NaN,
3539         ),
3540         (
3541             qnan,
3542             m_smallest_normalized,
3543             "nan",
3544             Status::OK,
3545             Category::NaN,
3546         ),
3547         /*
3548 // See Note 1.
3549 (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
3550 (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
3551 (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
3552 (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
3553 (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
3554 (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
3555 (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
3556 (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
3557 (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
3558 (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
3559 (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
3560 (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
3561 (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
3562 (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
3563         */
3564         (
3565             p_normal_value,
3566             p_inf,
3567             "-inf",
3568             Status::OK,
3569             Category::Infinity,
3570         ),
3571         (p_normal_value, m_inf, "inf", Status::OK, Category::Infinity),
3572         (
3573             p_normal_value,
3574             p_zero,
3575             "0x1p+0",
3576             Status::OK,
3577             Category::Normal,
3578         ),
3579         (
3580             p_normal_value,
3581             m_zero,
3582             "0x1p+0",
3583             Status::OK,
3584             Category::Normal,
3585         ),
3586         (p_normal_value, qnan, "-nan", Status::OK, Category::NaN),
3587         /*
3588 // See Note 1.
3589 (p_normal_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
3590         */
3591         (
3592             p_normal_value,
3593             p_normal_value,
3594             "0x0p+0",
3595             Status::OK,
3596             Category::Zero,
3597         ),
3598         (
3599             p_normal_value,
3600             m_normal_value,
3601             "0x1p+1",
3602             Status::OK,
3603             Category::Normal,
3604         ),
3605         (
3606             p_normal_value,
3607             p_largest_value,
3608             "-0x1.fffffep+127",
3609             Status::INEXACT,
3610             Category::Normal,
3611         ),
3612         (
3613             p_normal_value,
3614             m_largest_value,
3615             "0x1.fffffep+127",
3616             Status::INEXACT,
3617             Category::Normal,
3618         ),
3619         (
3620             p_normal_value,
3621             p_smallest_value,
3622             "0x1p+0",
3623             Status::INEXACT,
3624             Category::Normal,
3625         ),
3626         (
3627             p_normal_value,
3628             m_smallest_value,
3629             "0x1p+0",
3630             Status::INEXACT,
3631             Category::Normal,
3632         ),
3633         (
3634             p_normal_value,
3635             p_smallest_normalized,
3636             "0x1p+0",
3637             Status::INEXACT,
3638             Category::Normal,
3639         ),
3640         (
3641             p_normal_value,
3642             m_smallest_normalized,
3643             "0x1p+0",
3644             Status::INEXACT,
3645             Category::Normal,
3646         ),
3647         (
3648             m_normal_value,
3649             p_inf,
3650             "-inf",
3651             Status::OK,
3652             Category::Infinity,
3653         ),
3654         (m_normal_value, m_inf, "inf", Status::OK, Category::Infinity),
3655         (
3656             m_normal_value,
3657             p_zero,
3658             "-0x1p+0",
3659             Status::OK,
3660             Category::Normal,
3661         ),
3662         (
3663             m_normal_value,
3664             m_zero,
3665             "-0x1p+0",
3666             Status::OK,
3667             Category::Normal,
3668         ),
3669         (m_normal_value, qnan, "-nan", Status::OK, Category::NaN),
3670         /*
3671 // See Note 1.
3672 (m_normal_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
3673         */
3674         (
3675             m_normal_value,
3676             p_normal_value,
3677             "-0x1p+1",
3678             Status::OK,
3679             Category::Normal,
3680         ),
3681         (
3682             m_normal_value,
3683             m_normal_value,
3684             "0x0p+0",
3685             Status::OK,
3686             Category::Zero,
3687         ),
3688         (
3689             m_normal_value,
3690             p_largest_value,
3691             "-0x1.fffffep+127",
3692             Status::INEXACT,
3693             Category::Normal,
3694         ),
3695         (
3696             m_normal_value,
3697             m_largest_value,
3698             "0x1.fffffep+127",
3699             Status::INEXACT,
3700             Category::Normal,
3701         ),
3702         (
3703             m_normal_value,
3704             p_smallest_value,
3705             "-0x1p+0",
3706             Status::INEXACT,
3707             Category::Normal,
3708         ),
3709         (
3710             m_normal_value,
3711             m_smallest_value,
3712             "-0x1p+0",
3713             Status::INEXACT,
3714             Category::Normal,
3715         ),
3716         (
3717             m_normal_value,
3718             p_smallest_normalized,
3719             "-0x1p+0",
3720             Status::INEXACT,
3721             Category::Normal,
3722         ),
3723         (
3724             m_normal_value,
3725             m_smallest_normalized,
3726             "-0x1p+0",
3727             Status::INEXACT,
3728             Category::Normal,
3729         ),
3730         (
3731             p_largest_value,
3732             p_inf,
3733             "-inf",
3734             Status::OK,
3735             Category::Infinity,
3736         ),
3737         (
3738             p_largest_value,
3739             m_inf,
3740             "inf",
3741             Status::OK,
3742             Category::Infinity,
3743         ),
3744         (
3745             p_largest_value,
3746             p_zero,
3747             "0x1.fffffep+127",
3748             Status::OK,
3749             Category::Normal,
3750         ),
3751         (
3752             p_largest_value,
3753             m_zero,
3754             "0x1.fffffep+127",
3755             Status::OK,
3756             Category::Normal,
3757         ),
3758         (p_largest_value, qnan, "-nan", Status::OK, Category::NaN),
3759         /*
3760 // See Note 1.
3761 (p_largest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
3762         */
3763         (
3764             p_largest_value,
3765             p_normal_value,
3766             "0x1.fffffep+127",
3767             Status::INEXACT,
3768             Category::Normal,
3769         ),
3770         (
3771             p_largest_value,
3772             m_normal_value,
3773             "0x1.fffffep+127",
3774             Status::INEXACT,
3775             Category::Normal,
3776         ),
3777         (
3778             p_largest_value,
3779             p_largest_value,
3780             "0x0p+0",
3781             Status::OK,
3782             Category::Zero,
3783         ),
3784         (
3785             p_largest_value,
3786             m_largest_value,
3787             "inf",
3788             overflow_status,
3789             Category::Infinity,
3790         ),
3791         (
3792             p_largest_value,
3793             p_smallest_value,
3794             "0x1.fffffep+127",
3795             Status::INEXACT,
3796             Category::Normal,
3797         ),
3798         (
3799             p_largest_value,
3800             m_smallest_value,
3801             "0x1.fffffep+127",
3802             Status::INEXACT,
3803             Category::Normal,
3804         ),
3805         (
3806             p_largest_value,
3807             p_smallest_normalized,
3808             "0x1.fffffep+127",
3809             Status::INEXACT,
3810             Category::Normal,
3811         ),
3812         (
3813             p_largest_value,
3814             m_smallest_normalized,
3815             "0x1.fffffep+127",
3816             Status::INEXACT,
3817             Category::Normal,
3818         ),
3819         (
3820             m_largest_value,
3821             p_inf,
3822             "-inf",
3823             Status::OK,
3824             Category::Infinity,
3825         ),
3826         (
3827             m_largest_value,
3828             m_inf,
3829             "inf",
3830             Status::OK,
3831             Category::Infinity,
3832         ),
3833         (
3834             m_largest_value,
3835             p_zero,
3836             "-0x1.fffffep+127",
3837             Status::OK,
3838             Category::Normal,
3839         ),
3840         (
3841             m_largest_value,
3842             m_zero,
3843             "-0x1.fffffep+127",
3844             Status::OK,
3845             Category::Normal,
3846         ),
3847         (m_largest_value, qnan, "-nan", Status::OK, Category::NaN),
3848         /*
3849 // See Note 1.
3850 (m_largest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
3851         */
3852         (
3853             m_largest_value,
3854             p_normal_value,
3855             "-0x1.fffffep+127",
3856             Status::INEXACT,
3857             Category::Normal,
3858         ),
3859         (
3860             m_largest_value,
3861             m_normal_value,
3862             "-0x1.fffffep+127",
3863             Status::INEXACT,
3864             Category::Normal,
3865         ),
3866         (
3867             m_largest_value,
3868             p_largest_value,
3869             "-inf",
3870             overflow_status,
3871             Category::Infinity,
3872         ),
3873         (
3874             m_largest_value,
3875             m_largest_value,
3876             "0x0p+0",
3877             Status::OK,
3878             Category::Zero,
3879         ),
3880         (
3881             m_largest_value,
3882             p_smallest_value,
3883             "-0x1.fffffep+127",
3884             Status::INEXACT,
3885             Category::Normal,
3886         ),
3887         (
3888             m_largest_value,
3889             m_smallest_value,
3890             "-0x1.fffffep+127",
3891             Status::INEXACT,
3892             Category::Normal,
3893         ),
3894         (
3895             m_largest_value,
3896             p_smallest_normalized,
3897             "-0x1.fffffep+127",
3898             Status::INEXACT,
3899             Category::Normal,
3900         ),
3901         (
3902             m_largest_value,
3903             m_smallest_normalized,
3904             "-0x1.fffffep+127",
3905             Status::INEXACT,
3906             Category::Normal,
3907         ),
3908         (
3909             p_smallest_value,
3910             p_inf,
3911             "-inf",
3912             Status::OK,
3913             Category::Infinity,
3914         ),
3915         (
3916             p_smallest_value,
3917             m_inf,
3918             "inf",
3919             Status::OK,
3920             Category::Infinity,
3921         ),
3922         (
3923             p_smallest_value,
3924             p_zero,
3925             "0x1p-149",
3926             Status::OK,
3927             Category::Normal,
3928         ),
3929         (
3930             p_smallest_value,
3931             m_zero,
3932             "0x1p-149",
3933             Status::OK,
3934             Category::Normal,
3935         ),
3936         (p_smallest_value, qnan, "-nan", Status::OK, Category::NaN),
3937         /*
3938 // See Note 1.
3939 (p_smallest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
3940         */
3941         (
3942             p_smallest_value,
3943             p_normal_value,
3944             "-0x1p+0",
3945             Status::INEXACT,
3946             Category::Normal,
3947         ),
3948         (
3949             p_smallest_value,
3950             m_normal_value,
3951             "0x1p+0",
3952             Status::INEXACT,
3953             Category::Normal,
3954         ),
3955         (
3956             p_smallest_value,
3957             p_largest_value,
3958             "-0x1.fffffep+127",
3959             Status::INEXACT,
3960             Category::Normal,
3961         ),
3962         (
3963             p_smallest_value,
3964             m_largest_value,
3965             "0x1.fffffep+127",
3966             Status::INEXACT,
3967             Category::Normal,
3968         ),
3969         (
3970             p_smallest_value,
3971             p_smallest_value,
3972             "0x0p+0",
3973             Status::OK,
3974             Category::Zero,
3975         ),
3976         (
3977             p_smallest_value,
3978             m_smallest_value,
3979             "0x1p-148",
3980             Status::OK,
3981             Category::Normal,
3982         ),
3983         (
3984             p_smallest_value,
3985             p_smallest_normalized,
3986             "-0x1.fffffcp-127",
3987             Status::OK,
3988             Category::Normal,
3989         ),
3990         (
3991             p_smallest_value,
3992             m_smallest_normalized,
3993             "0x1.000002p-126",
3994             Status::OK,
3995             Category::Normal,
3996         ),
3997         (
3998             m_smallest_value,
3999             p_inf,
4000             "-inf",
4001             Status::OK,
4002             Category::Infinity,
4003         ),
4004         (
4005             m_smallest_value,
4006             m_inf,
4007             "inf",
4008             Status::OK,
4009             Category::Infinity,
4010         ),
4011         (
4012             m_smallest_value,
4013             p_zero,
4014             "-0x1p-149",
4015             Status::OK,
4016             Category::Normal,
4017         ),
4018         (
4019             m_smallest_value,
4020             m_zero,
4021             "-0x1p-149",
4022             Status::OK,
4023             Category::Normal,
4024         ),
4025         (m_smallest_value, qnan, "-nan", Status::OK, Category::NaN),
4026         /*
4027 // See Note 1.
4028 (m_smallest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
4029         */
4030         (
4031             m_smallest_value,
4032             p_normal_value,
4033             "-0x1p+0",
4034             Status::INEXACT,
4035             Category::Normal,
4036         ),
4037         (
4038             m_smallest_value,
4039             m_normal_value,
4040             "0x1p+0",
4041             Status::INEXACT,
4042             Category::Normal,
4043         ),
4044         (
4045             m_smallest_value,
4046             p_largest_value,
4047             "-0x1.fffffep+127",
4048             Status::INEXACT,
4049             Category::Normal,
4050         ),
4051         (
4052             m_smallest_value,
4053             m_largest_value,
4054             "0x1.fffffep+127",
4055             Status::INEXACT,
4056             Category::Normal,
4057         ),
4058         (
4059             m_smallest_value,
4060             p_smallest_value,
4061             "-0x1p-148",
4062             Status::OK,
4063             Category::Normal,
4064         ),
4065         (
4066             m_smallest_value,
4067             m_smallest_value,
4068             "0x0p+0",
4069             Status::OK,
4070             Category::Zero,
4071         ),
4072         (
4073             m_smallest_value,
4074             p_smallest_normalized,
4075             "-0x1.000002p-126",
4076             Status::OK,
4077             Category::Normal,
4078         ),
4079         (
4080             m_smallest_value,
4081             m_smallest_normalized,
4082             "0x1.fffffcp-127",
4083             Status::OK,
4084             Category::Normal,
4085         ),
4086         (
4087             p_smallest_normalized,
4088             p_inf,
4089             "-inf",
4090             Status::OK,
4091             Category::Infinity,
4092         ),
4093         (
4094             p_smallest_normalized,
4095             m_inf,
4096             "inf",
4097             Status::OK,
4098             Category::Infinity,
4099         ),
4100         (
4101             p_smallest_normalized,
4102             p_zero,
4103             "0x1p-126",
4104             Status::OK,
4105             Category::Normal,
4106         ),
4107         (
4108             p_smallest_normalized,
4109             m_zero,
4110             "0x1p-126",
4111             Status::OK,
4112             Category::Normal,
4113         ),
4114         (
4115             p_smallest_normalized,
4116             qnan,
4117             "-nan",
4118             Status::OK,
4119             Category::NaN,
4120         ),
4121         /*
4122 // See Note 1.
4123 (p_smallest_normalized, snan, "-nan", Status::INVALID_OP, Category::NaN),
4124         */
4125         (
4126             p_smallest_normalized,
4127             p_normal_value,
4128             "-0x1p+0",
4129             Status::INEXACT,
4130             Category::Normal,
4131         ),
4132         (
4133             p_smallest_normalized,
4134             m_normal_value,
4135             "0x1p+0",
4136             Status::INEXACT,
4137             Category::Normal,
4138         ),
4139         (
4140             p_smallest_normalized,
4141             p_largest_value,
4142             "-0x1.fffffep+127",
4143             Status::INEXACT,
4144             Category::Normal,
4145         ),
4146         (
4147             p_smallest_normalized,
4148             m_largest_value,
4149             "0x1.fffffep+127",
4150             Status::INEXACT,
4151             Category::Normal,
4152         ),
4153         (
4154             p_smallest_normalized,
4155             p_smallest_value,
4156             "0x1.fffffcp-127",
4157             Status::OK,
4158             Category::Normal,
4159         ),
4160         (
4161             p_smallest_normalized,
4162             m_smallest_value,
4163             "0x1.000002p-126",
4164             Status::OK,
4165             Category::Normal,
4166         ),
4167         (
4168             p_smallest_normalized,
4169             p_smallest_normalized,
4170             "0x0p+0",
4171             Status::OK,
4172             Category::Zero,
4173         ),
4174         (
4175             p_smallest_normalized,
4176             m_smallest_normalized,
4177             "0x1p-125",
4178             Status::OK,
4179             Category::Normal,
4180         ),
4181         (
4182             m_smallest_normalized,
4183             p_inf,
4184             "-inf",
4185             Status::OK,
4186             Category::Infinity,
4187         ),
4188         (
4189             m_smallest_normalized,
4190             m_inf,
4191             "inf",
4192             Status::OK,
4193             Category::Infinity,
4194         ),
4195         (
4196             m_smallest_normalized,
4197             p_zero,
4198             "-0x1p-126",
4199             Status::OK,
4200             Category::Normal,
4201         ),
4202         (
4203             m_smallest_normalized,
4204             m_zero,
4205             "-0x1p-126",
4206             Status::OK,
4207             Category::Normal,
4208         ),
4209         (
4210             m_smallest_normalized,
4211             qnan,
4212             "-nan",
4213             Status::OK,
4214             Category::NaN,
4215         ),
4216         /*
4217 // See Note 1.
4218 (m_smallest_normalized, snan, "-nan", Status::INVALID_OP, Category::NaN),
4219         */
4220         (
4221             m_smallest_normalized,
4222             p_normal_value,
4223             "-0x1p+0",
4224             Status::INEXACT,
4225             Category::Normal,
4226         ),
4227         (
4228             m_smallest_normalized,
4229             m_normal_value,
4230             "0x1p+0",
4231             Status::INEXACT,
4232             Category::Normal,
4233         ),
4234         (
4235             m_smallest_normalized,
4236             p_largest_value,
4237             "-0x1.fffffep+127",
4238             Status::INEXACT,
4239             Category::Normal,
4240         ),
4241         (
4242             m_smallest_normalized,
4243             m_largest_value,
4244             "0x1.fffffep+127",
4245             Status::INEXACT,
4246             Category::Normal,
4247         ),
4248         (
4249             m_smallest_normalized,
4250             p_smallest_value,
4251             "-0x1.000002p-126",
4252             Status::OK,
4253             Category::Normal,
4254         ),
4255         (
4256             m_smallest_normalized,
4257             m_smallest_value,
4258             "-0x1.fffffcp-127",
4259             Status::OK,
4260             Category::Normal,
4261         ),
4262         (
4263             m_smallest_normalized,
4264             p_smallest_normalized,
4265             "-0x1p-125",
4266             Status::OK,
4267             Category::Normal,
4268         ),
4269         (
4270             m_smallest_normalized,
4271             m_smallest_normalized,
4272             "0x0p+0",
4273             Status::OK,
4274             Category::Zero,
4275         ),
4276     ];
4277
4278     for &(x, y, e_result, e_status, e_category) in &special_cases[..] {
4279         let status;
4280         let result = unpack!(status=, x - y);
4281         assert_eq!(status, e_status);
4282         assert_eq!(result.category(), e_category);
4283         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
4284     }
4285 }
4286
4287 #[test]
4288 fn multiply() {
4289     // Test Special Cases against each other and normal values.
4290
4291     // FIXMES/NOTES:
4292     // 1. Since we perform only default exception handling all operations with
4293     // signaling NaNs should have a result that is a quiet NaN. Currently they
4294     // return sNaN.
4295
4296     let p_inf = Single::INFINITY;
4297     let m_inf = -Single::INFINITY;
4298     let p_zero = Single::ZERO;
4299     let m_zero = -Single::ZERO;
4300     let qnan = Single::NAN;
4301     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
4302     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
4303     let p_largest_value = Single::largest();
4304     let m_largest_value = -Single::largest();
4305     let p_smallest_value = Single::SMALLEST;
4306     let m_smallest_value = -Single::SMALLEST;
4307     let p_smallest_normalized = Single::smallest_normalized();
4308     let m_smallest_normalized = -Single::smallest_normalized();
4309
4310     let overflow_status = Status::OVERFLOW | Status::INEXACT;
4311     let underflow_status = Status::UNDERFLOW | Status::INEXACT;
4312
4313     let special_cases = [
4314         (p_inf, p_inf, "inf", Status::OK, Category::Infinity),
4315         (p_inf, m_inf, "-inf", Status::OK, Category::Infinity),
4316         (p_inf, p_zero, "nan", Status::INVALID_OP, Category::NaN),
4317         (p_inf, m_zero, "nan", Status::INVALID_OP, Category::NaN),
4318         (p_inf, qnan, "nan", Status::OK, Category::NaN),
4319         /*
4320 // See Note 1.
4321 (p_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
4322         */
4323         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
4324         (
4325             p_inf,
4326             m_normal_value,
4327             "-inf",
4328             Status::OK,
4329             Category::Infinity,
4330         ),
4331         (
4332             p_inf,
4333             p_largest_value,
4334             "inf",
4335             Status::OK,
4336             Category::Infinity,
4337         ),
4338         (
4339             p_inf,
4340             m_largest_value,
4341             "-inf",
4342             Status::OK,
4343             Category::Infinity,
4344         ),
4345         (
4346             p_inf,
4347             p_smallest_value,
4348             "inf",
4349             Status::OK,
4350             Category::Infinity,
4351         ),
4352         (
4353             p_inf,
4354             m_smallest_value,
4355             "-inf",
4356             Status::OK,
4357             Category::Infinity,
4358         ),
4359         (
4360             p_inf,
4361             p_smallest_normalized,
4362             "inf",
4363             Status::OK,
4364             Category::Infinity,
4365         ),
4366         (
4367             p_inf,
4368             m_smallest_normalized,
4369             "-inf",
4370             Status::OK,
4371             Category::Infinity,
4372         ),
4373         (m_inf, p_inf, "-inf", Status::OK, Category::Infinity),
4374         (m_inf, m_inf, "inf", Status::OK, Category::Infinity),
4375         (m_inf, p_zero, "nan", Status::INVALID_OP, Category::NaN),
4376         (m_inf, m_zero, "nan", Status::INVALID_OP, Category::NaN),
4377         (m_inf, qnan, "nan", Status::OK, Category::NaN),
4378         /*
4379 // See Note 1.
4380 (m_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
4381         */
4382         (
4383             m_inf,
4384             p_normal_value,
4385             "-inf",
4386             Status::OK,
4387             Category::Infinity,
4388         ),
4389         (m_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
4390         (
4391             m_inf,
4392             p_largest_value,
4393             "-inf",
4394             Status::OK,
4395             Category::Infinity,
4396         ),
4397         (
4398             m_inf,
4399             m_largest_value,
4400             "inf",
4401             Status::OK,
4402             Category::Infinity,
4403         ),
4404         (
4405             m_inf,
4406             p_smallest_value,
4407             "-inf",
4408             Status::OK,
4409             Category::Infinity,
4410         ),
4411         (
4412             m_inf,
4413             m_smallest_value,
4414             "inf",
4415             Status::OK,
4416             Category::Infinity,
4417         ),
4418         (
4419             m_inf,
4420             p_smallest_normalized,
4421             "-inf",
4422             Status::OK,
4423             Category::Infinity,
4424         ),
4425         (
4426             m_inf,
4427             m_smallest_normalized,
4428             "inf",
4429             Status::OK,
4430             Category::Infinity,
4431         ),
4432         (p_zero, p_inf, "nan", Status::INVALID_OP, Category::NaN),
4433         (p_zero, m_inf, "nan", Status::INVALID_OP, Category::NaN),
4434         (p_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
4435         (p_zero, m_zero, "-0x0p+0", Status::OK, Category::Zero),
4436         (p_zero, qnan, "nan", Status::OK, Category::NaN),
4437         /*
4438 // See Note 1.
4439 (p_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
4440         */
4441         (p_zero, p_normal_value, "0x0p+0", Status::OK, Category::Zero),
4442         (
4443             p_zero,
4444             m_normal_value,
4445             "-0x0p+0",
4446             Status::OK,
4447             Category::Zero,
4448         ),
4449         (
4450             p_zero,
4451             p_largest_value,
4452             "0x0p+0",
4453             Status::OK,
4454             Category::Zero,
4455         ),
4456         (
4457             p_zero,
4458             m_largest_value,
4459             "-0x0p+0",
4460             Status::OK,
4461             Category::Zero,
4462         ),
4463         (
4464             p_zero,
4465             p_smallest_value,
4466             "0x0p+0",
4467             Status::OK,
4468             Category::Zero,
4469         ),
4470         (
4471             p_zero,
4472             m_smallest_value,
4473             "-0x0p+0",
4474             Status::OK,
4475             Category::Zero,
4476         ),
4477         (
4478             p_zero,
4479             p_smallest_normalized,
4480             "0x0p+0",
4481             Status::OK,
4482             Category::Zero,
4483         ),
4484         (
4485             p_zero,
4486             m_smallest_normalized,
4487             "-0x0p+0",
4488             Status::OK,
4489             Category::Zero,
4490         ),
4491         (m_zero, p_inf, "nan", Status::INVALID_OP, Category::NaN),
4492         (m_zero, m_inf, "nan", Status::INVALID_OP, Category::NaN),
4493         (m_zero, p_zero, "-0x0p+0", Status::OK, Category::Zero),
4494         (m_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
4495         (m_zero, qnan, "nan", Status::OK, Category::NaN),
4496         /*
4497 // See Note 1.
4498 (m_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
4499         */
4500         (
4501             m_zero,
4502             p_normal_value,
4503             "-0x0p+0",
4504             Status::OK,
4505             Category::Zero,
4506         ),
4507         (m_zero, m_normal_value, "0x0p+0", Status::OK, Category::Zero),
4508         (
4509             m_zero,
4510             p_largest_value,
4511             "-0x0p+0",
4512             Status::OK,
4513             Category::Zero,
4514         ),
4515         (
4516             m_zero,
4517             m_largest_value,
4518             "0x0p+0",
4519             Status::OK,
4520             Category::Zero,
4521         ),
4522         (
4523             m_zero,
4524             p_smallest_value,
4525             "-0x0p+0",
4526             Status::OK,
4527             Category::Zero,
4528         ),
4529         (
4530             m_zero,
4531             m_smallest_value,
4532             "0x0p+0",
4533             Status::OK,
4534             Category::Zero,
4535         ),
4536         (
4537             m_zero,
4538             p_smallest_normalized,
4539             "-0x0p+0",
4540             Status::OK,
4541             Category::Zero,
4542         ),
4543         (
4544             m_zero,
4545             m_smallest_normalized,
4546             "0x0p+0",
4547             Status::OK,
4548             Category::Zero,
4549         ),
4550         (qnan, p_inf, "nan", Status::OK, Category::NaN),
4551         (qnan, m_inf, "nan", Status::OK, Category::NaN),
4552         (qnan, p_zero, "nan", Status::OK, Category::NaN),
4553         (qnan, m_zero, "nan", Status::OK, Category::NaN),
4554         (qnan, qnan, "nan", Status::OK, Category::NaN),
4555         /*
4556 // See Note 1.
4557 (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
4558         */
4559         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
4560         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
4561         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
4562         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
4563         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
4564         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
4565         (
4566             qnan,
4567             p_smallest_normalized,
4568             "nan",
4569             Status::OK,
4570             Category::NaN,
4571         ),
4572         (
4573             qnan,
4574             m_smallest_normalized,
4575             "nan",
4576             Status::OK,
4577             Category::NaN,
4578         ),
4579         /*
4580 // See Note 1.
4581 (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
4582 (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
4583 (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
4584 (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
4585 (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
4586 (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
4587 (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
4588 (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
4589 (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
4590 (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
4591 (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
4592 (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
4593 (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
4594 (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
4595         */
4596         (p_normal_value, p_inf, "inf", Status::OK, Category::Infinity),
4597         (
4598             p_normal_value,
4599             m_inf,
4600             "-inf",
4601             Status::OK,
4602             Category::Infinity,
4603         ),
4604         (p_normal_value, p_zero, "0x0p+0", Status::OK, Category::Zero),
4605         (
4606             p_normal_value,
4607             m_zero,
4608             "-0x0p+0",
4609             Status::OK,
4610             Category::Zero,
4611         ),
4612         (p_normal_value, qnan, "nan", Status::OK, Category::NaN),
4613         /*
4614 // See Note 1.
4615 (p_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
4616         */
4617         (
4618             p_normal_value,
4619             p_normal_value,
4620             "0x1p+0",
4621             Status::OK,
4622             Category::Normal,
4623         ),
4624         (
4625             p_normal_value,
4626             m_normal_value,
4627             "-0x1p+0",
4628             Status::OK,
4629             Category::Normal,
4630         ),
4631         (
4632             p_normal_value,
4633             p_largest_value,
4634             "0x1.fffffep+127",
4635             Status::OK,
4636             Category::Normal,
4637         ),
4638         (
4639             p_normal_value,
4640             m_largest_value,
4641             "-0x1.fffffep+127",
4642             Status::OK,
4643             Category::Normal,
4644         ),
4645         (
4646             p_normal_value,
4647             p_smallest_value,
4648             "0x1p-149",
4649             Status::OK,
4650             Category::Normal,
4651         ),
4652         (
4653             p_normal_value,
4654             m_smallest_value,
4655             "-0x1p-149",
4656             Status::OK,
4657             Category::Normal,
4658         ),
4659         (
4660             p_normal_value,
4661             p_smallest_normalized,
4662             "0x1p-126",
4663             Status::OK,
4664             Category::Normal,
4665         ),
4666         (
4667             p_normal_value,
4668             m_smallest_normalized,
4669             "-0x1p-126",
4670             Status::OK,
4671             Category::Normal,
4672         ),
4673         (
4674             m_normal_value,
4675             p_inf,
4676             "-inf",
4677             Status::OK,
4678             Category::Infinity,
4679         ),
4680         (m_normal_value, m_inf, "inf", Status::OK, Category::Infinity),
4681         (
4682             m_normal_value,
4683             p_zero,
4684             "-0x0p+0",
4685             Status::OK,
4686             Category::Zero,
4687         ),
4688         (m_normal_value, m_zero, "0x0p+0", Status::OK, Category::Zero),
4689         (m_normal_value, qnan, "nan", Status::OK, Category::NaN),
4690         /*
4691 // See Note 1.
4692 (m_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
4693         */
4694         (
4695             m_normal_value,
4696             p_normal_value,
4697             "-0x1p+0",
4698             Status::OK,
4699             Category::Normal,
4700         ),
4701         (
4702             m_normal_value,
4703             m_normal_value,
4704             "0x1p+0",
4705             Status::OK,
4706             Category::Normal,
4707         ),
4708         (
4709             m_normal_value,
4710             p_largest_value,
4711             "-0x1.fffffep+127",
4712             Status::OK,
4713             Category::Normal,
4714         ),
4715         (
4716             m_normal_value,
4717             m_largest_value,
4718             "0x1.fffffep+127",
4719             Status::OK,
4720             Category::Normal,
4721         ),
4722         (
4723             m_normal_value,
4724             p_smallest_value,
4725             "-0x1p-149",
4726             Status::OK,
4727             Category::Normal,
4728         ),
4729         (
4730             m_normal_value,
4731             m_smallest_value,
4732             "0x1p-149",
4733             Status::OK,
4734             Category::Normal,
4735         ),
4736         (
4737             m_normal_value,
4738             p_smallest_normalized,
4739             "-0x1p-126",
4740             Status::OK,
4741             Category::Normal,
4742         ),
4743         (
4744             m_normal_value,
4745             m_smallest_normalized,
4746             "0x1p-126",
4747             Status::OK,
4748             Category::Normal,
4749         ),
4750         (
4751             p_largest_value,
4752             p_inf,
4753             "inf",
4754             Status::OK,
4755             Category::Infinity,
4756         ),
4757         (
4758             p_largest_value,
4759             m_inf,
4760             "-inf",
4761             Status::OK,
4762             Category::Infinity,
4763         ),
4764         (
4765             p_largest_value,
4766             p_zero,
4767             "0x0p+0",
4768             Status::OK,
4769             Category::Zero,
4770         ),
4771         (
4772             p_largest_value,
4773             m_zero,
4774             "-0x0p+0",
4775             Status::OK,
4776             Category::Zero,
4777         ),
4778         (p_largest_value, qnan, "nan", Status::OK, Category::NaN),
4779         /*
4780 // See Note 1.
4781 (p_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
4782         */
4783         (
4784             p_largest_value,
4785             p_normal_value,
4786             "0x1.fffffep+127",
4787             Status::OK,
4788             Category::Normal,
4789         ),
4790         (
4791             p_largest_value,
4792             m_normal_value,
4793             "-0x1.fffffep+127",
4794             Status::OK,
4795             Category::Normal,
4796         ),
4797         (
4798             p_largest_value,
4799             p_largest_value,
4800             "inf",
4801             overflow_status,
4802             Category::Infinity,
4803         ),
4804         (
4805             p_largest_value,
4806             m_largest_value,
4807             "-inf",
4808             overflow_status,
4809             Category::Infinity,
4810         ),
4811         (
4812             p_largest_value,
4813             p_smallest_value,
4814             "0x1.fffffep-22",
4815             Status::OK,
4816             Category::Normal,
4817         ),
4818         (
4819             p_largest_value,
4820             m_smallest_value,
4821             "-0x1.fffffep-22",
4822             Status::OK,
4823             Category::Normal,
4824         ),
4825         (
4826             p_largest_value,
4827             p_smallest_normalized,
4828             "0x1.fffffep+1",
4829             Status::OK,
4830             Category::Normal,
4831         ),
4832         (
4833             p_largest_value,
4834             m_smallest_normalized,
4835             "-0x1.fffffep+1",
4836             Status::OK,
4837             Category::Normal,
4838         ),
4839         (
4840             m_largest_value,
4841             p_inf,
4842             "-inf",
4843             Status::OK,
4844             Category::Infinity,
4845         ),
4846         (
4847             m_largest_value,
4848             m_inf,
4849             "inf",
4850             Status::OK,
4851             Category::Infinity,
4852         ),
4853         (
4854             m_largest_value,
4855             p_zero,
4856             "-0x0p+0",
4857             Status::OK,
4858             Category::Zero,
4859         ),
4860         (
4861             m_largest_value,
4862             m_zero,
4863             "0x0p+0",
4864             Status::OK,
4865             Category::Zero,
4866         ),
4867         (m_largest_value, qnan, "nan", Status::OK, Category::NaN),
4868         /*
4869 // See Note 1.
4870 (m_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
4871         */
4872         (
4873             m_largest_value,
4874             p_normal_value,
4875             "-0x1.fffffep+127",
4876             Status::OK,
4877             Category::Normal,
4878         ),
4879         (
4880             m_largest_value,
4881             m_normal_value,
4882             "0x1.fffffep+127",
4883             Status::OK,
4884             Category::Normal,
4885         ),
4886         (
4887             m_largest_value,
4888             p_largest_value,
4889             "-inf",
4890             overflow_status,
4891             Category::Infinity,
4892         ),
4893         (
4894             m_largest_value,
4895             m_largest_value,
4896             "inf",
4897             overflow_status,
4898             Category::Infinity,
4899         ),
4900         (
4901             m_largest_value,
4902             p_smallest_value,
4903             "-0x1.fffffep-22",
4904             Status::OK,
4905             Category::Normal,
4906         ),
4907         (
4908             m_largest_value,
4909             m_smallest_value,
4910             "0x1.fffffep-22",
4911             Status::OK,
4912             Category::Normal,
4913         ),
4914         (
4915             m_largest_value,
4916             p_smallest_normalized,
4917             "-0x1.fffffep+1",
4918             Status::OK,
4919             Category::Normal,
4920         ),
4921         (
4922             m_largest_value,
4923             m_smallest_normalized,
4924             "0x1.fffffep+1",
4925             Status::OK,
4926             Category::Normal,
4927         ),
4928         (
4929             p_smallest_value,
4930             p_inf,
4931             "inf",
4932             Status::OK,
4933             Category::Infinity,
4934         ),
4935         (
4936             p_smallest_value,
4937             m_inf,
4938             "-inf",
4939             Status::OK,
4940             Category::Infinity,
4941         ),
4942         (
4943             p_smallest_value,
4944             p_zero,
4945             "0x0p+0",
4946             Status::OK,
4947             Category::Zero,
4948         ),
4949         (
4950             p_smallest_value,
4951             m_zero,
4952             "-0x0p+0",
4953             Status::OK,
4954             Category::Zero,
4955         ),
4956         (p_smallest_value, qnan, "nan", Status::OK, Category::NaN),
4957         /*
4958 // See Note 1.
4959 (p_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
4960         */
4961         (
4962             p_smallest_value,
4963             p_normal_value,
4964             "0x1p-149",
4965             Status::OK,
4966             Category::Normal,
4967         ),
4968         (
4969             p_smallest_value,
4970             m_normal_value,
4971             "-0x1p-149",
4972             Status::OK,
4973             Category::Normal,
4974         ),
4975         (
4976             p_smallest_value,
4977             p_largest_value,
4978             "0x1.fffffep-22",
4979             Status::OK,
4980             Category::Normal,
4981         ),
4982         (
4983             p_smallest_value,
4984             m_largest_value,
4985             "-0x1.fffffep-22",
4986             Status::OK,
4987             Category::Normal,
4988         ),
4989         (
4990             p_smallest_value,
4991             p_smallest_value,
4992             "0x0p+0",
4993             underflow_status,
4994             Category::Zero,
4995         ),
4996         (
4997             p_smallest_value,
4998             m_smallest_value,
4999             "-0x0p+0",
5000             underflow_status,
5001             Category::Zero,
5002         ),
5003         (
5004             p_smallest_value,
5005             p_smallest_normalized,
5006             "0x0p+0",
5007             underflow_status,
5008             Category::Zero,
5009         ),
5010         (
5011             p_smallest_value,
5012             m_smallest_normalized,
5013             "-0x0p+0",
5014             underflow_status,
5015             Category::Zero,
5016         ),
5017         (
5018             m_smallest_value,
5019             p_inf,
5020             "-inf",
5021             Status::OK,
5022             Category::Infinity,
5023         ),
5024         (
5025             m_smallest_value,
5026             m_inf,
5027             "inf",
5028             Status::OK,
5029             Category::Infinity,
5030         ),
5031         (
5032             m_smallest_value,
5033             p_zero,
5034             "-0x0p+0",
5035             Status::OK,
5036             Category::Zero,
5037         ),
5038         (
5039             m_smallest_value,
5040             m_zero,
5041             "0x0p+0",
5042             Status::OK,
5043             Category::Zero,
5044         ),
5045         (m_smallest_value, qnan, "nan", Status::OK, Category::NaN),
5046         /*
5047 // See Note 1.
5048 (m_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
5049         */
5050         (
5051             m_smallest_value,
5052             p_normal_value,
5053             "-0x1p-149",
5054             Status::OK,
5055             Category::Normal,
5056         ),
5057         (
5058             m_smallest_value,
5059             m_normal_value,
5060             "0x1p-149",
5061             Status::OK,
5062             Category::Normal,
5063         ),
5064         (
5065             m_smallest_value,
5066             p_largest_value,
5067             "-0x1.fffffep-22",
5068             Status::OK,
5069             Category::Normal,
5070         ),
5071         (
5072             m_smallest_value,
5073             m_largest_value,
5074             "0x1.fffffep-22",
5075             Status::OK,
5076             Category::Normal,
5077         ),
5078         (
5079             m_smallest_value,
5080             p_smallest_value,
5081             "-0x0p+0",
5082             underflow_status,
5083             Category::Zero,
5084         ),
5085         (
5086             m_smallest_value,
5087             m_smallest_value,
5088             "0x0p+0",
5089             underflow_status,
5090             Category::Zero,
5091         ),
5092         (
5093             m_smallest_value,
5094             p_smallest_normalized,
5095             "-0x0p+0",
5096             underflow_status,
5097             Category::Zero,
5098         ),
5099         (
5100             m_smallest_value,
5101             m_smallest_normalized,
5102             "0x0p+0",
5103             underflow_status,
5104             Category::Zero,
5105         ),
5106         (
5107             p_smallest_normalized,
5108             p_inf,
5109             "inf",
5110             Status::OK,
5111             Category::Infinity,
5112         ),
5113         (
5114             p_smallest_normalized,
5115             m_inf,
5116             "-inf",
5117             Status::OK,
5118             Category::Infinity,
5119         ),
5120         (
5121             p_smallest_normalized,
5122             p_zero,
5123             "0x0p+0",
5124             Status::OK,
5125             Category::Zero,
5126         ),
5127         (
5128             p_smallest_normalized,
5129             m_zero,
5130             "-0x0p+0",
5131             Status::OK,
5132             Category::Zero,
5133         ),
5134         (
5135             p_smallest_normalized,
5136             qnan,
5137             "nan",
5138             Status::OK,
5139             Category::NaN,
5140         ),
5141         /*
5142 // See Note 1.
5143 (p_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
5144         */
5145         (
5146             p_smallest_normalized,
5147             p_normal_value,
5148             "0x1p-126",
5149             Status::OK,
5150             Category::Normal,
5151         ),
5152         (
5153             p_smallest_normalized,
5154             m_normal_value,
5155             "-0x1p-126",
5156             Status::OK,
5157             Category::Normal,
5158         ),
5159         (
5160             p_smallest_normalized,
5161             p_largest_value,
5162             "0x1.fffffep+1",
5163             Status::OK,
5164             Category::Normal,
5165         ),
5166         (
5167             p_smallest_normalized,
5168             m_largest_value,
5169             "-0x1.fffffep+1",
5170             Status::OK,
5171             Category::Normal,
5172         ),
5173         (
5174             p_smallest_normalized,
5175             p_smallest_value,
5176             "0x0p+0",
5177             underflow_status,
5178             Category::Zero,
5179         ),
5180         (
5181             p_smallest_normalized,
5182             m_smallest_value,
5183             "-0x0p+0",
5184             underflow_status,
5185             Category::Zero,
5186         ),
5187         (
5188             p_smallest_normalized,
5189             p_smallest_normalized,
5190             "0x0p+0",
5191             underflow_status,
5192             Category::Zero,
5193         ),
5194         (
5195             p_smallest_normalized,
5196             m_smallest_normalized,
5197             "-0x0p+0",
5198             underflow_status,
5199             Category::Zero,
5200         ),
5201         (
5202             m_smallest_normalized,
5203             p_inf,
5204             "-inf",
5205             Status::OK,
5206             Category::Infinity,
5207         ),
5208         (
5209             m_smallest_normalized,
5210             m_inf,
5211             "inf",
5212             Status::OK,
5213             Category::Infinity,
5214         ),
5215         (
5216             m_smallest_normalized,
5217             p_zero,
5218             "-0x0p+0",
5219             Status::OK,
5220             Category::Zero,
5221         ),
5222         (
5223             m_smallest_normalized,
5224             m_zero,
5225             "0x0p+0",
5226             Status::OK,
5227             Category::Zero,
5228         ),
5229         (
5230             m_smallest_normalized,
5231             qnan,
5232             "nan",
5233             Status::OK,
5234             Category::NaN,
5235         ),
5236         /*
5237 // See Note 1.
5238 (m_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
5239         */
5240         (
5241             m_smallest_normalized,
5242             p_normal_value,
5243             "-0x1p-126",
5244             Status::OK,
5245             Category::Normal,
5246         ),
5247         (
5248             m_smallest_normalized,
5249             m_normal_value,
5250             "0x1p-126",
5251             Status::OK,
5252             Category::Normal,
5253         ),
5254         (
5255             m_smallest_normalized,
5256             p_largest_value,
5257             "-0x1.fffffep+1",
5258             Status::OK,
5259             Category::Normal,
5260         ),
5261         (
5262             m_smallest_normalized,
5263             m_largest_value,
5264             "0x1.fffffep+1",
5265             Status::OK,
5266             Category::Normal,
5267         ),
5268         (
5269             m_smallest_normalized,
5270             p_smallest_value,
5271             "-0x0p+0",
5272             underflow_status,
5273             Category::Zero,
5274         ),
5275         (
5276             m_smallest_normalized,
5277             m_smallest_value,
5278             "0x0p+0",
5279             underflow_status,
5280             Category::Zero,
5281         ),
5282         (
5283             m_smallest_normalized,
5284             p_smallest_normalized,
5285             "-0x0p+0",
5286             underflow_status,
5287             Category::Zero,
5288         ),
5289         (
5290             m_smallest_normalized,
5291             m_smallest_normalized,
5292             "0x0p+0",
5293             underflow_status,
5294             Category::Zero,
5295         ),
5296     ];
5297
5298     for &(x, y, e_result, e_status, e_category) in &special_cases[..] {
5299         let status;
5300         let result = unpack!(status=, x * y);
5301         assert_eq!(status, e_status);
5302         assert_eq!(result.category(), e_category);
5303         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
5304     }
5305 }
5306
5307 #[test]
5308 fn divide() {
5309     // Test Special Cases against each other and normal values.
5310
5311     // FIXMES/NOTES:
5312     // 1. Since we perform only default exception handling all operations with
5313     // signaling NaNs should have a result that is a quiet NaN. Currently they
5314     // return sNaN.
5315
5316     let p_inf = Single::INFINITY;
5317     let m_inf = -Single::INFINITY;
5318     let p_zero = Single::ZERO;
5319     let m_zero = -Single::ZERO;
5320     let qnan = Single::NAN;
5321     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
5322     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
5323     let p_largest_value = Single::largest();
5324     let m_largest_value = -Single::largest();
5325     let p_smallest_value = Single::SMALLEST;
5326     let m_smallest_value = -Single::SMALLEST;
5327     let p_smallest_normalized = Single::smallest_normalized();
5328     let m_smallest_normalized = -Single::smallest_normalized();
5329
5330     let overflow_status = Status::OVERFLOW | Status::INEXACT;
5331     let underflow_status = Status::UNDERFLOW | Status::INEXACT;
5332
5333     let special_cases = [
5334         (p_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
5335         (p_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
5336         (p_inf, p_zero, "inf", Status::OK, Category::Infinity),
5337         (p_inf, m_zero, "-inf", Status::OK, Category::Infinity),
5338         (p_inf, qnan, "nan", Status::OK, Category::NaN),
5339         /*
5340 // See Note 1.
5341 (p_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
5342         */
5343         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
5344         (
5345             p_inf,
5346             m_normal_value,
5347             "-inf",
5348             Status::OK,
5349             Category::Infinity,
5350         ),
5351         (
5352             p_inf,
5353             p_largest_value,
5354             "inf",
5355             Status::OK,
5356             Category::Infinity,
5357         ),
5358         (
5359             p_inf,
5360             m_largest_value,
5361             "-inf",
5362             Status::OK,
5363             Category::Infinity,
5364         ),
5365         (
5366             p_inf,
5367             p_smallest_value,
5368             "inf",
5369             Status::OK,
5370             Category::Infinity,
5371         ),
5372         (
5373             p_inf,
5374             m_smallest_value,
5375             "-inf",
5376             Status::OK,
5377             Category::Infinity,
5378         ),
5379         (
5380             p_inf,
5381             p_smallest_normalized,
5382             "inf",
5383             Status::OK,
5384             Category::Infinity,
5385         ),
5386         (
5387             p_inf,
5388             m_smallest_normalized,
5389             "-inf",
5390             Status::OK,
5391             Category::Infinity,
5392         ),
5393         (m_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
5394         (m_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
5395         (m_inf, p_zero, "-inf", Status::OK, Category::Infinity),
5396         (m_inf, m_zero, "inf", Status::OK, Category::Infinity),
5397         (m_inf, qnan, "nan", Status::OK, Category::NaN),
5398         /*
5399 // See Note 1.
5400 (m_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
5401         */
5402         (
5403             m_inf,
5404             p_normal_value,
5405             "-inf",
5406             Status::OK,
5407             Category::Infinity,
5408         ),
5409         (m_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
5410         (
5411             m_inf,
5412             p_largest_value,
5413             "-inf",
5414             Status::OK,
5415             Category::Infinity,
5416         ),
5417         (
5418             m_inf,
5419             m_largest_value,
5420             "inf",
5421             Status::OK,
5422             Category::Infinity,
5423         ),
5424         (
5425             m_inf,
5426             p_smallest_value,
5427             "-inf",
5428             Status::OK,
5429             Category::Infinity,
5430         ),
5431         (
5432             m_inf,
5433             m_smallest_value,
5434             "inf",
5435             Status::OK,
5436             Category::Infinity,
5437         ),
5438         (
5439             m_inf,
5440             p_smallest_normalized,
5441             "-inf",
5442             Status::OK,
5443             Category::Infinity,
5444         ),
5445         (
5446             m_inf,
5447             m_smallest_normalized,
5448             "inf",
5449             Status::OK,
5450             Category::Infinity,
5451         ),
5452         (p_zero, p_inf, "0x0p+0", Status::OK, Category::Zero),
5453         (p_zero, m_inf, "-0x0p+0", Status::OK, Category::Zero),
5454         (p_zero, p_zero, "nan", Status::INVALID_OP, Category::NaN),
5455         (p_zero, m_zero, "nan", Status::INVALID_OP, Category::NaN),
5456         (p_zero, qnan, "nan", Status::OK, Category::NaN),
5457         /*
5458 // See Note 1.
5459 (p_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
5460         */
5461         (p_zero, p_normal_value, "0x0p+0", Status::OK, Category::Zero),
5462         (
5463             p_zero,
5464             m_normal_value,
5465             "-0x0p+0",
5466             Status::OK,
5467             Category::Zero,
5468         ),
5469         (
5470             p_zero,
5471             p_largest_value,
5472             "0x0p+0",
5473             Status::OK,
5474             Category::Zero,
5475         ),
5476         (
5477             p_zero,
5478             m_largest_value,
5479             "-0x0p+0",
5480             Status::OK,
5481             Category::Zero,
5482         ),
5483         (
5484             p_zero,
5485             p_smallest_value,
5486             "0x0p+0",
5487             Status::OK,
5488             Category::Zero,
5489         ),
5490         (
5491             p_zero,
5492             m_smallest_value,
5493             "-0x0p+0",
5494             Status::OK,
5495             Category::Zero,
5496         ),
5497         (
5498             p_zero,
5499             p_smallest_normalized,
5500             "0x0p+0",
5501             Status::OK,
5502             Category::Zero,
5503         ),
5504         (
5505             p_zero,
5506             m_smallest_normalized,
5507             "-0x0p+0",
5508             Status::OK,
5509             Category::Zero,
5510         ),
5511         (m_zero, p_inf, "-0x0p+0", Status::OK, Category::Zero),
5512         (m_zero, m_inf, "0x0p+0", Status::OK, Category::Zero),
5513         (m_zero, p_zero, "nan", Status::INVALID_OP, Category::NaN),
5514         (m_zero, m_zero, "nan", Status::INVALID_OP, Category::NaN),
5515         (m_zero, qnan, "nan", Status::OK, Category::NaN),
5516         /*
5517 // See Note 1.
5518 (m_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
5519         */
5520         (
5521             m_zero,
5522             p_normal_value,
5523             "-0x0p+0",
5524             Status::OK,
5525             Category::Zero,
5526         ),
5527         (m_zero, m_normal_value, "0x0p+0", Status::OK, Category::Zero),
5528         (
5529             m_zero,
5530             p_largest_value,
5531             "-0x0p+0",
5532             Status::OK,
5533             Category::Zero,
5534         ),
5535         (
5536             m_zero,
5537             m_largest_value,
5538             "0x0p+0",
5539             Status::OK,
5540             Category::Zero,
5541         ),
5542         (
5543             m_zero,
5544             p_smallest_value,
5545             "-0x0p+0",
5546             Status::OK,
5547             Category::Zero,
5548         ),
5549         (
5550             m_zero,
5551             m_smallest_value,
5552             "0x0p+0",
5553             Status::OK,
5554             Category::Zero,
5555         ),
5556         (
5557             m_zero,
5558             p_smallest_normalized,
5559             "-0x0p+0",
5560             Status::OK,
5561             Category::Zero,
5562         ),
5563         (
5564             m_zero,
5565             m_smallest_normalized,
5566             "0x0p+0",
5567             Status::OK,
5568             Category::Zero,
5569         ),
5570         (qnan, p_inf, "nan", Status::OK, Category::NaN),
5571         (qnan, m_inf, "nan", Status::OK, Category::NaN),
5572         (qnan, p_zero, "nan", Status::OK, Category::NaN),
5573         (qnan, m_zero, "nan", Status::OK, Category::NaN),
5574         (qnan, qnan, "nan", Status::OK, Category::NaN),
5575         /*
5576 // See Note 1.
5577 (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
5578         */
5579         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
5580         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
5581         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
5582         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
5583         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
5584         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
5585         (
5586             qnan,
5587             p_smallest_normalized,
5588             "nan",
5589             Status::OK,
5590             Category::NaN,
5591         ),
5592         (
5593             qnan,
5594             m_smallest_normalized,
5595             "nan",
5596             Status::OK,
5597             Category::NaN,
5598         ),
5599         /*
5600 // See Note 1.
5601 (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
5602 (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
5603 (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
5604 (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
5605 (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
5606 (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
5607 (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
5608 (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
5609 (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
5610 (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
5611 (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
5612 (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
5613 (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
5614 (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
5615         */
5616         (p_normal_value, p_inf, "0x0p+0", Status::OK, Category::Zero),
5617         (p_normal_value, m_inf, "-0x0p+0", Status::OK, Category::Zero),
5618         (
5619             p_normal_value,
5620             p_zero,
5621             "inf",
5622             Status::DIV_BY_ZERO,
5623             Category::Infinity,
5624         ),
5625         (
5626             p_normal_value,
5627             m_zero,
5628             "-inf",
5629             Status::DIV_BY_ZERO,
5630             Category::Infinity,
5631         ),
5632         (p_normal_value, qnan, "nan", Status::OK, Category::NaN),
5633         /*
5634 // See Note 1.
5635 (p_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
5636         */
5637         (
5638             p_normal_value,
5639             p_normal_value,
5640             "0x1p+0",
5641             Status::OK,
5642             Category::Normal,
5643         ),
5644         (
5645             p_normal_value,
5646             m_normal_value,
5647             "-0x1p+0",
5648             Status::OK,
5649             Category::Normal,
5650         ),
5651         (
5652             p_normal_value,
5653             p_largest_value,
5654             "0x1p-128",
5655             underflow_status,
5656             Category::Normal,
5657         ),
5658         (
5659             p_normal_value,
5660             m_largest_value,
5661             "-0x1p-128",
5662             underflow_status,
5663             Category::Normal,
5664         ),
5665         (
5666             p_normal_value,
5667             p_smallest_value,
5668             "inf",
5669             overflow_status,
5670             Category::Infinity,
5671         ),
5672         (
5673             p_normal_value,
5674             m_smallest_value,
5675             "-inf",
5676             overflow_status,
5677             Category::Infinity,
5678         ),
5679         (
5680             p_normal_value,
5681             p_smallest_normalized,
5682             "0x1p+126",
5683             Status::OK,
5684             Category::Normal,
5685         ),
5686         (
5687             p_normal_value,
5688             m_smallest_normalized,
5689             "-0x1p+126",
5690             Status::OK,
5691             Category::Normal,
5692         ),
5693         (m_normal_value, p_inf, "-0x0p+0", Status::OK, Category::Zero),
5694         (m_normal_value, m_inf, "0x0p+0", Status::OK, Category::Zero),
5695         (
5696             m_normal_value,
5697             p_zero,
5698             "-inf",
5699             Status::DIV_BY_ZERO,
5700             Category::Infinity,
5701         ),
5702         (
5703             m_normal_value,
5704             m_zero,
5705             "inf",
5706             Status::DIV_BY_ZERO,
5707             Category::Infinity,
5708         ),
5709         (m_normal_value, qnan, "nan", Status::OK, Category::NaN),
5710         /*
5711 // See Note 1.
5712 (m_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
5713         */
5714         (
5715             m_normal_value,
5716             p_normal_value,
5717             "-0x1p+0",
5718             Status::OK,
5719             Category::Normal,
5720         ),
5721         (
5722             m_normal_value,
5723             m_normal_value,
5724             "0x1p+0",
5725             Status::OK,
5726             Category::Normal,
5727         ),
5728         (
5729             m_normal_value,
5730             p_largest_value,
5731             "-0x1p-128",
5732             underflow_status,
5733             Category::Normal,
5734         ),
5735         (
5736             m_normal_value,
5737             m_largest_value,
5738             "0x1p-128",
5739             underflow_status,
5740             Category::Normal,
5741         ),
5742         (
5743             m_normal_value,
5744             p_smallest_value,
5745             "-inf",
5746             overflow_status,
5747             Category::Infinity,
5748         ),
5749         (
5750             m_normal_value,
5751             m_smallest_value,
5752             "inf",
5753             overflow_status,
5754             Category::Infinity,
5755         ),
5756         (
5757             m_normal_value,
5758             p_smallest_normalized,
5759             "-0x1p+126",
5760             Status::OK,
5761             Category::Normal,
5762         ),
5763         (
5764             m_normal_value,
5765             m_smallest_normalized,
5766             "0x1p+126",
5767             Status::OK,
5768             Category::Normal,
5769         ),
5770         (p_largest_value, p_inf, "0x0p+0", Status::OK, Category::Zero),
5771         (
5772             p_largest_value,
5773             m_inf,
5774             "-0x0p+0",
5775             Status::OK,
5776             Category::Zero,
5777         ),
5778         (
5779             p_largest_value,
5780             p_zero,
5781             "inf",
5782             Status::DIV_BY_ZERO,
5783             Category::Infinity,
5784         ),
5785         (
5786             p_largest_value,
5787             m_zero,
5788             "-inf",
5789             Status::DIV_BY_ZERO,
5790             Category::Infinity,
5791         ),
5792         (p_largest_value, qnan, "nan", Status::OK, Category::NaN),
5793         /*
5794 // See Note 1.
5795 (p_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
5796         */
5797         (
5798             p_largest_value,
5799             p_normal_value,
5800             "0x1.fffffep+127",
5801             Status::OK,
5802             Category::Normal,
5803         ),
5804         (
5805             p_largest_value,
5806             m_normal_value,
5807             "-0x1.fffffep+127",
5808             Status::OK,
5809             Category::Normal,
5810         ),
5811         (
5812             p_largest_value,
5813             p_largest_value,
5814             "0x1p+0",
5815             Status::OK,
5816             Category::Normal,
5817         ),
5818         (
5819             p_largest_value,
5820             m_largest_value,
5821             "-0x1p+0",
5822             Status::OK,
5823             Category::Normal,
5824         ),
5825         (
5826             p_largest_value,
5827             p_smallest_value,
5828             "inf",
5829             overflow_status,
5830             Category::Infinity,
5831         ),
5832         (
5833             p_largest_value,
5834             m_smallest_value,
5835             "-inf",
5836             overflow_status,
5837             Category::Infinity,
5838         ),
5839         (
5840             p_largest_value,
5841             p_smallest_normalized,
5842             "inf",
5843             overflow_status,
5844             Category::Infinity,
5845         ),
5846         (
5847             p_largest_value,
5848             m_smallest_normalized,
5849             "-inf",
5850             overflow_status,
5851             Category::Infinity,
5852         ),
5853         (
5854             m_largest_value,
5855             p_inf,
5856             "-0x0p+0",
5857             Status::OK,
5858             Category::Zero,
5859         ),
5860         (m_largest_value, m_inf, "0x0p+0", Status::OK, Category::Zero),
5861         (
5862             m_largest_value,
5863             p_zero,
5864             "-inf",
5865             Status::DIV_BY_ZERO,
5866             Category::Infinity,
5867         ),
5868         (
5869             m_largest_value,
5870             m_zero,
5871             "inf",
5872             Status::DIV_BY_ZERO,
5873             Category::Infinity,
5874         ),
5875         (m_largest_value, qnan, "nan", Status::OK, Category::NaN),
5876         /*
5877 // See Note 1.
5878 (m_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
5879         */
5880         (
5881             m_largest_value,
5882             p_normal_value,
5883             "-0x1.fffffep+127",
5884             Status::OK,
5885             Category::Normal,
5886         ),
5887         (
5888             m_largest_value,
5889             m_normal_value,
5890             "0x1.fffffep+127",
5891             Status::OK,
5892             Category::Normal,
5893         ),
5894         (
5895             m_largest_value,
5896             p_largest_value,
5897             "-0x1p+0",
5898             Status::OK,
5899             Category::Normal,
5900         ),
5901         (
5902             m_largest_value,
5903             m_largest_value,
5904             "0x1p+0",
5905             Status::OK,
5906             Category::Normal,
5907         ),
5908         (
5909             m_largest_value,
5910             p_smallest_value,
5911             "-inf",
5912             overflow_status,
5913             Category::Infinity,
5914         ),
5915         (
5916             m_largest_value,
5917             m_smallest_value,
5918             "inf",
5919             overflow_status,
5920             Category::Infinity,
5921         ),
5922         (
5923             m_largest_value,
5924             p_smallest_normalized,
5925             "-inf",
5926             overflow_status,
5927             Category::Infinity,
5928         ),
5929         (
5930             m_largest_value,
5931             m_smallest_normalized,
5932             "inf",
5933             overflow_status,
5934             Category::Infinity,
5935         ),
5936         (
5937             p_smallest_value,
5938             p_inf,
5939             "0x0p+0",
5940             Status::OK,
5941             Category::Zero,
5942         ),
5943         (
5944             p_smallest_value,
5945             m_inf,
5946             "-0x0p+0",
5947             Status::OK,
5948             Category::Zero,
5949         ),
5950         (
5951             p_smallest_value,
5952             p_zero,
5953             "inf",
5954             Status::DIV_BY_ZERO,
5955             Category::Infinity,
5956         ),
5957         (
5958             p_smallest_value,
5959             m_zero,
5960             "-inf",
5961             Status::DIV_BY_ZERO,
5962             Category::Infinity,
5963         ),
5964         (p_smallest_value, qnan, "nan", Status::OK, Category::NaN),
5965         /*
5966 // See Note 1.
5967 (p_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
5968         */
5969         (
5970             p_smallest_value,
5971             p_normal_value,
5972             "0x1p-149",
5973             Status::OK,
5974             Category::Normal,
5975         ),
5976         (
5977             p_smallest_value,
5978             m_normal_value,
5979             "-0x1p-149",
5980             Status::OK,
5981             Category::Normal,
5982         ),
5983         (
5984             p_smallest_value,
5985             p_largest_value,
5986             "0x0p+0",
5987             underflow_status,
5988             Category::Zero,
5989         ),
5990         (
5991             p_smallest_value,
5992             m_largest_value,
5993             "-0x0p+0",
5994             underflow_status,
5995             Category::Zero,
5996         ),
5997         (
5998             p_smallest_value,
5999             p_smallest_value,
6000             "0x1p+0",
6001             Status::OK,
6002             Category::Normal,
6003         ),
6004         (
6005             p_smallest_value,
6006             m_smallest_value,
6007             "-0x1p+0",
6008             Status::OK,
6009             Category::Normal,
6010         ),
6011         (
6012             p_smallest_value,
6013             p_smallest_normalized,
6014             "0x1p-23",
6015             Status::OK,
6016             Category::Normal,
6017         ),
6018         (
6019             p_smallest_value,
6020             m_smallest_normalized,
6021             "-0x1p-23",
6022             Status::OK,
6023             Category::Normal,
6024         ),
6025         (
6026             m_smallest_value,
6027             p_inf,
6028             "-0x0p+0",
6029             Status::OK,
6030             Category::Zero,
6031         ),
6032         (
6033             m_smallest_value,
6034             m_inf,
6035             "0x0p+0",
6036             Status::OK,
6037             Category::Zero,
6038         ),
6039         (
6040             m_smallest_value,
6041             p_zero,
6042             "-inf",
6043             Status::DIV_BY_ZERO,
6044             Category::Infinity,
6045         ),
6046         (
6047             m_smallest_value,
6048             m_zero,
6049             "inf",
6050             Status::DIV_BY_ZERO,
6051             Category::Infinity,
6052         ),
6053         (m_smallest_value, qnan, "nan", Status::OK, Category::NaN),
6054         /*
6055 // See Note 1.
6056 (m_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
6057         */
6058         (
6059             m_smallest_value,
6060             p_normal_value,
6061             "-0x1p-149",
6062             Status::OK,
6063             Category::Normal,
6064         ),
6065         (
6066             m_smallest_value,
6067             m_normal_value,
6068             "0x1p-149",
6069             Status::OK,
6070             Category::Normal,
6071         ),
6072         (
6073             m_smallest_value,
6074             p_largest_value,
6075             "-0x0p+0",
6076             underflow_status,
6077             Category::Zero,
6078         ),
6079         (
6080             m_smallest_value,
6081             m_largest_value,
6082             "0x0p+0",
6083             underflow_status,
6084             Category::Zero,
6085         ),
6086         (
6087             m_smallest_value,
6088             p_smallest_value,
6089             "-0x1p+0",
6090             Status::OK,
6091             Category::Normal,
6092         ),
6093         (
6094             m_smallest_value,
6095             m_smallest_value,
6096             "0x1p+0",
6097             Status::OK,
6098             Category::Normal,
6099         ),
6100         (
6101             m_smallest_value,
6102             p_smallest_normalized,
6103             "-0x1p-23",
6104             Status::OK,
6105             Category::Normal,
6106         ),
6107         (
6108             m_smallest_value,
6109             m_smallest_normalized,
6110             "0x1p-23",
6111             Status::OK,
6112             Category::Normal,
6113         ),
6114         (
6115             p_smallest_normalized,
6116             p_inf,
6117             "0x0p+0",
6118             Status::OK,
6119             Category::Zero,
6120         ),
6121         (
6122             p_smallest_normalized,
6123             m_inf,
6124             "-0x0p+0",
6125             Status::OK,
6126             Category::Zero,
6127         ),
6128         (
6129             p_smallest_normalized,
6130             p_zero,
6131             "inf",
6132             Status::DIV_BY_ZERO,
6133             Category::Infinity,
6134         ),
6135         (
6136             p_smallest_normalized,
6137             m_zero,
6138             "-inf",
6139             Status::DIV_BY_ZERO,
6140             Category::Infinity,
6141         ),
6142         (
6143             p_smallest_normalized,
6144             qnan,
6145             "nan",
6146             Status::OK,
6147             Category::NaN,
6148         ),
6149         /*
6150 // See Note 1.
6151 (p_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
6152         */
6153         (
6154             p_smallest_normalized,
6155             p_normal_value,
6156             "0x1p-126",
6157             Status::OK,
6158             Category::Normal,
6159         ),
6160         (
6161             p_smallest_normalized,
6162             m_normal_value,
6163             "-0x1p-126",
6164             Status::OK,
6165             Category::Normal,
6166         ),
6167         (
6168             p_smallest_normalized,
6169             p_largest_value,
6170             "0x0p+0",
6171             underflow_status,
6172             Category::Zero,
6173         ),
6174         (
6175             p_smallest_normalized,
6176             m_largest_value,
6177             "-0x0p+0",
6178             underflow_status,
6179             Category::Zero,
6180         ),
6181         (
6182             p_smallest_normalized,
6183             p_smallest_value,
6184             "0x1p+23",
6185             Status::OK,
6186             Category::Normal,
6187         ),
6188         (
6189             p_smallest_normalized,
6190             m_smallest_value,
6191             "-0x1p+23",
6192             Status::OK,
6193             Category::Normal,
6194         ),
6195         (
6196             p_smallest_normalized,
6197             p_smallest_normalized,
6198             "0x1p+0",
6199             Status::OK,
6200             Category::Normal,
6201         ),
6202         (
6203             p_smallest_normalized,
6204             m_smallest_normalized,
6205             "-0x1p+0",
6206             Status::OK,
6207             Category::Normal,
6208         ),
6209         (
6210             m_smallest_normalized,
6211             p_inf,
6212             "-0x0p+0",
6213             Status::OK,
6214             Category::Zero,
6215         ),
6216         (
6217             m_smallest_normalized,
6218             m_inf,
6219             "0x0p+0",
6220             Status::OK,
6221             Category::Zero,
6222         ),
6223         (
6224             m_smallest_normalized,
6225             p_zero,
6226             "-inf",
6227             Status::DIV_BY_ZERO,
6228             Category::Infinity,
6229         ),
6230         (
6231             m_smallest_normalized,
6232             m_zero,
6233             "inf",
6234             Status::DIV_BY_ZERO,
6235             Category::Infinity,
6236         ),
6237         (
6238             m_smallest_normalized,
6239             qnan,
6240             "nan",
6241             Status::OK,
6242             Category::NaN,
6243         ),
6244         /*
6245 // See Note 1.
6246 (m_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
6247         */
6248         (
6249             m_smallest_normalized,
6250             p_normal_value,
6251             "-0x1p-126",
6252             Status::OK,
6253             Category::Normal,
6254         ),
6255         (
6256             m_smallest_normalized,
6257             m_normal_value,
6258             "0x1p-126",
6259             Status::OK,
6260             Category::Normal,
6261         ),
6262         (
6263             m_smallest_normalized,
6264             p_largest_value,
6265             "-0x0p+0",
6266             underflow_status,
6267             Category::Zero,
6268         ),
6269         (
6270             m_smallest_normalized,
6271             m_largest_value,
6272             "0x0p+0",
6273             underflow_status,
6274             Category::Zero,
6275         ),
6276         (
6277             m_smallest_normalized,
6278             p_smallest_value,
6279             "-0x1p+23",
6280             Status::OK,
6281             Category::Normal,
6282         ),
6283         (
6284             m_smallest_normalized,
6285             m_smallest_value,
6286             "0x1p+23",
6287             Status::OK,
6288             Category::Normal,
6289         ),
6290         (
6291             m_smallest_normalized,
6292             p_smallest_normalized,
6293             "-0x1p+0",
6294             Status::OK,
6295             Category::Normal,
6296         ),
6297         (
6298             m_smallest_normalized,
6299             m_smallest_normalized,
6300             "0x1p+0",
6301             Status::OK,
6302             Category::Normal,
6303         ),
6304     ];
6305
6306     for &(x, y, e_result, e_status, e_category) in &special_cases[..] {
6307         let status;
6308         let result = unpack!(status=, x / y);
6309         assert_eq!(status, e_status);
6310         assert_eq!(result.category(), e_category);
6311         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
6312     }
6313 }
6314
6315 #[test]
6316 fn operator_overloads() {
6317     // This is mostly testing that these operator overloads compile.
6318     let one = "0x1p+0".parse::<Single>().unwrap();
6319     let two = "0x2p+0".parse::<Single>().unwrap();
6320     assert!(two.bitwise_eq((one + one).value));
6321     assert!(one.bitwise_eq((two - one).value));
6322     assert!(two.bitwise_eq((one * two).value));
6323     assert!(one.bitwise_eq((two / two).value));
6324 }
6325
6326 #[test]
6327 fn abs() {
6328     let p_inf = Single::INFINITY;
6329     let m_inf = -Single::INFINITY;
6330     let p_zero = Single::ZERO;
6331     let m_zero = -Single::ZERO;
6332     let p_qnan = Single::NAN;
6333     let m_qnan = -Single::NAN;
6334     let p_snan = Single::snan(None);
6335     let m_snan = -Single::snan(None);
6336     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
6337     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
6338     let p_largest_value = Single::largest();
6339     let m_largest_value = -Single::largest();
6340     let p_smallest_value = Single::SMALLEST;
6341     let m_smallest_value = -Single::SMALLEST;
6342     let p_smallest_normalized = Single::smallest_normalized();
6343     let m_smallest_normalized = -Single::smallest_normalized();
6344
6345     assert!(p_inf.bitwise_eq(p_inf.abs()));
6346     assert!(p_inf.bitwise_eq(m_inf.abs()));
6347     assert!(p_zero.bitwise_eq(p_zero.abs()));
6348     assert!(p_zero.bitwise_eq(m_zero.abs()));
6349     assert!(p_qnan.bitwise_eq(p_qnan.abs()));
6350     assert!(p_qnan.bitwise_eq(m_qnan.abs()));
6351     assert!(p_snan.bitwise_eq(p_snan.abs()));
6352     assert!(p_snan.bitwise_eq(m_snan.abs()));
6353     assert!(p_normal_value.bitwise_eq(p_normal_value.abs()));
6354     assert!(p_normal_value.bitwise_eq(m_normal_value.abs()));
6355     assert!(p_largest_value.bitwise_eq(p_largest_value.abs()));
6356     assert!(p_largest_value.bitwise_eq(m_largest_value.abs()));
6357     assert!(p_smallest_value.bitwise_eq(p_smallest_value.abs()));
6358     assert!(p_smallest_value.bitwise_eq(m_smallest_value.abs()));
6359     assert!(p_smallest_normalized.bitwise_eq(
6360         p_smallest_normalized.abs(),
6361     ));
6362     assert!(p_smallest_normalized.bitwise_eq(
6363         m_smallest_normalized.abs(),
6364     ));
6365 }
6366
6367 #[test]
6368 fn neg() {
6369     let one = "1.0".parse::<Single>().unwrap();
6370     let neg_one = "-1.0".parse::<Single>().unwrap();
6371     let zero = Single::ZERO;
6372     let neg_zero = -Single::ZERO;
6373     let inf = Single::INFINITY;
6374     let neg_inf = -Single::INFINITY;
6375     let qnan = Single::NAN;
6376     let neg_qnan = -Single::NAN;
6377
6378     assert!(neg_one.bitwise_eq(-one));
6379     assert!(one.bitwise_eq(-neg_one));
6380     assert!(neg_zero.bitwise_eq(-zero));
6381     assert!(zero.bitwise_eq(-neg_zero));
6382     assert!(neg_inf.bitwise_eq(-inf));
6383     assert!(inf.bitwise_eq(-neg_inf));
6384     assert!(neg_inf.bitwise_eq(-inf));
6385     assert!(inf.bitwise_eq(-neg_inf));
6386     assert!(neg_qnan.bitwise_eq(-qnan));
6387     assert!(qnan.bitwise_eq(-neg_qnan));
6388 }
6389
6390 #[test]
6391 fn ilogb() {
6392     assert_eq!(-1074, Double::SMALLEST.ilogb());
6393     assert_eq!(-1074, (-Double::SMALLEST).ilogb());
6394     assert_eq!(
6395         -1023,
6396         "0x1.ffffffffffffep-1024".parse::<Double>().unwrap().ilogb()
6397     );
6398     assert_eq!(
6399         -1023,
6400         "0x1.ffffffffffffep-1023".parse::<Double>().unwrap().ilogb()
6401     );
6402     assert_eq!(
6403         -1023,
6404         "-0x1.ffffffffffffep-1023"
6405             .parse::<Double>()
6406             .unwrap()
6407             .ilogb()
6408     );
6409     assert_eq!(-51, "0x1p-51".parse::<Double>().unwrap().ilogb());
6410     assert_eq!(
6411         -1023,
6412         "0x1.c60f120d9f87cp-1023".parse::<Double>().unwrap().ilogb()
6413     );
6414     assert_eq!(-2, "0x0.ffffp-1".parse::<Double>().unwrap().ilogb());
6415     assert_eq!(-1023, "0x1.fffep-1023".parse::<Double>().unwrap().ilogb());
6416     assert_eq!(1023, Double::largest().ilogb());
6417     assert_eq!(1023, (-Double::largest()).ilogb());
6418
6419
6420     assert_eq!(0, "0x1p+0".parse::<Single>().unwrap().ilogb());
6421     assert_eq!(0, "-0x1p+0".parse::<Single>().unwrap().ilogb());
6422     assert_eq!(42, "0x1p+42".parse::<Single>().unwrap().ilogb());
6423     assert_eq!(-42, "0x1p-42".parse::<Single>().unwrap().ilogb());
6424
6425     assert_eq!(IEK_INF, Single::INFINITY.ilogb());
6426     assert_eq!(IEK_INF, (-Single::INFINITY).ilogb());
6427     assert_eq!(IEK_ZERO, Single::ZERO.ilogb());
6428     assert_eq!(IEK_ZERO, (-Single::ZERO).ilogb());
6429     assert_eq!(IEK_NAN, Single::NAN.ilogb());
6430     assert_eq!(IEK_NAN, Single::snan(None).ilogb());
6431
6432     assert_eq!(127, Single::largest().ilogb());
6433     assert_eq!(127, (-Single::largest()).ilogb());
6434
6435     assert_eq!(-149, Single::SMALLEST.ilogb());
6436     assert_eq!(-149, (-Single::SMALLEST).ilogb());
6437     assert_eq!(-126, Single::smallest_normalized().ilogb());
6438     assert_eq!(-126, (-Single::smallest_normalized()).ilogb());
6439 }
6440
6441 #[test]
6442 fn scalbn() {
6443     assert!("0x1p+0".parse::<Single>().unwrap().bitwise_eq(
6444         "0x1p+0".parse::<Single>().unwrap().scalbn(0),
6445     ));
6446     assert!("0x1p+42".parse::<Single>().unwrap().bitwise_eq(
6447         "0x1p+0".parse::<Single>().unwrap().scalbn(42),
6448     ));
6449     assert!("0x1p-42".parse::<Single>().unwrap().bitwise_eq(
6450         "0x1p+0".parse::<Single>().unwrap().scalbn(-42),
6451     ));
6452
6453     let p_inf = Single::INFINITY;
6454     let m_inf = -Single::INFINITY;
6455     let p_zero = Single::ZERO;
6456     let m_zero = -Single::ZERO;
6457     let p_qnan = Single::NAN;
6458     let m_qnan = -Single::NAN;
6459     let snan = Single::snan(None);
6460
6461     assert!(p_inf.bitwise_eq(p_inf.scalbn(0)));
6462     assert!(m_inf.bitwise_eq(m_inf.scalbn(0)));
6463     assert!(p_zero.bitwise_eq(p_zero.scalbn(0)));
6464     assert!(m_zero.bitwise_eq(m_zero.scalbn(0)));
6465     assert!(p_qnan.bitwise_eq(p_qnan.scalbn(0)));
6466     assert!(m_qnan.bitwise_eq(m_qnan.scalbn(0)));
6467     assert!(!snan.scalbn(0).is_signaling());
6468
6469     let scalbn_snan = snan.scalbn(1);
6470     assert!(scalbn_snan.is_nan() && !scalbn_snan.is_signaling());
6471
6472     // Make sure highest bit of payload is preserved.
6473     let payload = (1 << 50) | (1 << 49) | (1234 << 32) | 1;
6474
6475     let snan_with_payload = Double::snan(Some(payload));
6476     let quiet_payload = snan_with_payload.scalbn(1);
6477     assert!(quiet_payload.is_nan() && !quiet_payload.is_signaling());
6478     assert_eq!(payload, quiet_payload.to_bits() & ((1 << 51) - 1));
6479
6480     assert!(p_inf.bitwise_eq(
6481         "0x1p+0".parse::<Single>().unwrap().scalbn(128),
6482     ));
6483     assert!(m_inf.bitwise_eq(
6484         "-0x1p+0".parse::<Single>().unwrap().scalbn(128),
6485     ));
6486     assert!(p_inf.bitwise_eq(
6487         "0x1p+127".parse::<Single>().unwrap().scalbn(1),
6488     ));
6489     assert!(p_zero.bitwise_eq(
6490         "0x1p-127".parse::<Single>().unwrap().scalbn(-127),
6491     ));
6492     assert!(m_zero.bitwise_eq(
6493         "-0x1p-127".parse::<Single>().unwrap().scalbn(-127),
6494     ));
6495     assert!("-0x1p-149".parse::<Single>().unwrap().bitwise_eq(
6496         "-0x1p-127".parse::<Single>().unwrap().scalbn(-22),
6497     ));
6498     assert!(p_zero.bitwise_eq(
6499         "0x1p-126".parse::<Single>().unwrap().scalbn(-24),
6500     ));
6501
6502
6503     let smallest_f64 = Double::SMALLEST;
6504     let neg_smallest_f64 = -Double::SMALLEST;
6505
6506     let largest_f64 = Double::largest();
6507     let neg_largest_f64 = -Double::largest();
6508
6509     let largest_denormal_f64 = "0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
6510     let neg_largest_denormal_f64 = "-0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
6511
6512
6513     assert!(smallest_f64.bitwise_eq(
6514         "0x1p-1074".parse::<Double>().unwrap().scalbn(0),
6515     ));
6516     assert!(neg_smallest_f64.bitwise_eq(
6517         "-0x1p-1074".parse::<Double>().unwrap().scalbn(0),
6518     ));
6519
6520     assert!("0x1p+1023".parse::<Double>().unwrap().bitwise_eq(
6521         smallest_f64.scalbn(
6522             2097,
6523         ),
6524     ));
6525
6526     assert!(smallest_f64.scalbn(-2097).is_pos_zero());
6527     assert!(smallest_f64.scalbn(-2098).is_pos_zero());
6528     assert!(smallest_f64.scalbn(-2099).is_pos_zero());
6529     assert!("0x1p+1022".parse::<Double>().unwrap().bitwise_eq(
6530         smallest_f64.scalbn(
6531             2096,
6532         ),
6533     ));
6534     assert!("0x1p+1023".parse::<Double>().unwrap().bitwise_eq(
6535         smallest_f64.scalbn(
6536             2097,
6537         ),
6538     ));
6539     assert!(smallest_f64.scalbn(2098).is_infinite());
6540     assert!(smallest_f64.scalbn(2099).is_infinite());
6541
6542     // Test for integer overflows when adding to exponent.
6543     assert!(smallest_f64.scalbn(-ExpInt::max_value()).is_pos_zero());
6544     assert!(largest_f64.scalbn(ExpInt::max_value()).is_infinite());
6545
6546     assert!(largest_denormal_f64.bitwise_eq(
6547         largest_denormal_f64.scalbn(0),
6548     ));
6549     assert!(neg_largest_denormal_f64.bitwise_eq(
6550         neg_largest_denormal_f64.scalbn(0),
6551     ));
6552
6553     assert!(
6554         "0x1.ffffffffffffep-1022"
6555             .parse::<Double>()
6556             .unwrap()
6557             .bitwise_eq(largest_denormal_f64.scalbn(1))
6558     );
6559     assert!(
6560         "-0x1.ffffffffffffep-1021"
6561             .parse::<Double>()
6562             .unwrap()
6563             .bitwise_eq(neg_largest_denormal_f64.scalbn(2))
6564     );
6565
6566     assert!(
6567         "0x1.ffffffffffffep+1"
6568             .parse::<Double>()
6569             .unwrap()
6570             .bitwise_eq(largest_denormal_f64.scalbn(1024))
6571     );
6572     assert!(largest_denormal_f64.scalbn(-1023).is_pos_zero());
6573     assert!(largest_denormal_f64.scalbn(-1024).is_pos_zero());
6574     assert!(largest_denormal_f64.scalbn(-2048).is_pos_zero());
6575     assert!(largest_denormal_f64.scalbn(2047).is_infinite());
6576     assert!(largest_denormal_f64.scalbn(2098).is_infinite());
6577     assert!(largest_denormal_f64.scalbn(2099).is_infinite());
6578
6579     assert!(
6580         "0x1.ffffffffffffep-2"
6581             .parse::<Double>()
6582             .unwrap()
6583             .bitwise_eq(largest_denormal_f64.scalbn(1021))
6584     );
6585     assert!(
6586         "0x1.ffffffffffffep-1"
6587             .parse::<Double>()
6588             .unwrap()
6589             .bitwise_eq(largest_denormal_f64.scalbn(1022))
6590     );
6591     assert!(
6592         "0x1.ffffffffffffep+0"
6593             .parse::<Double>()
6594             .unwrap()
6595             .bitwise_eq(largest_denormal_f64.scalbn(1023))
6596     );
6597     assert!(
6598         "0x1.ffffffffffffep+1023"
6599             .parse::<Double>()
6600             .unwrap()
6601             .bitwise_eq(largest_denormal_f64.scalbn(2046))
6602     );
6603     assert!("0x1p+974".parse::<Double>().unwrap().bitwise_eq(
6604         smallest_f64.scalbn(
6605             2048,
6606         ),
6607     ));
6608
6609     let random_denormal_f64 = "0x1.c60f120d9f87cp+51".parse::<Double>().unwrap();
6610     assert!(
6611         "0x1.c60f120d9f87cp-972"
6612             .parse::<Double>()
6613             .unwrap()
6614             .bitwise_eq(random_denormal_f64.scalbn(-1023))
6615     );
6616     assert!(
6617         "0x1.c60f120d9f87cp-1"
6618             .parse::<Double>()
6619             .unwrap()
6620             .bitwise_eq(random_denormal_f64.scalbn(-52))
6621     );
6622     assert!(
6623         "0x1.c60f120d9f87cp-2"
6624             .parse::<Double>()
6625             .unwrap()
6626             .bitwise_eq(random_denormal_f64.scalbn(-53))
6627     );
6628     assert!(
6629         "0x1.c60f120d9f87cp+0"
6630             .parse::<Double>()
6631             .unwrap()
6632             .bitwise_eq(random_denormal_f64.scalbn(-51))
6633     );
6634
6635     assert!(random_denormal_f64.scalbn(-2097).is_pos_zero());
6636     assert!(random_denormal_f64.scalbn(-2090).is_pos_zero());
6637
6638
6639     assert!("-0x1p-1073".parse::<Double>().unwrap().bitwise_eq(
6640         neg_largest_f64.scalbn(-2097),
6641     ));
6642
6643     assert!("-0x1p-1024".parse::<Double>().unwrap().bitwise_eq(
6644         neg_largest_f64.scalbn(-2048),
6645     ));
6646
6647     assert!("0x1p-1073".parse::<Double>().unwrap().bitwise_eq(
6648         largest_f64.scalbn(
6649             -2097,
6650         ),
6651     ));
6652
6653     assert!("0x1p-1074".parse::<Double>().unwrap().bitwise_eq(
6654         largest_f64.scalbn(
6655             -2098,
6656         ),
6657     ));
6658     assert!("-0x1p-1074".parse::<Double>().unwrap().bitwise_eq(
6659         neg_largest_f64.scalbn(-2098),
6660     ));
6661     assert!(neg_largest_f64.scalbn(-2099).is_neg_zero());
6662     assert!(largest_f64.scalbn(1).is_infinite());
6663
6664
6665     assert!("0x1p+0".parse::<Double>().unwrap().bitwise_eq(
6666         "0x1p+52".parse::<Double>().unwrap().scalbn(-52),
6667     ));
6668
6669     assert!("0x1p-103".parse::<Double>().unwrap().bitwise_eq(
6670         "0x1p-51".parse::<Double>().unwrap().scalbn(-52),
6671     ));
6672 }
6673
6674 #[test]
6675 fn frexp() {
6676     let p_zero = Double::ZERO;
6677     let m_zero = -Double::ZERO;
6678     let one = Double::from_f64(1.0);
6679     let m_one = Double::from_f64(-1.0);
6680
6681     let largest_denormal = "0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
6682     let neg_largest_denormal = "-0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
6683
6684     let smallest = Double::SMALLEST;
6685     let neg_smallest = -Double::SMALLEST;
6686
6687     let largest = Double::largest();
6688     let neg_largest = -Double::largest();
6689
6690     let p_inf = Double::INFINITY;
6691     let m_inf = -Double::INFINITY;
6692
6693     let p_qnan = Double::NAN;
6694     let m_qnan = -Double::NAN;
6695     let snan = Double::snan(None);
6696
6697     // Make sure highest bit of payload is preserved.
6698     let payload = (1 << 50) | (1 << 49) | (1234 << 32) | 1;
6699
6700     let snan_with_payload = Double::snan(Some(payload));
6701
6702     let mut exp = 0;
6703
6704     let frac = p_zero.frexp(&mut exp);
6705     assert_eq!(0, exp);
6706     assert!(frac.is_pos_zero());
6707
6708     let frac = m_zero.frexp(&mut exp);
6709     assert_eq!(0, exp);
6710     assert!(frac.is_neg_zero());
6711
6712
6713     let frac = one.frexp(&mut exp);
6714     assert_eq!(1, exp);
6715     assert!("0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
6716
6717     let frac = m_one.frexp(&mut exp);
6718     assert_eq!(1, exp);
6719     assert!("-0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
6720
6721     let frac = largest_denormal.frexp(&mut exp);
6722     assert_eq!(-1022, exp);
6723     assert!(
6724         "0x1.ffffffffffffep-1"
6725             .parse::<Double>()
6726             .unwrap()
6727             .bitwise_eq(frac)
6728     );
6729
6730     let frac = neg_largest_denormal.frexp(&mut exp);
6731     assert_eq!(-1022, exp);
6732     assert!(
6733         "-0x1.ffffffffffffep-1"
6734             .parse::<Double>()
6735             .unwrap()
6736             .bitwise_eq(frac)
6737     );
6738
6739
6740     let frac = smallest.frexp(&mut exp);
6741     assert_eq!(-1073, exp);
6742     assert!("0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
6743
6744     let frac = neg_smallest.frexp(&mut exp);
6745     assert_eq!(-1073, exp);
6746     assert!("-0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
6747
6748
6749     let frac = largest.frexp(&mut exp);
6750     assert_eq!(1024, exp);
6751     assert!(
6752         "0x1.fffffffffffffp-1"
6753             .parse::<Double>()
6754             .unwrap()
6755             .bitwise_eq(frac)
6756     );
6757
6758     let frac = neg_largest.frexp(&mut exp);
6759     assert_eq!(1024, exp);
6760     assert!(
6761         "-0x1.fffffffffffffp-1"
6762             .parse::<Double>()
6763             .unwrap()
6764             .bitwise_eq(frac)
6765     );
6766
6767
6768     let frac = p_inf.frexp(&mut exp);
6769     assert_eq!(IEK_INF, exp);
6770     assert!(frac.is_infinite() && !frac.is_negative());
6771
6772     let frac = m_inf.frexp(&mut exp);
6773     assert_eq!(IEK_INF, exp);
6774     assert!(frac.is_infinite() && frac.is_negative());
6775
6776     let frac = p_qnan.frexp(&mut exp);
6777     assert_eq!(IEK_NAN, exp);
6778     assert!(frac.is_nan());
6779
6780     let frac = m_qnan.frexp(&mut exp);
6781     assert_eq!(IEK_NAN, exp);
6782     assert!(frac.is_nan());
6783
6784     let frac = snan.frexp(&mut exp);
6785     assert_eq!(IEK_NAN, exp);
6786     assert!(frac.is_nan() && !frac.is_signaling());
6787
6788     let frac = snan_with_payload.frexp(&mut exp);
6789     assert_eq!(IEK_NAN, exp);
6790     assert!(frac.is_nan() && !frac.is_signaling());
6791     assert_eq!(payload, frac.to_bits() & ((1 << 51) - 1));
6792
6793     let frac = "0x0.ffffp-1".parse::<Double>().unwrap().frexp(&mut exp);
6794     assert_eq!(-1, exp);
6795     assert!("0x1.fffep-1".parse::<Double>().unwrap().bitwise_eq(frac));
6796
6797     let frac = "0x1p-51".parse::<Double>().unwrap().frexp(&mut exp);
6798     assert_eq!(-50, exp);
6799     assert!("0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
6800
6801     let frac = "0x1.c60f120d9f87cp+51".parse::<Double>().unwrap().frexp(
6802         &mut exp,
6803     );
6804     assert_eq!(52, exp);
6805     assert!(
6806         "0x1.c60f120d9f87cp-1"
6807             .parse::<Double>()
6808             .unwrap()
6809             .bitwise_eq(frac)
6810     );
6811 }
6812
6813 #[test]
6814 fn modulo() {
6815     let mut status;
6816     {
6817         let f1 = "1.5".parse::<Double>().unwrap();
6818         let f2 = "1.0".parse::<Double>().unwrap();
6819         let expected = "0.5".parse::<Double>().unwrap();
6820         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
6821         assert_eq!(status, Status::OK);
6822     }
6823     {
6824         let f1 = "0.5".parse::<Double>().unwrap();
6825         let f2 = "1.0".parse::<Double>().unwrap();
6826         let expected = "0.5".parse::<Double>().unwrap();
6827         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
6828         assert_eq!(status, Status::OK);
6829     }
6830     {
6831         let f1 = "0x1.3333333333333p-2".parse::<Double>().unwrap(); // 0.3
6832         let f2 = "0x1.47ae147ae147bp-7".parse::<Double>().unwrap(); // 0.01
6833         // 0.009999999999999983
6834         let expected = "0x1.47ae147ae1471p-7".parse::<Double>().unwrap();
6835         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
6836         assert_eq!(status, Status::OK);
6837     }
6838     {
6839         let f1 = "0x1p64".parse::<Double>().unwrap(); // 1.8446744073709552e19
6840         let f2 = "1.5".parse::<Double>().unwrap();
6841         let expected = "1.0".parse::<Double>().unwrap();
6842         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
6843         assert_eq!(status, Status::OK);
6844     }
6845     {
6846         let f1 = "0x1p1000".parse::<Double>().unwrap();
6847         let f2 = "0x1p-1000".parse::<Double>().unwrap();
6848         let expected = "0.0".parse::<Double>().unwrap();
6849         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
6850         assert_eq!(status, Status::OK);
6851     }
6852     {
6853         let f1 = "0.0".parse::<Double>().unwrap();
6854         let f2 = "1.0".parse::<Double>().unwrap();
6855         let expected = "0.0".parse::<Double>().unwrap();
6856         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
6857         assert_eq!(status, Status::OK);
6858     }
6859     {
6860         let f1 = "1.0".parse::<Double>().unwrap();
6861         let f2 = "0.0".parse::<Double>().unwrap();
6862         assert!(unpack!(status=, f1 % f2).is_nan());
6863         assert_eq!(status, Status::INVALID_OP);
6864     }
6865     {
6866         let f1 = "0.0".parse::<Double>().unwrap();
6867         let f2 = "0.0".parse::<Double>().unwrap();
6868         assert!(unpack!(status=, f1 % f2).is_nan());
6869         assert_eq!(status, Status::INVALID_OP);
6870     }
6871     {
6872         let f1 = Double::INFINITY;
6873         let f2 = "1.0".parse::<Double>().unwrap();
6874         assert!(unpack!(status=, f1 % f2).is_nan());
6875         assert_eq!(status, Status::INVALID_OP);
6876     }
6877 }