]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_apfloat/tests/ieee.rs
Rollup merge of #80918 - yoshuawuyts:int-log2, r=m-ou-se
[rust.git] / compiler / rustc_apfloat / tests / ieee.rs
1 // ignore-tidy-filelength
2
3 use rustc_apfloat::ieee::{Double, Half, Quad, Single, X87DoubleExtended};
4 use rustc_apfloat::unpack;
5 use rustc_apfloat::{Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
6 use rustc_apfloat::{Float, FloatConvert, ParseError, Round, Status};
7
8 trait SingleExt {
9     fn from_f32(input: f32) -> Self;
10     fn to_f32(self) -> f32;
11 }
12
13 impl SingleExt for Single {
14     fn from_f32(input: f32) -> Self {
15         Self::from_bits(input.to_bits() as u128)
16     }
17
18     fn to_f32(self) -> f32 {
19         f32::from_bits(self.to_bits() as u32)
20     }
21 }
22
23 trait DoubleExt {
24     fn from_f64(input: f64) -> Self;
25     fn to_f64(self) -> f64;
26 }
27
28 impl DoubleExt for Double {
29     fn from_f64(input: f64) -> Self {
30         Self::from_bits(input.to_bits() as u128)
31     }
32
33     fn to_f64(self) -> f64 {
34         f64::from_bits(self.to_bits() as u64)
35     }
36 }
37
38 #[test]
39 fn is_signaling() {
40     // We test qNaN, -qNaN, +sNaN, -sNaN with and without payloads.
41     let payload = 4;
42     assert!(!Single::qnan(None).is_signaling());
43     assert!(!(-Single::qnan(None)).is_signaling());
44     assert!(!Single::qnan(Some(payload)).is_signaling());
45     assert!(!(-Single::qnan(Some(payload))).is_signaling());
46     assert!(Single::snan(None).is_signaling());
47     assert!((-Single::snan(None)).is_signaling());
48     assert!(Single::snan(Some(payload)).is_signaling());
49     assert!((-Single::snan(Some(payload))).is_signaling());
50 }
51
52 #[test]
53 fn next() {
54     // 1. Test Special Cases Values.
55     //
56     // Test all special values for nextUp and nextDown perscribed by IEEE-754R
57     // 2008. These are:
58     //   1. +inf
59     //   2. -inf
60     //   3. largest
61     //   4. -largest
62     //   5. smallest
63     //   6. -smallest
64     //   7. qNaN
65     //   8. sNaN
66     //   9. +0
67     //   10. -0
68
69     let mut status;
70
71     // nextUp(+inf) = +inf.
72     let test = unpack!(status=, Quad::INFINITY.next_up());
73     let expected = Quad::INFINITY;
74     assert_eq!(status, Status::OK);
75     assert!(test.is_infinite());
76     assert!(!test.is_negative());
77     assert!(test.bitwise_eq(expected));
78
79     // nextDown(+inf) = -nextUp(-inf) = -(-largest) = largest
80     let test = unpack!(status=, Quad::INFINITY.next_down());
81     let expected = Quad::largest();
82     assert_eq!(status, Status::OK);
83     assert!(!test.is_negative());
84     assert!(test.bitwise_eq(expected));
85
86     // nextUp(-inf) = -largest
87     let test = unpack!(status=, (-Quad::INFINITY).next_up());
88     let expected = -Quad::largest();
89     assert_eq!(status, Status::OK);
90     assert!(test.is_negative());
91     assert!(test.bitwise_eq(expected));
92
93     // nextDown(-inf) = -nextUp(+inf) = -(+inf) = -inf.
94     let test = unpack!(status=, (-Quad::INFINITY).next_down());
95     let expected = -Quad::INFINITY;
96     assert_eq!(status, Status::OK);
97     assert!(test.is_infinite() && test.is_negative());
98     assert!(test.bitwise_eq(expected));
99
100     // nextUp(largest) = +inf
101     let test = unpack!(status=, Quad::largest().next_up());
102     let expected = Quad::INFINITY;
103     assert_eq!(status, Status::OK);
104     assert!(test.is_infinite() && !test.is_negative());
105     assert!(test.bitwise_eq(expected));
106
107     // nextDown(largest) = -nextUp(-largest)
108     //                        = -(-largest + inc)
109     //                        = largest - inc.
110     let test = unpack!(status=, Quad::largest().next_down());
111     let expected = "0x1.fffffffffffffffffffffffffffep+16383".parse::<Quad>().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".parse::<Quad>().unwrap();
119     assert_eq!(status, Status::OK);
120     assert!(test.bitwise_eq(expected));
121
122     // nextDown(-largest) = -nextUp(largest) = -(inf) = -inf.
123     let test = unpack!(status=, (-Quad::largest()).next_down());
124     let expected = -Quad::INFINITY;
125     assert_eq!(status, Status::OK);
126     assert!(test.is_infinite() && test.is_negative());
127     assert!(test.bitwise_eq(expected));
128
129     // nextUp(smallest) = smallest + inc.
130     let test = unpack!(status=, "0x0.0000000000000000000000000001p-16382"
131         .parse::<Quad>()
132         .unwrap()
133         .next_up());
134     let expected = "0x0.0000000000000000000000000002p-16382".parse::<Quad>().unwrap();
135     assert_eq!(status, Status::OK);
136     assert!(test.bitwise_eq(expected));
137
138     // nextDown(smallest) = -nextUp(-smallest) = -(-0) = +0.
139     let test = unpack!(status=, "0x0.0000000000000000000000000001p-16382"
140         .parse::<Quad>()
141         .unwrap()
142         .next_down());
143     let expected = Quad::ZERO;
144     assert_eq!(status, Status::OK);
145     assert!(test.is_pos_zero());
146     assert!(test.bitwise_eq(expected));
147
148     // nextUp(-smallest) = -0.
149     let test = unpack!(status=, "-0x0.0000000000000000000000000001p-16382"
150         .parse::<Quad>()
151         .unwrap()
152         .next_up());
153     let expected = -Quad::ZERO;
154     assert_eq!(status, Status::OK);
155     assert!(test.is_neg_zero());
156     assert!(test.bitwise_eq(expected));
157
158     // nextDown(-smallest) = -nextUp(smallest) = -smallest - inc.
159     let test = unpack!(status=, "-0x0.0000000000000000000000000001p-16382"
160         .parse::<Quad>()
161         .unwrap()
162         .next_down());
163     let expected = "-0x0.0000000000000000000000000002p-16382".parse::<Quad>().unwrap();
164     assert_eq!(status, Status::OK);
165     assert!(test.bitwise_eq(expected));
166
167     // nextUp(qNaN) = qNaN
168     let test = unpack!(status=, Quad::qnan(None).next_up());
169     let expected = Quad::qnan(None);
170     assert_eq!(status, Status::OK);
171     assert!(test.bitwise_eq(expected));
172
173     // nextDown(qNaN) = qNaN
174     let test = unpack!(status=, Quad::qnan(None).next_down());
175     let expected = Quad::qnan(None);
176     assert_eq!(status, Status::OK);
177     assert!(test.bitwise_eq(expected));
178
179     // nextUp(sNaN) = qNaN
180     let test = unpack!(status=, Quad::snan(None).next_up());
181     let expected = Quad::qnan(None);
182     assert_eq!(status, Status::INVALID_OP);
183     assert!(test.bitwise_eq(expected));
184
185     // nextDown(sNaN) = qNaN
186     let test = unpack!(status=, Quad::snan(None).next_down());
187     let expected = Quad::qnan(None);
188     assert_eq!(status, Status::INVALID_OP);
189     assert!(test.bitwise_eq(expected));
190
191     // nextUp(+0) = +smallest
192     let test = unpack!(status=, Quad::ZERO.next_up());
193     let expected = Quad::SMALLEST;
194     assert_eq!(status, Status::OK);
195     assert!(test.bitwise_eq(expected));
196
197     // nextDown(+0) = -nextUp(-0) = -smallest
198     let test = unpack!(status=, Quad::ZERO.next_down());
199     let expected = -Quad::SMALLEST;
200     assert_eq!(status, Status::OK);
201     assert!(test.bitwise_eq(expected));
202
203     // nextUp(-0) = +smallest
204     let test = unpack!(status=, (-Quad::ZERO).next_up());
205     let expected = Quad::SMALLEST;
206     assert_eq!(status, Status::OK);
207     assert!(test.bitwise_eq(expected));
208
209     // nextDown(-0) = -nextUp(0) = -smallest
210     let test = unpack!(status=, (-Quad::ZERO).next_down());
211     let expected = -Quad::SMALLEST;
212     assert_eq!(status, Status::OK);
213     assert!(test.bitwise_eq(expected));
214
215     // 2. Binade Boundary Tests.
216
217     // 2a. Test denormal <-> normal binade boundaries.
218     //     * nextUp(+Largest Denormal) -> +Smallest Normal.
219     //     * nextDown(-Largest Denormal) -> -Smallest Normal.
220     //     * nextUp(-Smallest Normal) -> -Largest Denormal.
221     //     * nextDown(+Smallest Normal) -> +Largest Denormal.
222
223     // nextUp(+Largest Denormal) -> +Smallest Normal.
224     let test = unpack!(status=, "0x0.ffffffffffffffffffffffffffffp-16382"
225         .parse::<Quad>()
226         .unwrap()
227         .next_up());
228     let expected = "0x1.0000000000000000000000000000p-16382".parse::<Quad>().unwrap();
229     assert_eq!(status, Status::OK);
230     assert!(!test.is_denormal());
231     assert!(test.bitwise_eq(expected));
232
233     // nextDown(-Largest Denormal) -> -Smallest Normal.
234     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffffffffp-16382"
235         .parse::<Quad>()
236         .unwrap()
237         .next_down());
238     let expected = "-0x1.0000000000000000000000000000p-16382".parse::<Quad>().unwrap();
239     assert_eq!(status, Status::OK);
240     assert!(!test.is_denormal());
241     assert!(test.bitwise_eq(expected));
242
243     // nextUp(-Smallest Normal) -> -Largest Denormal.
244     let test = unpack!(status=, "-0x1.0000000000000000000000000000p-16382"
245         .parse::<Quad>()
246         .unwrap()
247         .next_up());
248     let expected = "-0x0.ffffffffffffffffffffffffffffp-16382".parse::<Quad>().unwrap();
249     assert_eq!(status, Status::OK);
250     assert!(test.is_denormal());
251     assert!(test.bitwise_eq(expected));
252
253     // nextDown(+Smallest Normal) -> +Largest Denormal.
254     let test = unpack!(status=, "+0x1.0000000000000000000000000000p-16382"
255         .parse::<Quad>()
256         .unwrap()
257         .next_down());
258     let expected = "+0x0.ffffffffffffffffffffffffffffp-16382".parse::<Quad>().unwrap();
259     assert_eq!(status, Status::OK);
260     assert!(test.is_denormal());
261     assert!(test.bitwise_eq(expected));
262
263     // 2b. Test normal <-> normal binade boundaries.
264     //     * nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
265     //     * nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
266     //     * nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
267     //     * nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
268
269     // nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
270     let test = unpack!(status=, "-0x1p+1".parse::<Quad>().unwrap().next_up());
271     let expected = "-0x1.ffffffffffffffffffffffffffffp+0".parse::<Quad>().unwrap();
272     assert_eq!(status, Status::OK);
273     assert!(test.bitwise_eq(expected));
274
275     // nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
276     let test = unpack!(status=, "0x1p+1".parse::<Quad>().unwrap().next_down());
277     let expected = "0x1.ffffffffffffffffffffffffffffp+0".parse::<Quad>().unwrap();
278     assert_eq!(status, Status::OK);
279     assert!(test.bitwise_eq(expected));
280
281     // nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
282     let test = unpack!(status=, "0x1.ffffffffffffffffffffffffffffp+0"
283         .parse::<Quad>()
284         .unwrap()
285         .next_up());
286     let expected = "0x1p+1".parse::<Quad>().unwrap();
287     assert_eq!(status, Status::OK);
288     assert!(test.bitwise_eq(expected));
289
290     // nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
291     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffffffffp+0"
292         .parse::<Quad>()
293         .unwrap()
294         .next_down());
295     let expected = "-0x1p+1".parse::<Quad>().unwrap();
296     assert_eq!(status, Status::OK);
297     assert!(test.bitwise_eq(expected));
298
299     // 2c. Test using next at binade boundaries with a direction away from the
300     // binade boundary. Away from denormal <-> normal boundaries.
301     //
302     // This is to make sure that even though we are at a binade boundary, since
303     // we are rounding away, we do not trigger the binade boundary code. Thus we
304     // test:
305     //   * nextUp(-Largest Denormal) -> -Largest Denormal + inc.
306     //   * nextDown(+Largest Denormal) -> +Largest Denormal - inc.
307     //   * nextUp(+Smallest Normal) -> +Smallest Normal + inc.
308     //   * nextDown(-Smallest Normal) -> -Smallest Normal - inc.
309
310     // nextUp(-Largest Denormal) -> -Largest Denormal + inc.
311     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffffffffp-16382"
312         .parse::<Quad>()
313         .unwrap()
314         .next_up());
315     let expected = "-0x0.fffffffffffffffffffffffffffep-16382".parse::<Quad>().unwrap();
316     assert_eq!(status, Status::OK);
317     assert!(test.is_denormal());
318     assert!(test.is_negative());
319     assert!(test.bitwise_eq(expected));
320
321     // nextDown(+Largest Denormal) -> +Largest Denormal - inc.
322     let test = unpack!(status=, "0x0.ffffffffffffffffffffffffffffp-16382"
323         .parse::<Quad>()
324         .unwrap()
325         .next_down());
326     let expected = "0x0.fffffffffffffffffffffffffffep-16382".parse::<Quad>().unwrap();
327     assert_eq!(status, Status::OK);
328     assert!(test.is_denormal());
329     assert!(!test.is_negative());
330     assert!(test.bitwise_eq(expected));
331
332     // nextUp(+Smallest Normal) -> +Smallest Normal + inc.
333     let test = unpack!(status=, "0x1.0000000000000000000000000000p-16382"
334         .parse::<Quad>()
335         .unwrap()
336         .next_up());
337     let expected = "0x1.0000000000000000000000000001p-16382".parse::<Quad>().unwrap();
338     assert_eq!(status, Status::OK);
339     assert!(!test.is_denormal());
340     assert!(!test.is_negative());
341     assert!(test.bitwise_eq(expected));
342
343     // nextDown(-Smallest Normal) -> -Smallest Normal - inc.
344     let test = unpack!(status=, "-0x1.0000000000000000000000000000p-16382"
345         .parse::<Quad>()
346         .unwrap()
347         .next_down());
348     let expected = "-0x1.0000000000000000000000000001p-16382".parse::<Quad>().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     // 2d. Test values which cause our exponent to go to min exponent. This
355     // is to ensure that guards in the code to check for min exponent
356     // trigger properly.
357     //     * nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
358     //     * nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
359     //         -0x1p-16381
360     //     * nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16382
361     //     * nextDown(0x1p-16382) -> 0x1.ffffffffffffffffffffffffffffp-16382
362
363     // nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
364     let test = unpack!(status=, "-0x1p-16381".parse::<Quad>().unwrap().next_up());
365     let expected = "-0x1.ffffffffffffffffffffffffffffp-16382".parse::<Quad>().unwrap();
366     assert_eq!(status, Status::OK);
367     assert!(test.bitwise_eq(expected));
368
369     // nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
370     //         -0x1p-16381
371     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffffffffp-16382"
372         .parse::<Quad>()
373         .unwrap()
374         .next_down());
375     let expected = "-0x1p-16381".parse::<Quad>().unwrap();
376     assert_eq!(status, Status::OK);
377     assert!(test.bitwise_eq(expected));
378
379     // nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16381
380     let test = unpack!(status=, "0x1.ffffffffffffffffffffffffffffp-16382"
381         .parse::<Quad>()
382         .unwrap()
383         .next_up());
384     let expected = "0x1p-16381".parse::<Quad>().unwrap();
385     assert_eq!(status, Status::OK);
386     assert!(test.bitwise_eq(expected));
387
388     // nextDown(0x1p-16381) -> 0x1.ffffffffffffffffffffffffffffp-16382
389     let test = unpack!(status=, "0x1p-16381".parse::<Quad>().unwrap().next_down());
390     let expected = "0x1.ffffffffffffffffffffffffffffp-16382".parse::<Quad>().unwrap();
391     assert_eq!(status, Status::OK);
392     assert!(test.bitwise_eq(expected));
393
394     // 3. Now we test both denormal/normal computation which will not cause us
395     // to go across binade boundaries. Specifically we test:
396     //   * nextUp(+Denormal) -> +Denormal.
397     //   * nextDown(+Denormal) -> +Denormal.
398     //   * nextUp(-Denormal) -> -Denormal.
399     //   * nextDown(-Denormal) -> -Denormal.
400     //   * nextUp(+Normal) -> +Normal.
401     //   * nextDown(+Normal) -> +Normal.
402     //   * nextUp(-Normal) -> -Normal.
403     //   * nextDown(-Normal) -> -Normal.
404
405     // nextUp(+Denormal) -> +Denormal.
406     let test = unpack!(status=, "0x0.ffffffffffffffffffffffff000cp-16382"
407         .parse::<Quad>()
408         .unwrap()
409         .next_up());
410     let expected = "0x0.ffffffffffffffffffffffff000dp-16382".parse::<Quad>().unwrap();
411     assert_eq!(status, Status::OK);
412     assert!(test.is_denormal());
413     assert!(!test.is_negative());
414     assert!(test.bitwise_eq(expected));
415
416     // nextDown(+Denormal) -> +Denormal.
417     let test = unpack!(status=, "0x0.ffffffffffffffffffffffff000cp-16382"
418         .parse::<Quad>()
419         .unwrap()
420         .next_down());
421     let expected = "0x0.ffffffffffffffffffffffff000bp-16382".parse::<Quad>().unwrap();
422     assert_eq!(status, Status::OK);
423     assert!(test.is_denormal());
424     assert!(!test.is_negative());
425     assert!(test.bitwise_eq(expected));
426
427     // nextUp(-Denormal) -> -Denormal.
428     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffff000cp-16382"
429         .parse::<Quad>()
430         .unwrap()
431         .next_up());
432     let expected = "-0x0.ffffffffffffffffffffffff000bp-16382".parse::<Quad>().unwrap();
433     assert_eq!(status, Status::OK);
434     assert!(test.is_denormal());
435     assert!(test.is_negative());
436     assert!(test.bitwise_eq(expected));
437
438     // nextDown(-Denormal) -> -Denormal
439     let test = unpack!(status=, "-0x0.ffffffffffffffffffffffff000cp-16382"
440         .parse::<Quad>()
441         .unwrap()
442         .next_down());
443     let expected = "-0x0.ffffffffffffffffffffffff000dp-16382".parse::<Quad>().unwrap();
444     assert_eq!(status, Status::OK);
445     assert!(test.is_denormal());
446     assert!(test.is_negative());
447     assert!(test.bitwise_eq(expected));
448
449     // nextUp(+Normal) -> +Normal.
450     let test = unpack!(status=, "0x1.ffffffffffffffffffffffff000cp-16000"
451         .parse::<Quad>()
452         .unwrap()
453         .next_up());
454     let expected = "0x1.ffffffffffffffffffffffff000dp-16000".parse::<Quad>().unwrap();
455     assert_eq!(status, Status::OK);
456     assert!(!test.is_denormal());
457     assert!(!test.is_negative());
458     assert!(test.bitwise_eq(expected));
459
460     // nextDown(+Normal) -> +Normal.
461     let test = unpack!(status=, "0x1.ffffffffffffffffffffffff000cp-16000"
462         .parse::<Quad>()
463         .unwrap()
464         .next_down());
465     let expected = "0x1.ffffffffffffffffffffffff000bp-16000".parse::<Quad>().unwrap();
466     assert_eq!(status, Status::OK);
467     assert!(!test.is_denormal());
468     assert!(!test.is_negative());
469     assert!(test.bitwise_eq(expected));
470
471     // nextUp(-Normal) -> -Normal.
472     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffff000cp-16000"
473         .parse::<Quad>()
474         .unwrap()
475         .next_up());
476     let expected = "-0x1.ffffffffffffffffffffffff000bp-16000".parse::<Quad>().unwrap();
477     assert_eq!(status, Status::OK);
478     assert!(!test.is_denormal());
479     assert!(test.is_negative());
480     assert!(test.bitwise_eq(expected));
481
482     // nextDown(-Normal) -> -Normal.
483     let test = unpack!(status=, "-0x1.ffffffffffffffffffffffff000cp-16000"
484         .parse::<Quad>()
485         .unwrap()
486         .next_down());
487     let expected = "-0x1.ffffffffffffffffffffffff000dp-16000".parse::<Quad>().unwrap();
488     assert_eq!(status, Status::OK);
489     assert!(!test.is_denormal());
490     assert!(test.is_negative());
491     assert!(test.bitwise_eq(expected));
492 }
493
494 #[test]
495 fn fma() {
496     {
497         let mut f1 = Single::from_f32(14.5);
498         let f2 = Single::from_f32(-14.5);
499         let f3 = Single::from_f32(225.0);
500         f1 = f1.mul_add(f2, f3).value;
501         assert_eq!(14.75, f1.to_f32());
502     }
503
504     {
505         let val2 = Single::from_f32(2.0);
506         let mut f1 = Single::from_f32(1.17549435e-38);
507         let mut f2 = Single::from_f32(1.17549435e-38);
508         f1 /= val2;
509         f2 /= val2;
510         let f3 = Single::from_f32(12.0);
511         f1 = f1.mul_add(f2, f3).value;
512         assert_eq!(12.0, f1.to_f32());
513     }
514
515     // Test for correct zero sign when answer is exactly zero.
516     // fma(1.0, -1.0, 1.0) -> +ve 0.
517     {
518         let mut f1 = Double::from_f64(1.0);
519         let f2 = Double::from_f64(-1.0);
520         let f3 = Double::from_f64(1.0);
521         f1 = f1.mul_add(f2, f3).value;
522         assert!(!f1.is_negative() && f1.is_zero());
523     }
524
525     // Test for correct zero sign when answer is exactly zero and rounding towards
526     // negative.
527     // fma(1.0, -1.0, 1.0) -> +ve 0.
528     {
529         let mut f1 = Double::from_f64(1.0);
530         let f2 = Double::from_f64(-1.0);
531         let f3 = Double::from_f64(1.0);
532         f1 = f1.mul_add_r(f2, f3, Round::TowardNegative).value;
533         assert!(f1.is_negative() && f1.is_zero());
534     }
535
536     // Test for correct (in this case -ve) sign when adding like signed zeros.
537     // Test fma(0.0, -0.0, -0.0) -> -ve 0.
538     {
539         let mut f1 = Double::from_f64(0.0);
540         let f2 = Double::from_f64(-0.0);
541         let f3 = Double::from_f64(-0.0);
542         f1 = f1.mul_add(f2, f3).value;
543         assert!(f1.is_negative() && f1.is_zero());
544     }
545
546     // Test -ve sign preservation when small negative results underflow.
547     {
548         let mut f1 = "-0x1p-1074".parse::<Double>().unwrap();
549         let f2 = "+0x1p-1074".parse::<Double>().unwrap();
550         let f3 = Double::from_f64(0.0);
551         f1 = f1.mul_add(f2, f3).value;
552         assert!(f1.is_negative() && f1.is_zero());
553     }
554
555     // Test x87 extended precision case from https://llvm.org/PR20728.
556     {
557         let mut m1 = X87DoubleExtended::from_u128(1).value;
558         let m2 = X87DoubleExtended::from_u128(1).value;
559         let a = X87DoubleExtended::from_u128(3).value;
560
561         let mut loses_info = false;
562         m1 = m1.mul_add(m2, a).value;
563         let r: Single = m1.convert(&mut loses_info).value;
564         assert!(!loses_info);
565         assert_eq!(4.0, r.to_f32());
566     }
567 }
568
569 #[test]
570 fn issue_69532() {
571     let f = Double::from_bits(0x7FF0_0000_0000_0001u64 as u128);
572     let mut loses_info = false;
573     let sta = f.convert(&mut loses_info);
574     let r: Single = sta.value;
575     assert!(loses_info);
576     assert!(r.is_nan());
577     assert_eq!(sta.status, Status::INVALID_OP);
578 }
579
580 #[test]
581 fn min_num() {
582     let f1 = Double::from_f64(1.0);
583     let f2 = Double::from_f64(2.0);
584     let nan = Double::NAN;
585
586     assert_eq!(1.0, f1.min(f2).to_f64());
587     assert_eq!(1.0, f2.min(f1).to_f64());
588     assert_eq!(1.0, f1.min(nan).to_f64());
589     assert_eq!(1.0, nan.min(f1).to_f64());
590 }
591
592 #[test]
593 fn max_num() {
594     let f1 = Double::from_f64(1.0);
595     let f2 = Double::from_f64(2.0);
596     let nan = Double::NAN;
597
598     assert_eq!(2.0, f1.max(f2).to_f64());
599     assert_eq!(2.0, f2.max(f1).to_f64());
600     assert_eq!(1.0, f1.max(nan).to_f64());
601     assert_eq!(1.0, nan.max(f1).to_f64());
602 }
603
604 #[test]
605 fn denormal() {
606     // Test single precision
607     {
608         assert!(!Single::from_f32(0.0).is_denormal());
609
610         let mut t = "1.17549435082228750797e-38".parse::<Single>().unwrap();
611         assert!(!t.is_denormal());
612
613         let val2 = Single::from_f32(2.0e0);
614         t /= val2;
615         assert!(t.is_denormal());
616     }
617
618     // Test double precision
619     {
620         assert!(!Double::from_f64(0.0).is_denormal());
621
622         let mut t = "2.22507385850720138309e-308".parse::<Double>().unwrap();
623         assert!(!t.is_denormal());
624
625         let val2 = Double::from_f64(2.0e0);
626         t /= val2;
627         assert!(t.is_denormal());
628     }
629
630     // Test Intel double-ext
631     {
632         assert!(!X87DoubleExtended::from_u128(0).value.is_denormal());
633
634         let mut t = "3.36210314311209350626e-4932".parse::<X87DoubleExtended>().unwrap();
635         assert!(!t.is_denormal());
636
637         t /= X87DoubleExtended::from_u128(2).value;
638         assert!(t.is_denormal());
639     }
640
641     // Test quadruple precision
642     {
643         assert!(!Quad::from_u128(0).value.is_denormal());
644
645         let mut t = "3.36210314311209350626267781732175260e-4932".parse::<Quad>().unwrap();
646         assert!(!t.is_denormal());
647
648         t /= Quad::from_u128(2).value;
649         assert!(t.is_denormal());
650     }
651 }
652
653 #[test]
654 fn decimal_strings_without_null_terminators() {
655     // Make sure that we can parse strings without null terminators.
656     // rdar://14323230.
657     let val = "0.00"[..3].parse::<Double>().unwrap();
658     assert_eq!(val.to_f64(), 0.0);
659     let val = "0.01"[..3].parse::<Double>().unwrap();
660     assert_eq!(val.to_f64(), 0.0);
661     let val = "0.09"[..3].parse::<Double>().unwrap();
662     assert_eq!(val.to_f64(), 0.0);
663     let val = "0.095"[..4].parse::<Double>().unwrap();
664     assert_eq!(val.to_f64(), 0.09);
665     let val = "0.00e+3"[..7].parse::<Double>().unwrap();
666     assert_eq!(val.to_f64(), 0.00);
667     let val = "0e+3"[..4].parse::<Double>().unwrap();
668     assert_eq!(val.to_f64(), 0.00);
669 }
670
671 #[test]
672 fn from_zero_decimal_string() {
673     assert_eq!(0.0, "0".parse::<Double>().unwrap().to_f64());
674     assert_eq!(0.0, "+0".parse::<Double>().unwrap().to_f64());
675     assert_eq!(-0.0, "-0".parse::<Double>().unwrap().to_f64());
676
677     assert_eq!(0.0, "0.".parse::<Double>().unwrap().to_f64());
678     assert_eq!(0.0, "+0.".parse::<Double>().unwrap().to_f64());
679     assert_eq!(-0.0, "-0.".parse::<Double>().unwrap().to_f64());
680
681     assert_eq!(0.0, ".0".parse::<Double>().unwrap().to_f64());
682     assert_eq!(0.0, "+.0".parse::<Double>().unwrap().to_f64());
683     assert_eq!(-0.0, "-.0".parse::<Double>().unwrap().to_f64());
684
685     assert_eq!(0.0, "0.0".parse::<Double>().unwrap().to_f64());
686     assert_eq!(0.0, "+0.0".parse::<Double>().unwrap().to_f64());
687     assert_eq!(-0.0, "-0.0".parse::<Double>().unwrap().to_f64());
688
689     assert_eq!(0.0, "00000.".parse::<Double>().unwrap().to_f64());
690     assert_eq!(0.0, "+00000.".parse::<Double>().unwrap().to_f64());
691     assert_eq!(-0.0, "-00000.".parse::<Double>().unwrap().to_f64());
692
693     assert_eq!(0.0, ".00000".parse::<Double>().unwrap().to_f64());
694     assert_eq!(0.0, "+.00000".parse::<Double>().unwrap().to_f64());
695     assert_eq!(-0.0, "-.00000".parse::<Double>().unwrap().to_f64());
696
697     assert_eq!(0.0, "0000.00000".parse::<Double>().unwrap().to_f64());
698     assert_eq!(0.0, "+0000.00000".parse::<Double>().unwrap().to_f64());
699     assert_eq!(-0.0, "-0000.00000".parse::<Double>().unwrap().to_f64());
700 }
701
702 #[test]
703 fn from_zero_decimal_single_exponent_string() {
704     assert_eq!(0.0, "0e1".parse::<Double>().unwrap().to_f64());
705     assert_eq!(0.0, "+0e1".parse::<Double>().unwrap().to_f64());
706     assert_eq!(-0.0, "-0e1".parse::<Double>().unwrap().to_f64());
707
708     assert_eq!(0.0, "0e+1".parse::<Double>().unwrap().to_f64());
709     assert_eq!(0.0, "+0e+1".parse::<Double>().unwrap().to_f64());
710     assert_eq!(-0.0, "-0e+1".parse::<Double>().unwrap().to_f64());
711
712     assert_eq!(0.0, "0e-1".parse::<Double>().unwrap().to_f64());
713     assert_eq!(0.0, "+0e-1".parse::<Double>().unwrap().to_f64());
714     assert_eq!(-0.0, "-0e-1".parse::<Double>().unwrap().to_f64());
715
716     assert_eq!(0.0, "0.e1".parse::<Double>().unwrap().to_f64());
717     assert_eq!(0.0, "+0.e1".parse::<Double>().unwrap().to_f64());
718     assert_eq!(-0.0, "-0.e1".parse::<Double>().unwrap().to_f64());
719
720     assert_eq!(0.0, "0.e+1".parse::<Double>().unwrap().to_f64());
721     assert_eq!(0.0, "+0.e+1".parse::<Double>().unwrap().to_f64());
722     assert_eq!(-0.0, "-0.e+1".parse::<Double>().unwrap().to_f64());
723
724     assert_eq!(0.0, "0.e-1".parse::<Double>().unwrap().to_f64());
725     assert_eq!(0.0, "+0.e-1".parse::<Double>().unwrap().to_f64());
726     assert_eq!(-0.0, "-0.e-1".parse::<Double>().unwrap().to_f64());
727
728     assert_eq!(0.0, ".0e1".parse::<Double>().unwrap().to_f64());
729     assert_eq!(0.0, "+.0e1".parse::<Double>().unwrap().to_f64());
730     assert_eq!(-0.0, "-.0e1".parse::<Double>().unwrap().to_f64());
731
732     assert_eq!(0.0, ".0e+1".parse::<Double>().unwrap().to_f64());
733     assert_eq!(0.0, "+.0e+1".parse::<Double>().unwrap().to_f64());
734     assert_eq!(-0.0, "-.0e+1".parse::<Double>().unwrap().to_f64());
735
736     assert_eq!(0.0, ".0e-1".parse::<Double>().unwrap().to_f64());
737     assert_eq!(0.0, "+.0e-1".parse::<Double>().unwrap().to_f64());
738     assert_eq!(-0.0, "-.0e-1".parse::<Double>().unwrap().to_f64());
739
740     assert_eq!(0.0, "0.0e1".parse::<Double>().unwrap().to_f64());
741     assert_eq!(0.0, "+0.0e1".parse::<Double>().unwrap().to_f64());
742     assert_eq!(-0.0, "-0.0e1".parse::<Double>().unwrap().to_f64());
743
744     assert_eq!(0.0, "0.0e+1".parse::<Double>().unwrap().to_f64());
745     assert_eq!(0.0, "+0.0e+1".parse::<Double>().unwrap().to_f64());
746     assert_eq!(-0.0, "-0.0e+1".parse::<Double>().unwrap().to_f64());
747
748     assert_eq!(0.0, "0.0e-1".parse::<Double>().unwrap().to_f64());
749     assert_eq!(0.0, "+0.0e-1".parse::<Double>().unwrap().to_f64());
750     assert_eq!(-0.0, "-0.0e-1".parse::<Double>().unwrap().to_f64());
751
752     assert_eq!(0.0, "000.0000e1".parse::<Double>().unwrap().to_f64());
753     assert_eq!(0.0, "+000.0000e+1".parse::<Double>().unwrap().to_f64());
754     assert_eq!(-0.0, "-000.0000e+1".parse::<Double>().unwrap().to_f64());
755 }
756
757 #[test]
758 fn from_zero_decimal_large_exponent_string() {
759     assert_eq!(0.0, "0e1234".parse::<Double>().unwrap().to_f64());
760     assert_eq!(0.0, "+0e1234".parse::<Double>().unwrap().to_f64());
761     assert_eq!(-0.0, "-0e1234".parse::<Double>().unwrap().to_f64());
762
763     assert_eq!(0.0, "0e+1234".parse::<Double>().unwrap().to_f64());
764     assert_eq!(0.0, "+0e+1234".parse::<Double>().unwrap().to_f64());
765     assert_eq!(-0.0, "-0e+1234".parse::<Double>().unwrap().to_f64());
766
767     assert_eq!(0.0, "0e-1234".parse::<Double>().unwrap().to_f64());
768     assert_eq!(0.0, "+0e-1234".parse::<Double>().unwrap().to_f64());
769     assert_eq!(-0.0, "-0e-1234".parse::<Double>().unwrap().to_f64());
770
771     assert_eq!(0.0, "000.0000e1234".parse::<Double>().unwrap().to_f64());
772     assert_eq!(0.0, "000.0000e-1234".parse::<Double>().unwrap().to_f64());
773 }
774
775 #[test]
776 fn from_zero_hexadecimal_string() {
777     assert_eq!(0.0, "0x0p1".parse::<Double>().unwrap().to_f64());
778     assert_eq!(0.0, "+0x0p1".parse::<Double>().unwrap().to_f64());
779     assert_eq!(-0.0, "-0x0p1".parse::<Double>().unwrap().to_f64());
780
781     assert_eq!(0.0, "0x0p+1".parse::<Double>().unwrap().to_f64());
782     assert_eq!(0.0, "+0x0p+1".parse::<Double>().unwrap().to_f64());
783     assert_eq!(-0.0, "-0x0p+1".parse::<Double>().unwrap().to_f64());
784
785     assert_eq!(0.0, "0x0p-1".parse::<Double>().unwrap().to_f64());
786     assert_eq!(0.0, "+0x0p-1".parse::<Double>().unwrap().to_f64());
787     assert_eq!(-0.0, "-0x0p-1".parse::<Double>().unwrap().to_f64());
788
789     assert_eq!(0.0, "0x0.p1".parse::<Double>().unwrap().to_f64());
790     assert_eq!(0.0, "+0x0.p1".parse::<Double>().unwrap().to_f64());
791     assert_eq!(-0.0, "-0x0.p1".parse::<Double>().unwrap().to_f64());
792
793     assert_eq!(0.0, "0x0.p+1".parse::<Double>().unwrap().to_f64());
794     assert_eq!(0.0, "+0x0.p+1".parse::<Double>().unwrap().to_f64());
795     assert_eq!(-0.0, "-0x0.p+1".parse::<Double>().unwrap().to_f64());
796
797     assert_eq!(0.0, "0x0.p-1".parse::<Double>().unwrap().to_f64());
798     assert_eq!(0.0, "+0x0.p-1".parse::<Double>().unwrap().to_f64());
799     assert_eq!(-0.0, "-0x0.p-1".parse::<Double>().unwrap().to_f64());
800
801     assert_eq!(0.0, "0x.0p1".parse::<Double>().unwrap().to_f64());
802     assert_eq!(0.0, "+0x.0p1".parse::<Double>().unwrap().to_f64());
803     assert_eq!(-0.0, "-0x.0p1".parse::<Double>().unwrap().to_f64());
804
805     assert_eq!(0.0, "0x.0p+1".parse::<Double>().unwrap().to_f64());
806     assert_eq!(0.0, "+0x.0p+1".parse::<Double>().unwrap().to_f64());
807     assert_eq!(-0.0, "-0x.0p+1".parse::<Double>().unwrap().to_f64());
808
809     assert_eq!(0.0, "0x.0p-1".parse::<Double>().unwrap().to_f64());
810     assert_eq!(0.0, "+0x.0p-1".parse::<Double>().unwrap().to_f64());
811     assert_eq!(-0.0, "-0x.0p-1".parse::<Double>().unwrap().to_f64());
812
813     assert_eq!(0.0, "0x0.0p1".parse::<Double>().unwrap().to_f64());
814     assert_eq!(0.0, "+0x0.0p1".parse::<Double>().unwrap().to_f64());
815     assert_eq!(-0.0, "-0x0.0p1".parse::<Double>().unwrap().to_f64());
816
817     assert_eq!(0.0, "0x0.0p+1".parse::<Double>().unwrap().to_f64());
818     assert_eq!(0.0, "+0x0.0p+1".parse::<Double>().unwrap().to_f64());
819     assert_eq!(-0.0, "-0x0.0p+1".parse::<Double>().unwrap().to_f64());
820
821     assert_eq!(0.0, "0x0.0p-1".parse::<Double>().unwrap().to_f64());
822     assert_eq!(0.0, "+0x0.0p-1".parse::<Double>().unwrap().to_f64());
823     assert_eq!(-0.0, "-0x0.0p-1".parse::<Double>().unwrap().to_f64());
824
825     assert_eq!(0.0, "0x00000.p1".parse::<Double>().unwrap().to_f64());
826     assert_eq!(0.0, "0x0000.00000p1".parse::<Double>().unwrap().to_f64());
827     assert_eq!(0.0, "0x.00000p1".parse::<Double>().unwrap().to_f64());
828     assert_eq!(0.0, "0x0.p1".parse::<Double>().unwrap().to_f64());
829     assert_eq!(0.0, "0x0p1234".parse::<Double>().unwrap().to_f64());
830     assert_eq!(-0.0, "-0x0p1234".parse::<Double>().unwrap().to_f64());
831     assert_eq!(0.0, "0x00000.p1234".parse::<Double>().unwrap().to_f64());
832     assert_eq!(0.0, "0x0000.00000p1234".parse::<Double>().unwrap().to_f64());
833     assert_eq!(0.0, "0x.00000p1234".parse::<Double>().unwrap().to_f64());
834     assert_eq!(0.0, "0x0.p1234".parse::<Double>().unwrap().to_f64());
835 }
836
837 #[test]
838 fn from_decimal_string() {
839     assert_eq!(1.0, "1".parse::<Double>().unwrap().to_f64());
840     assert_eq!(2.0, "2.".parse::<Double>().unwrap().to_f64());
841     assert_eq!(0.5, ".5".parse::<Double>().unwrap().to_f64());
842     assert_eq!(1.0, "1.0".parse::<Double>().unwrap().to_f64());
843     assert_eq!(-2.0, "-2".parse::<Double>().unwrap().to_f64());
844     assert_eq!(-4.0, "-4.".parse::<Double>().unwrap().to_f64());
845     assert_eq!(-0.5, "-.5".parse::<Double>().unwrap().to_f64());
846     assert_eq!(-1.5, "-1.5".parse::<Double>().unwrap().to_f64());
847     assert_eq!(1.25e12, "1.25e12".parse::<Double>().unwrap().to_f64());
848     assert_eq!(1.25e+12, "1.25e+12".parse::<Double>().unwrap().to_f64());
849     assert_eq!(1.25e-12, "1.25e-12".parse::<Double>().unwrap().to_f64());
850     assert_eq!(1024.0, "1024.".parse::<Double>().unwrap().to_f64());
851     assert_eq!(1024.05, "1024.05000".parse::<Double>().unwrap().to_f64());
852     assert_eq!(0.05, ".05000".parse::<Double>().unwrap().to_f64());
853     assert_eq!(2.0, "2.".parse::<Double>().unwrap().to_f64());
854     assert_eq!(2.0e2, "2.e2".parse::<Double>().unwrap().to_f64());
855     assert_eq!(2.0e+2, "2.e+2".parse::<Double>().unwrap().to_f64());
856     assert_eq!(2.0e-2, "2.e-2".parse::<Double>().unwrap().to_f64());
857     assert_eq!(2.05e2, "002.05000e2".parse::<Double>().unwrap().to_f64());
858     assert_eq!(2.05e+2, "002.05000e+2".parse::<Double>().unwrap().to_f64());
859     assert_eq!(2.05e-2, "002.05000e-2".parse::<Double>().unwrap().to_f64());
860     assert_eq!(2.05e12, "002.05000e12".parse::<Double>().unwrap().to_f64());
861     assert_eq!(2.05e+12, "002.05000e+12".parse::<Double>().unwrap().to_f64());
862     assert_eq!(2.05e-12, "002.05000e-12".parse::<Double>().unwrap().to_f64());
863
864     // These are "carefully selected" to overflow the fast log-base
865     // calculations in the implementation.
866     assert!("99e99999".parse::<Double>().unwrap().is_infinite());
867     assert!("-99e99999".parse::<Double>().unwrap().is_infinite());
868     assert!("1e-99999".parse::<Double>().unwrap().is_pos_zero());
869     assert!("-1e-99999".parse::<Double>().unwrap().is_neg_zero());
870
871     assert_eq!(2.71828, "2.71828".parse::<Double>().unwrap().to_f64());
872 }
873
874 #[test]
875 fn from_hexadecimal_string() {
876     assert_eq!(1.0, "0x1p0".parse::<Double>().unwrap().to_f64());
877     assert_eq!(1.0, "+0x1p0".parse::<Double>().unwrap().to_f64());
878     assert_eq!(-1.0, "-0x1p0".parse::<Double>().unwrap().to_f64());
879
880     assert_eq!(1.0, "0x1p+0".parse::<Double>().unwrap().to_f64());
881     assert_eq!(1.0, "+0x1p+0".parse::<Double>().unwrap().to_f64());
882     assert_eq!(-1.0, "-0x1p+0".parse::<Double>().unwrap().to_f64());
883
884     assert_eq!(1.0, "0x1p-0".parse::<Double>().unwrap().to_f64());
885     assert_eq!(1.0, "+0x1p-0".parse::<Double>().unwrap().to_f64());
886     assert_eq!(-1.0, "-0x1p-0".parse::<Double>().unwrap().to_f64());
887
888     assert_eq!(2.0, "0x1p1".parse::<Double>().unwrap().to_f64());
889     assert_eq!(2.0, "+0x1p1".parse::<Double>().unwrap().to_f64());
890     assert_eq!(-2.0, "-0x1p1".parse::<Double>().unwrap().to_f64());
891
892     assert_eq!(2.0, "0x1p+1".parse::<Double>().unwrap().to_f64());
893     assert_eq!(2.0, "+0x1p+1".parse::<Double>().unwrap().to_f64());
894     assert_eq!(-2.0, "-0x1p+1".parse::<Double>().unwrap().to_f64());
895
896     assert_eq!(0.5, "0x1p-1".parse::<Double>().unwrap().to_f64());
897     assert_eq!(0.5, "+0x1p-1".parse::<Double>().unwrap().to_f64());
898     assert_eq!(-0.5, "-0x1p-1".parse::<Double>().unwrap().to_f64());
899
900     assert_eq!(3.0, "0x1.8p1".parse::<Double>().unwrap().to_f64());
901     assert_eq!(3.0, "+0x1.8p1".parse::<Double>().unwrap().to_f64());
902     assert_eq!(-3.0, "-0x1.8p1".parse::<Double>().unwrap().to_f64());
903
904     assert_eq!(3.0, "0x1.8p+1".parse::<Double>().unwrap().to_f64());
905     assert_eq!(3.0, "+0x1.8p+1".parse::<Double>().unwrap().to_f64());
906     assert_eq!(-3.0, "-0x1.8p+1".parse::<Double>().unwrap().to_f64());
907
908     assert_eq!(0.75, "0x1.8p-1".parse::<Double>().unwrap().to_f64());
909     assert_eq!(0.75, "+0x1.8p-1".parse::<Double>().unwrap().to_f64());
910     assert_eq!(-0.75, "-0x1.8p-1".parse::<Double>().unwrap().to_f64());
911
912     assert_eq!(8192.0, "0x1000.000p1".parse::<Double>().unwrap().to_f64());
913     assert_eq!(8192.0, "+0x1000.000p1".parse::<Double>().unwrap().to_f64());
914     assert_eq!(-8192.0, "-0x1000.000p1".parse::<Double>().unwrap().to_f64());
915
916     assert_eq!(8192.0, "0x1000.000p+1".parse::<Double>().unwrap().to_f64());
917     assert_eq!(8192.0, "+0x1000.000p+1".parse::<Double>().unwrap().to_f64());
918     assert_eq!(-8192.0, "-0x1000.000p+1".parse::<Double>().unwrap().to_f64());
919
920     assert_eq!(2048.0, "0x1000.000p-1".parse::<Double>().unwrap().to_f64());
921     assert_eq!(2048.0, "+0x1000.000p-1".parse::<Double>().unwrap().to_f64());
922     assert_eq!(-2048.0, "-0x1000.000p-1".parse::<Double>().unwrap().to_f64());
923
924     assert_eq!(8192.0, "0x1000p1".parse::<Double>().unwrap().to_f64());
925     assert_eq!(8192.0, "+0x1000p1".parse::<Double>().unwrap().to_f64());
926     assert_eq!(-8192.0, "-0x1000p1".parse::<Double>().unwrap().to_f64());
927
928     assert_eq!(8192.0, "0x1000p+1".parse::<Double>().unwrap().to_f64());
929     assert_eq!(8192.0, "+0x1000p+1".parse::<Double>().unwrap().to_f64());
930     assert_eq!(-8192.0, "-0x1000p+1".parse::<Double>().unwrap().to_f64());
931
932     assert_eq!(2048.0, "0x1000p-1".parse::<Double>().unwrap().to_f64());
933     assert_eq!(2048.0, "+0x1000p-1".parse::<Double>().unwrap().to_f64());
934     assert_eq!(-2048.0, "-0x1000p-1".parse::<Double>().unwrap().to_f64());
935
936     assert_eq!(16384.0, "0x10p10".parse::<Double>().unwrap().to_f64());
937     assert_eq!(16384.0, "+0x10p10".parse::<Double>().unwrap().to_f64());
938     assert_eq!(-16384.0, "-0x10p10".parse::<Double>().unwrap().to_f64());
939
940     assert_eq!(16384.0, "0x10p+10".parse::<Double>().unwrap().to_f64());
941     assert_eq!(16384.0, "+0x10p+10".parse::<Double>().unwrap().to_f64());
942     assert_eq!(-16384.0, "-0x10p+10".parse::<Double>().unwrap().to_f64());
943
944     assert_eq!(0.015625, "0x10p-10".parse::<Double>().unwrap().to_f64());
945     assert_eq!(0.015625, "+0x10p-10".parse::<Double>().unwrap().to_f64());
946     assert_eq!(-0.015625, "-0x10p-10".parse::<Double>().unwrap().to_f64());
947
948     assert_eq!(1.0625, "0x1.1p0".parse::<Double>().unwrap().to_f64());
949     assert_eq!(1.0, "0x1p0".parse::<Double>().unwrap().to_f64());
950
951     assert_eq!(
952         "0x1p-150".parse::<Double>().unwrap().to_f64(),
953         "+0x800000000000000001.p-221".parse::<Double>().unwrap().to_f64()
954     );
955     assert_eq!(
956         2251799813685248.5,
957         "0x80000000000004000000.010p-28".parse::<Double>().unwrap().to_f64()
958     );
959 }
960
961 #[test]
962 fn to_string() {
963     let to_string = |d: f64, precision: usize, width: usize| {
964         let x = Double::from_f64(d);
965         if precision == 0 {
966             format!("{:1$}", x, width)
967         } else {
968             format!("{:2$.1$}", x, precision, width)
969         }
970     };
971     assert_eq!("10", to_string(10.0, 6, 3));
972     assert_eq!("1.0E+1", to_string(10.0, 6, 0));
973     assert_eq!("10100", to_string(1.01E+4, 5, 2));
974     assert_eq!("1.01E+4", to_string(1.01E+4, 4, 2));
975     assert_eq!("1.01E+4", to_string(1.01E+4, 5, 1));
976     assert_eq!("0.0101", to_string(1.01E-2, 5, 2));
977     assert_eq!("0.0101", to_string(1.01E-2, 4, 2));
978     assert_eq!("1.01E-2", to_string(1.01E-2, 5, 1));
979     assert_eq!("0.78539816339744828", to_string(0.78539816339744830961, 0, 3));
980     assert_eq!("4.9406564584124654E-324", to_string(4.9406564584124654e-324, 0, 3));
981     assert_eq!("873.18340000000001", to_string(873.1834, 0, 1));
982     assert_eq!("8.7318340000000001E+2", to_string(873.1834, 0, 0));
983     assert_eq!("1.7976931348623157E+308", to_string(1.7976931348623157E+308, 0, 0));
984
985     let to_string = |d: f64, precision: usize, width: usize| {
986         let x = Double::from_f64(d);
987         if precision == 0 {
988             format!("{:#1$}", x, width)
989         } else {
990             format!("{:#2$.1$}", x, precision, width)
991         }
992     };
993     assert_eq!("10", to_string(10.0, 6, 3));
994     assert_eq!("1.000000e+01", to_string(10.0, 6, 0));
995     assert_eq!("10100", to_string(1.01E+4, 5, 2));
996     assert_eq!("1.0100e+04", to_string(1.01E+4, 4, 2));
997     assert_eq!("1.01000e+04", to_string(1.01E+4, 5, 1));
998     assert_eq!("0.0101", to_string(1.01E-2, 5, 2));
999     assert_eq!("0.0101", to_string(1.01E-2, 4, 2));
1000     assert_eq!("1.01000e-02", to_string(1.01E-2, 5, 1));
1001     assert_eq!("0.78539816339744828", to_string(0.78539816339744830961, 0, 3));
1002     assert_eq!("4.94065645841246540e-324", to_string(4.9406564584124654e-324, 0, 3));
1003     assert_eq!("873.18340000000001", to_string(873.1834, 0, 1));
1004     assert_eq!("8.73183400000000010e+02", to_string(873.1834, 0, 0));
1005     assert_eq!("1.79769313486231570e+308", to_string(1.7976931348623157E+308, 0, 0));
1006 }
1007
1008 #[test]
1009 fn to_integer() {
1010     let mut is_exact = false;
1011
1012     assert_eq!(
1013         Status::OK.and(10),
1014         "10".parse::<Double>().unwrap().to_u128_r(5, Round::TowardZero, &mut is_exact,)
1015     );
1016     assert!(is_exact);
1017
1018     assert_eq!(
1019         Status::INVALID_OP.and(0),
1020         "-10".parse::<Double>().unwrap().to_u128_r(5, Round::TowardZero, &mut is_exact,)
1021     );
1022     assert!(!is_exact);
1023
1024     assert_eq!(
1025         Status::INVALID_OP.and(31),
1026         "32".parse::<Double>().unwrap().to_u128_r(5, Round::TowardZero, &mut is_exact,)
1027     );
1028     assert!(!is_exact);
1029
1030     assert_eq!(
1031         Status::INEXACT.and(7),
1032         "7.9".parse::<Double>().unwrap().to_u128_r(5, Round::TowardZero, &mut is_exact,)
1033     );
1034     assert!(!is_exact);
1035
1036     assert_eq!(
1037         Status::OK.and(-10),
1038         "-10".parse::<Double>().unwrap().to_i128_r(5, Round::TowardZero, &mut is_exact,)
1039     );
1040     assert!(is_exact);
1041
1042     assert_eq!(
1043         Status::INVALID_OP.and(-16),
1044         "-17".parse::<Double>().unwrap().to_i128_r(5, Round::TowardZero, &mut is_exact,)
1045     );
1046     assert!(!is_exact);
1047
1048     assert_eq!(
1049         Status::INVALID_OP.and(15),
1050         "16".parse::<Double>().unwrap().to_i128_r(5, Round::TowardZero, &mut is_exact,)
1051     );
1052     assert!(!is_exact);
1053 }
1054
1055 #[test]
1056 fn nan() {
1057     fn nanbits<T: Float>(signaling: bool, negative: bool, fill: u128) -> u128 {
1058         let x = if signaling { T::snan(Some(fill)) } else { T::qnan(Some(fill)) };
1059         if negative { (-x).to_bits() } else { x.to_bits() }
1060     }
1061
1062     assert_eq!(0x7fc00000, nanbits::<Single>(false, false, 0));
1063     assert_eq!(0xffc00000, nanbits::<Single>(false, true, 0));
1064     assert_eq!(0x7fc0ae72, nanbits::<Single>(false, false, 0xae72));
1065     assert_eq!(0x7fffae72, nanbits::<Single>(false, false, 0xffffae72));
1066     assert_eq!(0x7fa00000, nanbits::<Single>(true, false, 0));
1067     assert_eq!(0xffa00000, nanbits::<Single>(true, true, 0));
1068     assert_eq!(0x7f80ae72, nanbits::<Single>(true, false, 0xae72));
1069     assert_eq!(0x7fbfae72, nanbits::<Single>(true, false, 0xffffae72));
1070
1071     assert_eq!(0x7ff8000000000000, nanbits::<Double>(false, false, 0));
1072     assert_eq!(0xfff8000000000000, nanbits::<Double>(false, true, 0));
1073     assert_eq!(0x7ff800000000ae72, nanbits::<Double>(false, false, 0xae72));
1074     assert_eq!(0x7fffffffffffae72, nanbits::<Double>(false, false, 0xffffffffffffae72));
1075     assert_eq!(0x7ff4000000000000, nanbits::<Double>(true, false, 0));
1076     assert_eq!(0xfff4000000000000, nanbits::<Double>(true, true, 0));
1077     assert_eq!(0x7ff000000000ae72, nanbits::<Double>(true, false, 0xae72));
1078     assert_eq!(0x7ff7ffffffffae72, nanbits::<Double>(true, false, 0xffffffffffffae72));
1079 }
1080
1081 #[test]
1082 fn string_decimal_death() {
1083     assert_eq!("".parse::<Double>(), Err(ParseError("Invalid string length")));
1084     assert_eq!("+".parse::<Double>(), Err(ParseError("String has no digits")));
1085     assert_eq!("-".parse::<Double>(), Err(ParseError("String has no digits")));
1086
1087     assert_eq!("\0".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1088     assert_eq!("1\0".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1089     assert_eq!("1\02".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1090     assert_eq!("1\02e1".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1091     assert_eq!("1e\0".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1092     assert_eq!("1e1\0".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1093     assert_eq!("1e1\02".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1094
1095     assert_eq!("1.0f".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1096
1097     assert_eq!("..".parse::<Double>(), Err(ParseError("String contains multiple dots")));
1098     assert_eq!("..0".parse::<Double>(), Err(ParseError("String contains multiple dots")));
1099     assert_eq!("1.0.0".parse::<Double>(), Err(ParseError("String contains multiple dots")));
1100 }
1101
1102 #[test]
1103 fn string_decimal_significand_death() {
1104     assert_eq!(".".parse::<Double>(), Err(ParseError("Significand has no digits")));
1105     assert_eq!("+.".parse::<Double>(), Err(ParseError("Significand has no digits")));
1106     assert_eq!("-.".parse::<Double>(), Err(ParseError("Significand has no digits")));
1107
1108     assert_eq!("e".parse::<Double>(), Err(ParseError("Significand has no digits")));
1109     assert_eq!("+e".parse::<Double>(), Err(ParseError("Significand has no digits")));
1110     assert_eq!("-e".parse::<Double>(), Err(ParseError("Significand has no digits")));
1111
1112     assert_eq!("e1".parse::<Double>(), Err(ParseError("Significand has no digits")));
1113     assert_eq!("+e1".parse::<Double>(), Err(ParseError("Significand has no digits")));
1114     assert_eq!("-e1".parse::<Double>(), Err(ParseError("Significand has no digits")));
1115
1116     assert_eq!(".e1".parse::<Double>(), Err(ParseError("Significand has no digits")));
1117     assert_eq!("+.e1".parse::<Double>(), Err(ParseError("Significand has no digits")));
1118     assert_eq!("-.e1".parse::<Double>(), Err(ParseError("Significand has no digits")));
1119
1120     assert_eq!(".e".parse::<Double>(), Err(ParseError("Significand has no digits")));
1121     assert_eq!("+.e".parse::<Double>(), Err(ParseError("Significand has no digits")));
1122     assert_eq!("-.e".parse::<Double>(), Err(ParseError("Significand has no digits")));
1123 }
1124
1125 #[test]
1126 fn string_decimal_exponent_death() {
1127     assert_eq!("1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1128     assert_eq!("+1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1129     assert_eq!("-1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1130
1131     assert_eq!("1.e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1132     assert_eq!("+1.e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1133     assert_eq!("-1.e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1134
1135     assert_eq!(".1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1136     assert_eq!("+.1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1137     assert_eq!("-.1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1138
1139     assert_eq!("1.1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1140     assert_eq!("+1.1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1141     assert_eq!("-1.1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1142
1143     assert_eq!("1e+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1144     assert_eq!("1e-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1145
1146     assert_eq!(".1e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1147     assert_eq!(".1e+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1148     assert_eq!(".1e-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1149
1150     assert_eq!("1.0e".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1151     assert_eq!("1.0e+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1152     assert_eq!("1.0e-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1153 }
1154
1155 #[test]
1156 fn string_hexadecimal_death() {
1157     assert_eq!("0x".parse::<Double>(), Err(ParseError("Invalid string")));
1158     assert_eq!("+0x".parse::<Double>(), Err(ParseError("Invalid string")));
1159     assert_eq!("-0x".parse::<Double>(), Err(ParseError("Invalid string")));
1160
1161     assert_eq!("0x0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1162     assert_eq!("+0x0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1163     assert_eq!("-0x0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1164
1165     assert_eq!("0x0.".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1166     assert_eq!("+0x0.".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1167     assert_eq!("-0x0.".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1168
1169     assert_eq!("0x.0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1170     assert_eq!("+0x.0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1171     assert_eq!("-0x.0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1172
1173     assert_eq!("0x0.0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1174     assert_eq!("+0x0.0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1175     assert_eq!("-0x0.0".parse::<Double>(), Err(ParseError("Hex strings require an exponent")));
1176
1177     assert_eq!("0x\0".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1178     assert_eq!("0x1\0".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1179     assert_eq!("0x1\02".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1180     assert_eq!("0x1\02p1".parse::<Double>(), Err(ParseError("Invalid character in significand")));
1181     assert_eq!("0x1p\0".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1182     assert_eq!("0x1p1\0".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1183     assert_eq!("0x1p1\02".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1184
1185     assert_eq!("0x1p0f".parse::<Double>(), Err(ParseError("Invalid character in exponent")));
1186
1187     assert_eq!("0x..p1".parse::<Double>(), Err(ParseError("String contains multiple dots")));
1188     assert_eq!("0x..0p1".parse::<Double>(), Err(ParseError("String contains multiple dots")));
1189     assert_eq!("0x1.0.0p1".parse::<Double>(), Err(ParseError("String contains multiple dots")));
1190 }
1191
1192 #[test]
1193 fn string_hexadecimal_significand_death() {
1194     assert_eq!("0x.".parse::<Double>(), Err(ParseError("Significand has no digits")));
1195     assert_eq!("+0x.".parse::<Double>(), Err(ParseError("Significand has no digits")));
1196     assert_eq!("-0x.".parse::<Double>(), Err(ParseError("Significand has no digits")));
1197
1198     assert_eq!("0xp".parse::<Double>(), Err(ParseError("Significand has no digits")));
1199     assert_eq!("+0xp".parse::<Double>(), Err(ParseError("Significand has no digits")));
1200     assert_eq!("-0xp".parse::<Double>(), Err(ParseError("Significand has no digits")));
1201
1202     assert_eq!("0xp+".parse::<Double>(), Err(ParseError("Significand has no digits")));
1203     assert_eq!("+0xp+".parse::<Double>(), Err(ParseError("Significand has no digits")));
1204     assert_eq!("-0xp+".parse::<Double>(), Err(ParseError("Significand has no digits")));
1205
1206     assert_eq!("0xp-".parse::<Double>(), Err(ParseError("Significand has no digits")));
1207     assert_eq!("+0xp-".parse::<Double>(), Err(ParseError("Significand has no digits")));
1208     assert_eq!("-0xp-".parse::<Double>(), Err(ParseError("Significand has no digits")));
1209
1210     assert_eq!("0x.p".parse::<Double>(), Err(ParseError("Significand has no digits")));
1211     assert_eq!("+0x.p".parse::<Double>(), Err(ParseError("Significand has no digits")));
1212     assert_eq!("-0x.p".parse::<Double>(), Err(ParseError("Significand has no digits")));
1213
1214     assert_eq!("0x.p+".parse::<Double>(), Err(ParseError("Significand has no digits")));
1215     assert_eq!("+0x.p+".parse::<Double>(), Err(ParseError("Significand has no digits")));
1216     assert_eq!("-0x.p+".parse::<Double>(), Err(ParseError("Significand has no digits")));
1217
1218     assert_eq!("0x.p-".parse::<Double>(), Err(ParseError("Significand has no digits")));
1219     assert_eq!("+0x.p-".parse::<Double>(), Err(ParseError("Significand has no digits")));
1220     assert_eq!("-0x.p-".parse::<Double>(), Err(ParseError("Significand has no digits")));
1221 }
1222
1223 #[test]
1224 fn string_hexadecimal_exponent_death() {
1225     assert_eq!("0x1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1226     assert_eq!("+0x1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1227     assert_eq!("-0x1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1228
1229     assert_eq!("0x1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1230     assert_eq!("+0x1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1231     assert_eq!("-0x1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1232
1233     assert_eq!("0x1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1234     assert_eq!("+0x1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1235     assert_eq!("-0x1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1236
1237     assert_eq!("0x1.p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1238     assert_eq!("+0x1.p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1239     assert_eq!("-0x1.p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1240
1241     assert_eq!("0x1.p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1242     assert_eq!("+0x1.p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1243     assert_eq!("-0x1.p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1244
1245     assert_eq!("0x1.p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1246     assert_eq!("+0x1.p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1247     assert_eq!("-0x1.p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1248
1249     assert_eq!("0x.1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1250     assert_eq!("+0x.1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1251     assert_eq!("-0x.1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1252
1253     assert_eq!("0x.1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1254     assert_eq!("+0x.1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1255     assert_eq!("-0x.1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1256
1257     assert_eq!("0x.1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1258     assert_eq!("+0x.1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1259     assert_eq!("-0x.1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1260
1261     assert_eq!("0x1.1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1262     assert_eq!("+0x1.1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1263     assert_eq!("-0x1.1p".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1264
1265     assert_eq!("0x1.1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1266     assert_eq!("+0x1.1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1267     assert_eq!("-0x1.1p+".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1268
1269     assert_eq!("0x1.1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1270     assert_eq!("+0x1.1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1271     assert_eq!("-0x1.1p-".parse::<Double>(), Err(ParseError("Exponent has no digits")));
1272 }
1273
1274 #[test]
1275 fn exact_inverse() {
1276     // Trivial operation.
1277     assert!(Double::from_f64(2.0).get_exact_inverse().unwrap().bitwise_eq(Double::from_f64(0.5)));
1278     assert!(Single::from_f32(2.0).get_exact_inverse().unwrap().bitwise_eq(Single::from_f32(0.5)));
1279     assert!(
1280         "2.0"
1281             .parse::<Quad>()
1282             .unwrap()
1283             .get_exact_inverse()
1284             .unwrap()
1285             .bitwise_eq("0.5".parse::<Quad>().unwrap())
1286     );
1287     assert!(
1288         "2.0"
1289             .parse::<X87DoubleExtended>()
1290             .unwrap()
1291             .get_exact_inverse()
1292             .unwrap()
1293             .bitwise_eq("0.5".parse::<X87DoubleExtended>().unwrap())
1294     );
1295
1296     // FLT_MIN
1297     assert!(
1298         Single::from_f32(1.17549435e-38)
1299             .get_exact_inverse()
1300             .unwrap()
1301             .bitwise_eq(Single::from_f32(8.5070592e+37))
1302     );
1303
1304     // Large float, inverse is a denormal.
1305     assert!(Single::from_f32(1.7014118e38).get_exact_inverse().is_none());
1306     // Zero
1307     assert!(Double::from_f64(0.0).get_exact_inverse().is_none());
1308     // Denormalized float
1309     assert!(Single::from_f32(1.40129846e-45).get_exact_inverse().is_none());
1310 }
1311
1312 #[test]
1313 fn round_to_integral() {
1314     let t = Double::from_f64(-0.5);
1315     assert_eq!(-0.0, t.round_to_integral(Round::TowardZero).value.to_f64());
1316     assert_eq!(-1.0, t.round_to_integral(Round::TowardNegative).value.to_f64());
1317     assert_eq!(-0.0, t.round_to_integral(Round::TowardPositive).value.to_f64());
1318     assert_eq!(-0.0, t.round_to_integral(Round::NearestTiesToEven).value.to_f64());
1319
1320     let s = Double::from_f64(3.14);
1321     assert_eq!(3.0, s.round_to_integral(Round::TowardZero).value.to_f64());
1322     assert_eq!(3.0, s.round_to_integral(Round::TowardNegative).value.to_f64());
1323     assert_eq!(4.0, s.round_to_integral(Round::TowardPositive).value.to_f64());
1324     assert_eq!(3.0, s.round_to_integral(Round::NearestTiesToEven).value.to_f64());
1325
1326     let r = Double::largest();
1327     assert_eq!(r.to_f64(), r.round_to_integral(Round::TowardZero).value.to_f64());
1328     assert_eq!(r.to_f64(), r.round_to_integral(Round::TowardNegative).value.to_f64());
1329     assert_eq!(r.to_f64(), r.round_to_integral(Round::TowardPositive).value.to_f64());
1330     assert_eq!(r.to_f64(), r.round_to_integral(Round::NearestTiesToEven).value.to_f64());
1331
1332     let p = Double::ZERO.round_to_integral(Round::TowardZero).value;
1333     assert_eq!(0.0, p.to_f64());
1334     let p = (-Double::ZERO).round_to_integral(Round::TowardZero).value;
1335     assert_eq!(-0.0, p.to_f64());
1336     let p = Double::NAN.round_to_integral(Round::TowardZero).value;
1337     assert!(p.to_f64().is_nan());
1338     let p = Double::INFINITY.round_to_integral(Round::TowardZero).value;
1339     assert!(p.to_f64().is_infinite() && p.to_f64() > 0.0);
1340     let p = (-Double::INFINITY).round_to_integral(Round::TowardZero).value;
1341     assert!(p.to_f64().is_infinite() && p.to_f64() < 0.0);
1342 }
1343
1344 #[test]
1345 fn is_integer() {
1346     let t = Double::from_f64(-0.0);
1347     assert!(t.is_integer());
1348     let t = Double::from_f64(3.14159);
1349     assert!(!t.is_integer());
1350     let t = Double::NAN;
1351     assert!(!t.is_integer());
1352     let t = Double::INFINITY;
1353     assert!(!t.is_integer());
1354     let t = -Double::INFINITY;
1355     assert!(!t.is_integer());
1356     let t = Double::largest();
1357     assert!(t.is_integer());
1358 }
1359
1360 #[test]
1361 fn largest() {
1362     assert_eq!(3.402823466e+38, Single::largest().to_f32());
1363     assert_eq!(1.7976931348623158e+308, Double::largest().to_f64());
1364 }
1365
1366 #[test]
1367 fn smallest() {
1368     let test = Single::SMALLEST;
1369     let expected = "0x0.000002p-126".parse::<Single>().unwrap();
1370     assert!(!test.is_negative());
1371     assert!(test.is_finite_non_zero());
1372     assert!(test.is_denormal());
1373     assert!(test.bitwise_eq(expected));
1374
1375     let test = -Single::SMALLEST;
1376     let expected = "-0x0.000002p-126".parse::<Single>().unwrap();
1377     assert!(test.is_negative());
1378     assert!(test.is_finite_non_zero());
1379     assert!(test.is_denormal());
1380     assert!(test.bitwise_eq(expected));
1381
1382     let test = Quad::SMALLEST;
1383     let expected = "0x0.0000000000000000000000000001p-16382".parse::<Quad>().unwrap();
1384     assert!(!test.is_negative());
1385     assert!(test.is_finite_non_zero());
1386     assert!(test.is_denormal());
1387     assert!(test.bitwise_eq(expected));
1388
1389     let test = -Quad::SMALLEST;
1390     let expected = "-0x0.0000000000000000000000000001p-16382".parse::<Quad>().unwrap();
1391     assert!(test.is_negative());
1392     assert!(test.is_finite_non_zero());
1393     assert!(test.is_denormal());
1394     assert!(test.bitwise_eq(expected));
1395 }
1396
1397 #[test]
1398 fn smallest_normalized() {
1399     let test = Single::smallest_normalized();
1400     let expected = "0x1p-126".parse::<Single>().unwrap();
1401     assert!(!test.is_negative());
1402     assert!(test.is_finite_non_zero());
1403     assert!(!test.is_denormal());
1404     assert!(test.bitwise_eq(expected));
1405
1406     let test = -Single::smallest_normalized();
1407     let expected = "-0x1p-126".parse::<Single>().unwrap();
1408     assert!(test.is_negative());
1409     assert!(test.is_finite_non_zero());
1410     assert!(!test.is_denormal());
1411     assert!(test.bitwise_eq(expected));
1412
1413     let test = Quad::smallest_normalized();
1414     let expected = "0x1p-16382".parse::<Quad>().unwrap();
1415     assert!(!test.is_negative());
1416     assert!(test.is_finite_non_zero());
1417     assert!(!test.is_denormal());
1418     assert!(test.bitwise_eq(expected));
1419
1420     let test = -Quad::smallest_normalized();
1421     let expected = "-0x1p-16382".parse::<Quad>().unwrap();
1422     assert!(test.is_negative());
1423     assert!(test.is_finite_non_zero());
1424     assert!(!test.is_denormal());
1425     assert!(test.bitwise_eq(expected));
1426 }
1427
1428 #[test]
1429 fn zero() {
1430     assert_eq!(0.0, Single::from_f32(0.0).to_f32());
1431     assert_eq!(-0.0, Single::from_f32(-0.0).to_f32());
1432     assert!(Single::from_f32(-0.0).is_negative());
1433
1434     assert_eq!(0.0, Double::from_f64(0.0).to_f64());
1435     assert_eq!(-0.0, Double::from_f64(-0.0).to_f64());
1436     assert!(Double::from_f64(-0.0).is_negative());
1437
1438     fn test<T: Float>(sign: bool, bits: u128) {
1439         let test = if sign { -T::ZERO } else { T::ZERO };
1440         let pattern = if sign { "-0x0p+0" } else { "0x0p+0" };
1441         let expected = pattern.parse::<T>().unwrap();
1442         assert!(test.is_zero());
1443         assert_eq!(sign, test.is_negative());
1444         assert!(test.bitwise_eq(expected));
1445         assert_eq!(bits, test.to_bits());
1446     }
1447     test::<Half>(false, 0);
1448     test::<Half>(true, 0x8000);
1449     test::<Single>(false, 0);
1450     test::<Single>(true, 0x80000000);
1451     test::<Double>(false, 0);
1452     test::<Double>(true, 0x8000000000000000);
1453     test::<Quad>(false, 0);
1454     test::<Quad>(true, 0x8000000000000000_0000000000000000);
1455     test::<X87DoubleExtended>(false, 0);
1456     test::<X87DoubleExtended>(true, 0x8000_0000000000000000);
1457 }
1458
1459 #[test]
1460 fn copy_sign() {
1461     assert!(
1462         Double::from_f64(-42.0)
1463             .bitwise_eq(Double::from_f64(42.0).copy_sign(Double::from_f64(-1.0),),)
1464     );
1465     assert!(
1466         Double::from_f64(42.0)
1467             .bitwise_eq(Double::from_f64(-42.0).copy_sign(Double::from_f64(1.0),),)
1468     );
1469     assert!(
1470         Double::from_f64(-42.0)
1471             .bitwise_eq(Double::from_f64(-42.0).copy_sign(Double::from_f64(-1.0),),)
1472     );
1473     assert!(
1474         Double::from_f64(42.0)
1475             .bitwise_eq(Double::from_f64(42.0).copy_sign(Double::from_f64(1.0),),)
1476     );
1477 }
1478
1479 #[test]
1480 fn convert() {
1481     let mut loses_info = false;
1482     let test = "1.0".parse::<Double>().unwrap();
1483     let test: Single = test.convert(&mut loses_info).value;
1484     assert_eq!(1.0, test.to_f32());
1485     assert!(!loses_info);
1486
1487     let mut test = "0x1p-53".parse::<X87DoubleExtended>().unwrap();
1488     let one = "1.0".parse::<X87DoubleExtended>().unwrap();
1489     test += one;
1490     let test: Double = test.convert(&mut loses_info).value;
1491     assert_eq!(1.0, test.to_f64());
1492     assert!(loses_info);
1493
1494     let mut test = "0x1p-53".parse::<Quad>().unwrap();
1495     let one = "1.0".parse::<Quad>().unwrap();
1496     test += one;
1497     let test: Double = test.convert(&mut loses_info).value;
1498     assert_eq!(1.0, test.to_f64());
1499     assert!(loses_info);
1500
1501     let test = "0xf.fffffffp+28".parse::<X87DoubleExtended>().unwrap();
1502     let test: Double = test.convert(&mut loses_info).value;
1503     assert_eq!(4294967295.0, test.to_f64());
1504     assert!(!loses_info);
1505
1506     let test = Single::qnan(None);
1507     let x87_qnan = X87DoubleExtended::qnan(None);
1508     let test: X87DoubleExtended = test.convert(&mut loses_info).value;
1509     assert!(test.bitwise_eq(x87_qnan));
1510     assert!(!loses_info);
1511
1512     let test = Single::snan(None);
1513     let sta = test.convert(&mut loses_info);
1514     let test: X87DoubleExtended = sta.value;
1515     assert!(test.is_nan());
1516     assert!(!test.is_signaling());
1517     assert!(!loses_info);
1518     assert_eq!(sta.status, Status::INVALID_OP);
1519
1520     let test = X87DoubleExtended::qnan(None);
1521     let test: X87DoubleExtended = test.convert(&mut loses_info).value;
1522     assert!(test.bitwise_eq(x87_qnan));
1523     assert!(!loses_info);
1524
1525     let test = X87DoubleExtended::snan(None);
1526     let sta = test.convert(&mut loses_info);
1527     let test: X87DoubleExtended = sta.value;
1528     assert!(test.is_nan());
1529     assert!(!test.is_signaling());
1530     assert!(!loses_info);
1531     assert_eq!(sta.status, Status::INVALID_OP);
1532 }
1533
1534 #[test]
1535 fn is_negative() {
1536     let t = "0x1p+0".parse::<Single>().unwrap();
1537     assert!(!t.is_negative());
1538     let t = "-0x1p+0".parse::<Single>().unwrap();
1539     assert!(t.is_negative());
1540
1541     assert!(!Single::INFINITY.is_negative());
1542     assert!((-Single::INFINITY).is_negative());
1543
1544     assert!(!Single::ZERO.is_negative());
1545     assert!((-Single::ZERO).is_negative());
1546
1547     assert!(!Single::NAN.is_negative());
1548     assert!((-Single::NAN).is_negative());
1549
1550     assert!(!Single::snan(None).is_negative());
1551     assert!((-Single::snan(None)).is_negative());
1552 }
1553
1554 #[test]
1555 fn is_normal() {
1556     let t = "0x1p+0".parse::<Single>().unwrap();
1557     assert!(t.is_normal());
1558
1559     assert!(!Single::INFINITY.is_normal());
1560     assert!(!Single::ZERO.is_normal());
1561     assert!(!Single::NAN.is_normal());
1562     assert!(!Single::snan(None).is_normal());
1563     assert!(!"0x1p-149".parse::<Single>().unwrap().is_normal());
1564 }
1565
1566 #[test]
1567 fn is_finite() {
1568     let t = "0x1p+0".parse::<Single>().unwrap();
1569     assert!(t.is_finite());
1570     assert!(!Single::INFINITY.is_finite());
1571     assert!(Single::ZERO.is_finite());
1572     assert!(!Single::NAN.is_finite());
1573     assert!(!Single::snan(None).is_finite());
1574     assert!("0x1p-149".parse::<Single>().unwrap().is_finite());
1575 }
1576
1577 #[test]
1578 fn is_infinite() {
1579     let t = "0x1p+0".parse::<Single>().unwrap();
1580     assert!(!t.is_infinite());
1581     assert!(Single::INFINITY.is_infinite());
1582     assert!(!Single::ZERO.is_infinite());
1583     assert!(!Single::NAN.is_infinite());
1584     assert!(!Single::snan(None).is_infinite());
1585     assert!(!"0x1p-149".parse::<Single>().unwrap().is_infinite());
1586 }
1587
1588 #[test]
1589 fn is_nan() {
1590     let t = "0x1p+0".parse::<Single>().unwrap();
1591     assert!(!t.is_nan());
1592     assert!(!Single::INFINITY.is_nan());
1593     assert!(!Single::ZERO.is_nan());
1594     assert!(Single::NAN.is_nan());
1595     assert!(Single::snan(None).is_nan());
1596     assert!(!"0x1p-149".parse::<Single>().unwrap().is_nan());
1597 }
1598
1599 #[test]
1600 fn is_finite_non_zero() {
1601     // Test positive/negative normal value.
1602     assert!("0x1p+0".parse::<Single>().unwrap().is_finite_non_zero());
1603     assert!("-0x1p+0".parse::<Single>().unwrap().is_finite_non_zero());
1604
1605     // Test positive/negative denormal value.
1606     assert!("0x1p-149".parse::<Single>().unwrap().is_finite_non_zero());
1607     assert!("-0x1p-149".parse::<Single>().unwrap().is_finite_non_zero());
1608
1609     // Test +/- Infinity.
1610     assert!(!Single::INFINITY.is_finite_non_zero());
1611     assert!(!(-Single::INFINITY).is_finite_non_zero());
1612
1613     // Test +/- Zero.
1614     assert!(!Single::ZERO.is_finite_non_zero());
1615     assert!(!(-Single::ZERO).is_finite_non_zero());
1616
1617     // Test +/- qNaN. +/- don't mean anything with qNaN but paranoia can't hurt in
1618     // this instance.
1619     assert!(!Single::NAN.is_finite_non_zero());
1620     assert!(!(-Single::NAN).is_finite_non_zero());
1621
1622     // Test +/- sNaN. +/- don't mean anything with sNaN but paranoia can't hurt in
1623     // this instance.
1624     assert!(!Single::snan(None).is_finite_non_zero());
1625     assert!(!(-Single::snan(None)).is_finite_non_zero());
1626 }
1627
1628 #[test]
1629 fn add() {
1630     // Test Special Cases against each other and normal values.
1631
1632     // FIXMES/NOTES:
1633     // 1. Since we perform only default exception handling all operations with
1634     // signaling NaNs should have a result that is a quiet NaN. Currently they
1635     // return sNaN.
1636
1637     let p_inf = Single::INFINITY;
1638     let m_inf = -Single::INFINITY;
1639     let p_zero = Single::ZERO;
1640     let m_zero = -Single::ZERO;
1641     let qnan = Single::NAN;
1642     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
1643     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
1644     let p_largest_value = Single::largest();
1645     let m_largest_value = -Single::largest();
1646     let p_smallest_value = Single::SMALLEST;
1647     let m_smallest_value = -Single::SMALLEST;
1648     let p_smallest_normalized = Single::smallest_normalized();
1649     let m_smallest_normalized = -Single::smallest_normalized();
1650
1651     let overflow_status = Status::OVERFLOW | Status::INEXACT;
1652
1653     let special_cases = [
1654         (p_inf, p_inf, "inf", Status::OK, Category::Infinity),
1655         (p_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
1656         (p_inf, p_zero, "inf", Status::OK, Category::Infinity),
1657         (p_inf, m_zero, "inf", Status::OK, Category::Infinity),
1658         (p_inf, qnan, "nan", Status::OK, Category::NaN),
1659         /*
1660         // See Note 1.
1661         (p_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
1662                 */
1663         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
1664         (p_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
1665         (p_inf, p_largest_value, "inf", Status::OK, Category::Infinity),
1666         (p_inf, m_largest_value, "inf", Status::OK, Category::Infinity),
1667         (p_inf, p_smallest_value, "inf", Status::OK, Category::Infinity),
1668         (p_inf, m_smallest_value, "inf", Status::OK, Category::Infinity),
1669         (p_inf, p_smallest_normalized, "inf", Status::OK, Category::Infinity),
1670         (p_inf, m_smallest_normalized, "inf", Status::OK, Category::Infinity),
1671         (m_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
1672         (m_inf, m_inf, "-inf", Status::OK, Category::Infinity),
1673         (m_inf, p_zero, "-inf", Status::OK, Category::Infinity),
1674         (m_inf, m_zero, "-inf", Status::OK, Category::Infinity),
1675         (m_inf, qnan, "nan", Status::OK, Category::NaN),
1676         /*
1677         // See Note 1.
1678         (m_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
1679                 */
1680         (m_inf, p_normal_value, "-inf", Status::OK, Category::Infinity),
1681         (m_inf, m_normal_value, "-inf", Status::OK, Category::Infinity),
1682         (m_inf, p_largest_value, "-inf", Status::OK, Category::Infinity),
1683         (m_inf, m_largest_value, "-inf", Status::OK, Category::Infinity),
1684         (m_inf, p_smallest_value, "-inf", Status::OK, Category::Infinity),
1685         (m_inf, m_smallest_value, "-inf", Status::OK, Category::Infinity),
1686         (m_inf, p_smallest_normalized, "-inf", Status::OK, Category::Infinity),
1687         (m_inf, m_smallest_normalized, "-inf", Status::OK, Category::Infinity),
1688         (p_zero, p_inf, "inf", Status::OK, Category::Infinity),
1689         (p_zero, m_inf, "-inf", Status::OK, Category::Infinity),
1690         (p_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
1691         (p_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
1692         (p_zero, qnan, "nan", Status::OK, Category::NaN),
1693         /*
1694         // See Note 1.
1695         (p_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
1696                 */
1697         (p_zero, p_normal_value, "0x1p+0", Status::OK, Category::Normal),
1698         (p_zero, m_normal_value, "-0x1p+0", Status::OK, Category::Normal),
1699         (p_zero, p_largest_value, "0x1.fffffep+127", Status::OK, Category::Normal),
1700         (p_zero, m_largest_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
1701         (p_zero, p_smallest_value, "0x1p-149", Status::OK, Category::Normal),
1702         (p_zero, m_smallest_value, "-0x1p-149", Status::OK, Category::Normal),
1703         (p_zero, p_smallest_normalized, "0x1p-126", Status::OK, Category::Normal),
1704         (p_zero, m_smallest_normalized, "-0x1p-126", Status::OK, Category::Normal),
1705         (m_zero, p_inf, "inf", Status::OK, Category::Infinity),
1706         (m_zero, m_inf, "-inf", Status::OK, Category::Infinity),
1707         (m_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
1708         (m_zero, m_zero, "-0x0p+0", Status::OK, Category::Zero),
1709         (m_zero, qnan, "nan", Status::OK, Category::NaN),
1710         /*
1711         // See Note 1.
1712         (m_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
1713                 */
1714         (m_zero, p_normal_value, "0x1p+0", Status::OK, Category::Normal),
1715         (m_zero, m_normal_value, "-0x1p+0", Status::OK, Category::Normal),
1716         (m_zero, p_largest_value, "0x1.fffffep+127", Status::OK, Category::Normal),
1717         (m_zero, m_largest_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
1718         (m_zero, p_smallest_value, "0x1p-149", Status::OK, Category::Normal),
1719         (m_zero, m_smallest_value, "-0x1p-149", Status::OK, Category::Normal),
1720         (m_zero, p_smallest_normalized, "0x1p-126", Status::OK, Category::Normal),
1721         (m_zero, m_smallest_normalized, "-0x1p-126", Status::OK, Category::Normal),
1722         (qnan, p_inf, "nan", Status::OK, Category::NaN),
1723         (qnan, m_inf, "nan", Status::OK, Category::NaN),
1724         (qnan, p_zero, "nan", Status::OK, Category::NaN),
1725         (qnan, m_zero, "nan", Status::OK, Category::NaN),
1726         (qnan, qnan, "nan", Status::OK, Category::NaN),
1727         /*
1728         // See Note 1.
1729         (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
1730                 */
1731         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
1732         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
1733         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
1734         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
1735         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
1736         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
1737         (qnan, p_smallest_normalized, "nan", Status::OK, Category::NaN),
1738         (qnan, m_smallest_normalized, "nan", Status::OK, Category::NaN),
1739         /*
1740         // See Note 1.
1741         (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
1742         (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
1743         (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
1744         (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
1745         (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
1746         (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
1747         (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
1748         (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
1749         (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
1750         (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
1751         (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
1752         (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
1753         (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
1754         (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
1755                 */
1756         (p_normal_value, p_inf, "inf", Status::OK, Category::Infinity),
1757         (p_normal_value, m_inf, "-inf", Status::OK, Category::Infinity),
1758         (p_normal_value, p_zero, "0x1p+0", Status::OK, Category::Normal),
1759         (p_normal_value, m_zero, "0x1p+0", Status::OK, Category::Normal),
1760         (p_normal_value, qnan, "nan", Status::OK, Category::NaN),
1761         /*
1762         // See Note 1.
1763         (p_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
1764                 */
1765         (p_normal_value, p_normal_value, "0x1p+1", Status::OK, Category::Normal),
1766         (p_normal_value, m_normal_value, "0x0p+0", Status::OK, Category::Zero),
1767         (p_normal_value, p_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1768         (p_normal_value, m_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1769         (p_normal_value, p_smallest_value, "0x1p+0", Status::INEXACT, Category::Normal),
1770         (p_normal_value, m_smallest_value, "0x1p+0", Status::INEXACT, Category::Normal),
1771         (p_normal_value, p_smallest_normalized, "0x1p+0", Status::INEXACT, Category::Normal),
1772         (p_normal_value, m_smallest_normalized, "0x1p+0", Status::INEXACT, Category::Normal),
1773         (m_normal_value, p_inf, "inf", Status::OK, Category::Infinity),
1774         (m_normal_value, m_inf, "-inf", Status::OK, Category::Infinity),
1775         (m_normal_value, p_zero, "-0x1p+0", Status::OK, Category::Normal),
1776         (m_normal_value, m_zero, "-0x1p+0", Status::OK, Category::Normal),
1777         (m_normal_value, qnan, "nan", Status::OK, Category::NaN),
1778         /*
1779         // See Note 1.
1780         (m_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
1781                 */
1782         (m_normal_value, p_normal_value, "0x0p+0", Status::OK, Category::Zero),
1783         (m_normal_value, m_normal_value, "-0x1p+1", Status::OK, Category::Normal),
1784         (m_normal_value, p_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1785         (m_normal_value, m_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1786         (m_normal_value, p_smallest_value, "-0x1p+0", Status::INEXACT, Category::Normal),
1787         (m_normal_value, m_smallest_value, "-0x1p+0", Status::INEXACT, Category::Normal),
1788         (m_normal_value, p_smallest_normalized, "-0x1p+0", Status::INEXACT, Category::Normal),
1789         (m_normal_value, m_smallest_normalized, "-0x1p+0", Status::INEXACT, Category::Normal),
1790         (p_largest_value, p_inf, "inf", Status::OK, Category::Infinity),
1791         (p_largest_value, m_inf, "-inf", Status::OK, Category::Infinity),
1792         (p_largest_value, p_zero, "0x1.fffffep+127", Status::OK, Category::Normal),
1793         (p_largest_value, m_zero, "0x1.fffffep+127", Status::OK, Category::Normal),
1794         (p_largest_value, qnan, "nan", Status::OK, Category::NaN),
1795         /*
1796         // See Note 1.
1797         (p_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
1798                 */
1799         (p_largest_value, p_normal_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1800         (p_largest_value, m_normal_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1801         (p_largest_value, p_largest_value, "inf", overflow_status, Category::Infinity),
1802         (p_largest_value, m_largest_value, "0x0p+0", Status::OK, Category::Zero),
1803         (p_largest_value, p_smallest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1804         (p_largest_value, m_smallest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1805         (
1806             p_largest_value,
1807             p_smallest_normalized,
1808             "0x1.fffffep+127",
1809             Status::INEXACT,
1810             Category::Normal,
1811         ),
1812         (
1813             p_largest_value,
1814             m_smallest_normalized,
1815             "0x1.fffffep+127",
1816             Status::INEXACT,
1817             Category::Normal,
1818         ),
1819         (m_largest_value, p_inf, "inf", Status::OK, Category::Infinity),
1820         (m_largest_value, m_inf, "-inf", Status::OK, Category::Infinity),
1821         (m_largest_value, p_zero, "-0x1.fffffep+127", Status::OK, Category::Normal),
1822         (m_largest_value, m_zero, "-0x1.fffffep+127", Status::OK, Category::Normal),
1823         (m_largest_value, qnan, "nan", Status::OK, Category::NaN),
1824         /*
1825         // See Note 1.
1826         (m_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
1827                 */
1828         (m_largest_value, p_normal_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1829         (m_largest_value, m_normal_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1830         (m_largest_value, p_largest_value, "0x0p+0", Status::OK, Category::Zero),
1831         (m_largest_value, m_largest_value, "-inf", overflow_status, Category::Infinity),
1832         (m_largest_value, p_smallest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1833         (m_largest_value, m_smallest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1834         (
1835             m_largest_value,
1836             p_smallest_normalized,
1837             "-0x1.fffffep+127",
1838             Status::INEXACT,
1839             Category::Normal,
1840         ),
1841         (
1842             m_largest_value,
1843             m_smallest_normalized,
1844             "-0x1.fffffep+127",
1845             Status::INEXACT,
1846             Category::Normal,
1847         ),
1848         (p_smallest_value, p_inf, "inf", Status::OK, Category::Infinity),
1849         (p_smallest_value, m_inf, "-inf", Status::OK, Category::Infinity),
1850         (p_smallest_value, p_zero, "0x1p-149", Status::OK, Category::Normal),
1851         (p_smallest_value, m_zero, "0x1p-149", Status::OK, Category::Normal),
1852         (p_smallest_value, qnan, "nan", Status::OK, Category::NaN),
1853         /*
1854         // See Note 1.
1855         (p_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
1856                 */
1857         (p_smallest_value, p_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
1858         (p_smallest_value, m_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
1859         (p_smallest_value, p_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1860         (p_smallest_value, m_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1861         (p_smallest_value, p_smallest_value, "0x1p-148", Status::OK, Category::Normal),
1862         (p_smallest_value, m_smallest_value, "0x0p+0", Status::OK, Category::Zero),
1863         (p_smallest_value, p_smallest_normalized, "0x1.000002p-126", Status::OK, Category::Normal),
1864         (p_smallest_value, m_smallest_normalized, "-0x1.fffffcp-127", Status::OK, Category::Normal),
1865         (m_smallest_value, p_inf, "inf", Status::OK, Category::Infinity),
1866         (m_smallest_value, m_inf, "-inf", Status::OK, Category::Infinity),
1867         (m_smallest_value, p_zero, "-0x1p-149", Status::OK, Category::Normal),
1868         (m_smallest_value, m_zero, "-0x1p-149", Status::OK, Category::Normal),
1869         (m_smallest_value, qnan, "nan", Status::OK, Category::NaN),
1870         /*
1871         // See Note 1.
1872         (m_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
1873                 */
1874         (m_smallest_value, p_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
1875         (m_smallest_value, m_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
1876         (m_smallest_value, p_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
1877         (m_smallest_value, m_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
1878         (m_smallest_value, p_smallest_value, "0x0p+0", Status::OK, Category::Zero),
1879         (m_smallest_value, m_smallest_value, "-0x1p-148", Status::OK, Category::Normal),
1880         (m_smallest_value, p_smallest_normalized, "0x1.fffffcp-127", Status::OK, Category::Normal),
1881         (m_smallest_value, m_smallest_normalized, "-0x1.000002p-126", Status::OK, Category::Normal),
1882         (p_smallest_normalized, p_inf, "inf", Status::OK, Category::Infinity),
1883         (p_smallest_normalized, m_inf, "-inf", Status::OK, Category::Infinity),
1884         (p_smallest_normalized, p_zero, "0x1p-126", Status::OK, Category::Normal),
1885         (p_smallest_normalized, m_zero, "0x1p-126", Status::OK, Category::Normal),
1886         (p_smallest_normalized, qnan, "nan", Status::OK, Category::NaN),
1887         /*
1888         // See Note 1.
1889         (p_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
1890                 */
1891         (p_smallest_normalized, p_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
1892         (p_smallest_normalized, m_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
1893         (
1894             p_smallest_normalized,
1895             p_largest_value,
1896             "0x1.fffffep+127",
1897             Status::INEXACT,
1898             Category::Normal,
1899         ),
1900         (
1901             p_smallest_normalized,
1902             m_largest_value,
1903             "-0x1.fffffep+127",
1904             Status::INEXACT,
1905             Category::Normal,
1906         ),
1907         (p_smallest_normalized, p_smallest_value, "0x1.000002p-126", Status::OK, Category::Normal),
1908         (p_smallest_normalized, m_smallest_value, "0x1.fffffcp-127", Status::OK, Category::Normal),
1909         (p_smallest_normalized, p_smallest_normalized, "0x1p-125", Status::OK, Category::Normal),
1910         (p_smallest_normalized, m_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
1911         (m_smallest_normalized, p_inf, "inf", Status::OK, Category::Infinity),
1912         (m_smallest_normalized, m_inf, "-inf", Status::OK, Category::Infinity),
1913         (m_smallest_normalized, p_zero, "-0x1p-126", Status::OK, Category::Normal),
1914         (m_smallest_normalized, m_zero, "-0x1p-126", Status::OK, Category::Normal),
1915         (m_smallest_normalized, qnan, "nan", Status::OK, Category::NaN),
1916         /*
1917         // See Note 1.
1918         (m_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
1919                 */
1920         (m_smallest_normalized, p_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
1921         (m_smallest_normalized, m_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
1922         (
1923             m_smallest_normalized,
1924             p_largest_value,
1925             "0x1.fffffep+127",
1926             Status::INEXACT,
1927             Category::Normal,
1928         ),
1929         (
1930             m_smallest_normalized,
1931             m_largest_value,
1932             "-0x1.fffffep+127",
1933             Status::INEXACT,
1934             Category::Normal,
1935         ),
1936         (m_smallest_normalized, p_smallest_value, "-0x1.fffffcp-127", Status::OK, Category::Normal),
1937         (m_smallest_normalized, m_smallest_value, "-0x1.000002p-126", Status::OK, Category::Normal),
1938         (m_smallest_normalized, p_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
1939         (m_smallest_normalized, m_smallest_normalized, "-0x1p-125", Status::OK, Category::Normal),
1940     ];
1941
1942     for (x, y, e_result, e_status, e_category) in special_cases {
1943         let status;
1944         let result = unpack!(status=, x + y);
1945         assert_eq!(status, e_status);
1946         assert_eq!(result.category(), e_category);
1947         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
1948     }
1949 }
1950
1951 #[test]
1952 fn subtract() {
1953     // Test Special Cases against each other and normal values.
1954
1955     // FIXMES/NOTES:
1956     // 1. Since we perform only default exception handling all operations with
1957     // signaling NaNs should have a result that is a quiet NaN. Currently they
1958     // return sNaN.
1959
1960     let p_inf = Single::INFINITY;
1961     let m_inf = -Single::INFINITY;
1962     let p_zero = Single::ZERO;
1963     let m_zero = -Single::ZERO;
1964     let qnan = Single::NAN;
1965     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
1966     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
1967     let p_largest_value = Single::largest();
1968     let m_largest_value = -Single::largest();
1969     let p_smallest_value = Single::SMALLEST;
1970     let m_smallest_value = -Single::SMALLEST;
1971     let p_smallest_normalized = Single::smallest_normalized();
1972     let m_smallest_normalized = -Single::smallest_normalized();
1973
1974     let overflow_status = Status::OVERFLOW | Status::INEXACT;
1975
1976     let special_cases = [
1977         (p_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
1978         (p_inf, m_inf, "inf", Status::OK, Category::Infinity),
1979         (p_inf, p_zero, "inf", Status::OK, Category::Infinity),
1980         (p_inf, m_zero, "inf", Status::OK, Category::Infinity),
1981         (p_inf, qnan, "-nan", Status::OK, Category::NaN),
1982         /*
1983         // See Note 1.
1984         (p_inf, snan, "-nan", Status::INVALID_OP, Category::NaN),
1985                 */
1986         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
1987         (p_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
1988         (p_inf, p_largest_value, "inf", Status::OK, Category::Infinity),
1989         (p_inf, m_largest_value, "inf", Status::OK, Category::Infinity),
1990         (p_inf, p_smallest_value, "inf", Status::OK, Category::Infinity),
1991         (p_inf, m_smallest_value, "inf", Status::OK, Category::Infinity),
1992         (p_inf, p_smallest_normalized, "inf", Status::OK, Category::Infinity),
1993         (p_inf, m_smallest_normalized, "inf", Status::OK, Category::Infinity),
1994         (m_inf, p_inf, "-inf", Status::OK, Category::Infinity),
1995         (m_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
1996         (m_inf, p_zero, "-inf", Status::OK, Category::Infinity),
1997         (m_inf, m_zero, "-inf", Status::OK, Category::Infinity),
1998         (m_inf, qnan, "-nan", Status::OK, Category::NaN),
1999         /*
2000         // See Note 1.
2001         (m_inf, snan, "-nan", Status::INVALID_OP, Category::NaN),
2002                 */
2003         (m_inf, p_normal_value, "-inf", Status::OK, Category::Infinity),
2004         (m_inf, m_normal_value, "-inf", Status::OK, Category::Infinity),
2005         (m_inf, p_largest_value, "-inf", Status::OK, Category::Infinity),
2006         (m_inf, m_largest_value, "-inf", Status::OK, Category::Infinity),
2007         (m_inf, p_smallest_value, "-inf", Status::OK, Category::Infinity),
2008         (m_inf, m_smallest_value, "-inf", Status::OK, Category::Infinity),
2009         (m_inf, p_smallest_normalized, "-inf", Status::OK, Category::Infinity),
2010         (m_inf, m_smallest_normalized, "-inf", Status::OK, Category::Infinity),
2011         (p_zero, p_inf, "-inf", Status::OK, Category::Infinity),
2012         (p_zero, m_inf, "inf", Status::OK, Category::Infinity),
2013         (p_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
2014         (p_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
2015         (p_zero, qnan, "-nan", Status::OK, Category::NaN),
2016         /*
2017         // See Note 1.
2018         (p_zero, snan, "-nan", Status::INVALID_OP, Category::NaN),
2019                 */
2020         (p_zero, p_normal_value, "-0x1p+0", Status::OK, Category::Normal),
2021         (p_zero, m_normal_value, "0x1p+0", Status::OK, Category::Normal),
2022         (p_zero, p_largest_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2023         (p_zero, m_largest_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2024         (p_zero, p_smallest_value, "-0x1p-149", Status::OK, Category::Normal),
2025         (p_zero, m_smallest_value, "0x1p-149", Status::OK, Category::Normal),
2026         (p_zero, p_smallest_normalized, "-0x1p-126", Status::OK, Category::Normal),
2027         (p_zero, m_smallest_normalized, "0x1p-126", Status::OK, Category::Normal),
2028         (m_zero, p_inf, "-inf", Status::OK, Category::Infinity),
2029         (m_zero, m_inf, "inf", Status::OK, Category::Infinity),
2030         (m_zero, p_zero, "-0x0p+0", Status::OK, Category::Zero),
2031         (m_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
2032         (m_zero, qnan, "-nan", Status::OK, Category::NaN),
2033         /*
2034         // See Note 1.
2035         (m_zero, snan, "-nan", Status::INVALID_OP, Category::NaN),
2036                 */
2037         (m_zero, p_normal_value, "-0x1p+0", Status::OK, Category::Normal),
2038         (m_zero, m_normal_value, "0x1p+0", Status::OK, Category::Normal),
2039         (m_zero, p_largest_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2040         (m_zero, m_largest_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2041         (m_zero, p_smallest_value, "-0x1p-149", Status::OK, Category::Normal),
2042         (m_zero, m_smallest_value, "0x1p-149", Status::OK, Category::Normal),
2043         (m_zero, p_smallest_normalized, "-0x1p-126", Status::OK, Category::Normal),
2044         (m_zero, m_smallest_normalized, "0x1p-126", Status::OK, Category::Normal),
2045         (qnan, p_inf, "nan", Status::OK, Category::NaN),
2046         (qnan, m_inf, "nan", Status::OK, Category::NaN),
2047         (qnan, p_zero, "nan", Status::OK, Category::NaN),
2048         (qnan, m_zero, "nan", Status::OK, Category::NaN),
2049         (qnan, qnan, "nan", Status::OK, Category::NaN),
2050         /*
2051         // See Note 1.
2052         (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
2053                 */
2054         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
2055         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
2056         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
2057         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
2058         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
2059         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
2060         (qnan, p_smallest_normalized, "nan", Status::OK, Category::NaN),
2061         (qnan, m_smallest_normalized, "nan", Status::OK, Category::NaN),
2062         /*
2063         // See Note 1.
2064         (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2065         (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2066         (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2067         (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2068         (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
2069         (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
2070         (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2071         (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2072         (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2073         (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2074         (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2075         (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2076         (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2077         (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2078                 */
2079         (p_normal_value, p_inf, "-inf", Status::OK, Category::Infinity),
2080         (p_normal_value, m_inf, "inf", Status::OK, Category::Infinity),
2081         (p_normal_value, p_zero, "0x1p+0", Status::OK, Category::Normal),
2082         (p_normal_value, m_zero, "0x1p+0", Status::OK, Category::Normal),
2083         (p_normal_value, qnan, "-nan", Status::OK, Category::NaN),
2084         /*
2085         // See Note 1.
2086         (p_normal_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
2087                 */
2088         (p_normal_value, p_normal_value, "0x0p+0", Status::OK, Category::Zero),
2089         (p_normal_value, m_normal_value, "0x1p+1", Status::OK, Category::Normal),
2090         (p_normal_value, p_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2091         (p_normal_value, m_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2092         (p_normal_value, p_smallest_value, "0x1p+0", Status::INEXACT, Category::Normal),
2093         (p_normal_value, m_smallest_value, "0x1p+0", Status::INEXACT, Category::Normal),
2094         (p_normal_value, p_smallest_normalized, "0x1p+0", Status::INEXACT, Category::Normal),
2095         (p_normal_value, m_smallest_normalized, "0x1p+0", Status::INEXACT, Category::Normal),
2096         (m_normal_value, p_inf, "-inf", Status::OK, Category::Infinity),
2097         (m_normal_value, m_inf, "inf", Status::OK, Category::Infinity),
2098         (m_normal_value, p_zero, "-0x1p+0", Status::OK, Category::Normal),
2099         (m_normal_value, m_zero, "-0x1p+0", Status::OK, Category::Normal),
2100         (m_normal_value, qnan, "-nan", Status::OK, Category::NaN),
2101         /*
2102         // See Note 1.
2103         (m_normal_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
2104                 */
2105         (m_normal_value, p_normal_value, "-0x1p+1", Status::OK, Category::Normal),
2106         (m_normal_value, m_normal_value, "0x0p+0", Status::OK, Category::Zero),
2107         (m_normal_value, p_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2108         (m_normal_value, m_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2109         (m_normal_value, p_smallest_value, "-0x1p+0", Status::INEXACT, Category::Normal),
2110         (m_normal_value, m_smallest_value, "-0x1p+0", Status::INEXACT, Category::Normal),
2111         (m_normal_value, p_smallest_normalized, "-0x1p+0", Status::INEXACT, Category::Normal),
2112         (m_normal_value, m_smallest_normalized, "-0x1p+0", Status::INEXACT, Category::Normal),
2113         (p_largest_value, p_inf, "-inf", Status::OK, Category::Infinity),
2114         (p_largest_value, m_inf, "inf", Status::OK, Category::Infinity),
2115         (p_largest_value, p_zero, "0x1.fffffep+127", Status::OK, Category::Normal),
2116         (p_largest_value, m_zero, "0x1.fffffep+127", Status::OK, Category::Normal),
2117         (p_largest_value, qnan, "-nan", Status::OK, Category::NaN),
2118         /*
2119         // See Note 1.
2120         (p_largest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
2121                 */
2122         (p_largest_value, p_normal_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2123         (p_largest_value, m_normal_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2124         (p_largest_value, p_largest_value, "0x0p+0", Status::OK, Category::Zero),
2125         (p_largest_value, m_largest_value, "inf", overflow_status, Category::Infinity),
2126         (p_largest_value, p_smallest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2127         (p_largest_value, m_smallest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2128         (
2129             p_largest_value,
2130             p_smallest_normalized,
2131             "0x1.fffffep+127",
2132             Status::INEXACT,
2133             Category::Normal,
2134         ),
2135         (
2136             p_largest_value,
2137             m_smallest_normalized,
2138             "0x1.fffffep+127",
2139             Status::INEXACT,
2140             Category::Normal,
2141         ),
2142         (m_largest_value, p_inf, "-inf", Status::OK, Category::Infinity),
2143         (m_largest_value, m_inf, "inf", Status::OK, Category::Infinity),
2144         (m_largest_value, p_zero, "-0x1.fffffep+127", Status::OK, Category::Normal),
2145         (m_largest_value, m_zero, "-0x1.fffffep+127", Status::OK, Category::Normal),
2146         (m_largest_value, qnan, "-nan", Status::OK, Category::NaN),
2147         /*
2148         // See Note 1.
2149         (m_largest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
2150                 */
2151         (m_largest_value, p_normal_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2152         (m_largest_value, m_normal_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2153         (m_largest_value, p_largest_value, "-inf", overflow_status, Category::Infinity),
2154         (m_largest_value, m_largest_value, "0x0p+0", Status::OK, Category::Zero),
2155         (m_largest_value, p_smallest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2156         (m_largest_value, m_smallest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2157         (
2158             m_largest_value,
2159             p_smallest_normalized,
2160             "-0x1.fffffep+127",
2161             Status::INEXACT,
2162             Category::Normal,
2163         ),
2164         (
2165             m_largest_value,
2166             m_smallest_normalized,
2167             "-0x1.fffffep+127",
2168             Status::INEXACT,
2169             Category::Normal,
2170         ),
2171         (p_smallest_value, p_inf, "-inf", Status::OK, Category::Infinity),
2172         (p_smallest_value, m_inf, "inf", Status::OK, Category::Infinity),
2173         (p_smallest_value, p_zero, "0x1p-149", Status::OK, Category::Normal),
2174         (p_smallest_value, m_zero, "0x1p-149", Status::OK, Category::Normal),
2175         (p_smallest_value, qnan, "-nan", Status::OK, Category::NaN),
2176         /*
2177         // See Note 1.
2178         (p_smallest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
2179                 */
2180         (p_smallest_value, p_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
2181         (p_smallest_value, m_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
2182         (p_smallest_value, p_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2183         (p_smallest_value, m_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2184         (p_smallest_value, p_smallest_value, "0x0p+0", Status::OK, Category::Zero),
2185         (p_smallest_value, m_smallest_value, "0x1p-148", Status::OK, Category::Normal),
2186         (p_smallest_value, p_smallest_normalized, "-0x1.fffffcp-127", Status::OK, Category::Normal),
2187         (p_smallest_value, m_smallest_normalized, "0x1.000002p-126", Status::OK, Category::Normal),
2188         (m_smallest_value, p_inf, "-inf", Status::OK, Category::Infinity),
2189         (m_smallest_value, m_inf, "inf", Status::OK, Category::Infinity),
2190         (m_smallest_value, p_zero, "-0x1p-149", Status::OK, Category::Normal),
2191         (m_smallest_value, m_zero, "-0x1p-149", Status::OK, Category::Normal),
2192         (m_smallest_value, qnan, "-nan", Status::OK, Category::NaN),
2193         /*
2194         // See Note 1.
2195         (m_smallest_value, snan, "-nan", Status::INVALID_OP, Category::NaN),
2196                 */
2197         (m_smallest_value, p_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
2198         (m_smallest_value, m_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
2199         (m_smallest_value, p_largest_value, "-0x1.fffffep+127", Status::INEXACT, Category::Normal),
2200         (m_smallest_value, m_largest_value, "0x1.fffffep+127", Status::INEXACT, Category::Normal),
2201         (m_smallest_value, p_smallest_value, "-0x1p-148", Status::OK, Category::Normal),
2202         (m_smallest_value, m_smallest_value, "0x0p+0", Status::OK, Category::Zero),
2203         (m_smallest_value, p_smallest_normalized, "-0x1.000002p-126", Status::OK, Category::Normal),
2204         (m_smallest_value, m_smallest_normalized, "0x1.fffffcp-127", Status::OK, Category::Normal),
2205         (p_smallest_normalized, p_inf, "-inf", Status::OK, Category::Infinity),
2206         (p_smallest_normalized, m_inf, "inf", Status::OK, Category::Infinity),
2207         (p_smallest_normalized, p_zero, "0x1p-126", Status::OK, Category::Normal),
2208         (p_smallest_normalized, m_zero, "0x1p-126", Status::OK, Category::Normal),
2209         (p_smallest_normalized, qnan, "-nan", Status::OK, Category::NaN),
2210         /*
2211         // See Note 1.
2212         (p_smallest_normalized, snan, "-nan", Status::INVALID_OP, Category::NaN),
2213                 */
2214         (p_smallest_normalized, p_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
2215         (p_smallest_normalized, m_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
2216         (
2217             p_smallest_normalized,
2218             p_largest_value,
2219             "-0x1.fffffep+127",
2220             Status::INEXACT,
2221             Category::Normal,
2222         ),
2223         (
2224             p_smallest_normalized,
2225             m_largest_value,
2226             "0x1.fffffep+127",
2227             Status::INEXACT,
2228             Category::Normal,
2229         ),
2230         (p_smallest_normalized, p_smallest_value, "0x1.fffffcp-127", Status::OK, Category::Normal),
2231         (p_smallest_normalized, m_smallest_value, "0x1.000002p-126", Status::OK, Category::Normal),
2232         (p_smallest_normalized, p_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
2233         (p_smallest_normalized, m_smallest_normalized, "0x1p-125", Status::OK, Category::Normal),
2234         (m_smallest_normalized, p_inf, "-inf", Status::OK, Category::Infinity),
2235         (m_smallest_normalized, m_inf, "inf", Status::OK, Category::Infinity),
2236         (m_smallest_normalized, p_zero, "-0x1p-126", Status::OK, Category::Normal),
2237         (m_smallest_normalized, m_zero, "-0x1p-126", Status::OK, Category::Normal),
2238         (m_smallest_normalized, qnan, "-nan", Status::OK, Category::NaN),
2239         /*
2240         // See Note 1.
2241         (m_smallest_normalized, snan, "-nan", Status::INVALID_OP, Category::NaN),
2242                 */
2243         (m_smallest_normalized, p_normal_value, "-0x1p+0", Status::INEXACT, Category::Normal),
2244         (m_smallest_normalized, m_normal_value, "0x1p+0", Status::INEXACT, Category::Normal),
2245         (
2246             m_smallest_normalized,
2247             p_largest_value,
2248             "-0x1.fffffep+127",
2249             Status::INEXACT,
2250             Category::Normal,
2251         ),
2252         (
2253             m_smallest_normalized,
2254             m_largest_value,
2255             "0x1.fffffep+127",
2256             Status::INEXACT,
2257             Category::Normal,
2258         ),
2259         (m_smallest_normalized, p_smallest_value, "-0x1.000002p-126", Status::OK, Category::Normal),
2260         (m_smallest_normalized, m_smallest_value, "-0x1.fffffcp-127", Status::OK, Category::Normal),
2261         (m_smallest_normalized, p_smallest_normalized, "-0x1p-125", Status::OK, Category::Normal),
2262         (m_smallest_normalized, m_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
2263     ];
2264
2265     for (x, y, e_result, e_status, e_category) in special_cases {
2266         let status;
2267         let result = unpack!(status=, x - y);
2268         assert_eq!(status, e_status);
2269         assert_eq!(result.category(), e_category);
2270         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
2271     }
2272 }
2273
2274 #[test]
2275 fn multiply() {
2276     // Test Special Cases against each other and normal values.
2277
2278     // FIXMES/NOTES:
2279     // 1. Since we perform only default exception handling all operations with
2280     // signaling NaNs should have a result that is a quiet NaN. Currently they
2281     // return sNaN.
2282
2283     let p_inf = Single::INFINITY;
2284     let m_inf = -Single::INFINITY;
2285     let p_zero = Single::ZERO;
2286     let m_zero = -Single::ZERO;
2287     let qnan = Single::NAN;
2288     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
2289     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
2290     let p_largest_value = Single::largest();
2291     let m_largest_value = -Single::largest();
2292     let p_smallest_value = Single::SMALLEST;
2293     let m_smallest_value = -Single::SMALLEST;
2294     let p_smallest_normalized = Single::smallest_normalized();
2295     let m_smallest_normalized = -Single::smallest_normalized();
2296
2297     let overflow_status = Status::OVERFLOW | Status::INEXACT;
2298     let underflow_status = Status::UNDERFLOW | Status::INEXACT;
2299
2300     let special_cases = [
2301         (p_inf, p_inf, "inf", Status::OK, Category::Infinity),
2302         (p_inf, m_inf, "-inf", Status::OK, Category::Infinity),
2303         (p_inf, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2304         (p_inf, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2305         (p_inf, qnan, "nan", Status::OK, Category::NaN),
2306         /*
2307         // See Note 1.
2308         (p_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
2309                 */
2310         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
2311         (p_inf, m_normal_value, "-inf", Status::OK, Category::Infinity),
2312         (p_inf, p_largest_value, "inf", Status::OK, Category::Infinity),
2313         (p_inf, m_largest_value, "-inf", Status::OK, Category::Infinity),
2314         (p_inf, p_smallest_value, "inf", Status::OK, Category::Infinity),
2315         (p_inf, m_smallest_value, "-inf", Status::OK, Category::Infinity),
2316         (p_inf, p_smallest_normalized, "inf", Status::OK, Category::Infinity),
2317         (p_inf, m_smallest_normalized, "-inf", Status::OK, Category::Infinity),
2318         (m_inf, p_inf, "-inf", Status::OK, Category::Infinity),
2319         (m_inf, m_inf, "inf", Status::OK, Category::Infinity),
2320         (m_inf, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2321         (m_inf, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2322         (m_inf, qnan, "nan", Status::OK, Category::NaN),
2323         /*
2324         // See Note 1.
2325         (m_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
2326                 */
2327         (m_inf, p_normal_value, "-inf", Status::OK, Category::Infinity),
2328         (m_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
2329         (m_inf, p_largest_value, "-inf", Status::OK, Category::Infinity),
2330         (m_inf, m_largest_value, "inf", Status::OK, Category::Infinity),
2331         (m_inf, p_smallest_value, "-inf", Status::OK, Category::Infinity),
2332         (m_inf, m_smallest_value, "inf", Status::OK, Category::Infinity),
2333         (m_inf, p_smallest_normalized, "-inf", Status::OK, Category::Infinity),
2334         (m_inf, m_smallest_normalized, "inf", Status::OK, Category::Infinity),
2335         (p_zero, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2336         (p_zero, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2337         (p_zero, p_zero, "0x0p+0", Status::OK, Category::Zero),
2338         (p_zero, m_zero, "-0x0p+0", Status::OK, Category::Zero),
2339         (p_zero, qnan, "nan", Status::OK, Category::NaN),
2340         /*
2341         // See Note 1.
2342         (p_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
2343                 */
2344         (p_zero, p_normal_value, "0x0p+0", Status::OK, Category::Zero),
2345         (p_zero, m_normal_value, "-0x0p+0", Status::OK, Category::Zero),
2346         (p_zero, p_largest_value, "0x0p+0", Status::OK, Category::Zero),
2347         (p_zero, m_largest_value, "-0x0p+0", Status::OK, Category::Zero),
2348         (p_zero, p_smallest_value, "0x0p+0", Status::OK, Category::Zero),
2349         (p_zero, m_smallest_value, "-0x0p+0", Status::OK, Category::Zero),
2350         (p_zero, p_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
2351         (p_zero, m_smallest_normalized, "-0x0p+0", Status::OK, Category::Zero),
2352         (m_zero, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2353         (m_zero, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2354         (m_zero, p_zero, "-0x0p+0", Status::OK, Category::Zero),
2355         (m_zero, m_zero, "0x0p+0", Status::OK, Category::Zero),
2356         (m_zero, qnan, "nan", Status::OK, Category::NaN),
2357         /*
2358         // See Note 1.
2359         (m_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
2360                 */
2361         (m_zero, p_normal_value, "-0x0p+0", Status::OK, Category::Zero),
2362         (m_zero, m_normal_value, "0x0p+0", Status::OK, Category::Zero),
2363         (m_zero, p_largest_value, "-0x0p+0", Status::OK, Category::Zero),
2364         (m_zero, m_largest_value, "0x0p+0", Status::OK, Category::Zero),
2365         (m_zero, p_smallest_value, "-0x0p+0", Status::OK, Category::Zero),
2366         (m_zero, m_smallest_value, "0x0p+0", Status::OK, Category::Zero),
2367         (m_zero, p_smallest_normalized, "-0x0p+0", Status::OK, Category::Zero),
2368         (m_zero, m_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
2369         (qnan, p_inf, "nan", Status::OK, Category::NaN),
2370         (qnan, m_inf, "nan", Status::OK, Category::NaN),
2371         (qnan, p_zero, "nan", Status::OK, Category::NaN),
2372         (qnan, m_zero, "nan", Status::OK, Category::NaN),
2373         (qnan, qnan, "nan", Status::OK, Category::NaN),
2374         /*
2375         // See Note 1.
2376         (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
2377                 */
2378         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
2379         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
2380         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
2381         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
2382         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
2383         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
2384         (qnan, p_smallest_normalized, "nan", Status::OK, Category::NaN),
2385         (qnan, m_smallest_normalized, "nan", Status::OK, Category::NaN),
2386         /*
2387         // See Note 1.
2388         (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2389         (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2390         (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2391         (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2392         (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
2393         (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
2394         (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2395         (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2396         (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2397         (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2398         (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2399         (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2400         (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2401         (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2402                 */
2403         (p_normal_value, p_inf, "inf", Status::OK, Category::Infinity),
2404         (p_normal_value, m_inf, "-inf", Status::OK, Category::Infinity),
2405         (p_normal_value, p_zero, "0x0p+0", Status::OK, Category::Zero),
2406         (p_normal_value, m_zero, "-0x0p+0", Status::OK, Category::Zero),
2407         (p_normal_value, qnan, "nan", Status::OK, Category::NaN),
2408         /*
2409         // See Note 1.
2410         (p_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2411                 */
2412         (p_normal_value, p_normal_value, "0x1p+0", Status::OK, Category::Normal),
2413         (p_normal_value, m_normal_value, "-0x1p+0", Status::OK, Category::Normal),
2414         (p_normal_value, p_largest_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2415         (p_normal_value, m_largest_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2416         (p_normal_value, p_smallest_value, "0x1p-149", Status::OK, Category::Normal),
2417         (p_normal_value, m_smallest_value, "-0x1p-149", Status::OK, Category::Normal),
2418         (p_normal_value, p_smallest_normalized, "0x1p-126", Status::OK, Category::Normal),
2419         (p_normal_value, m_smallest_normalized, "-0x1p-126", Status::OK, Category::Normal),
2420         (m_normal_value, p_inf, "-inf", Status::OK, Category::Infinity),
2421         (m_normal_value, m_inf, "inf", Status::OK, Category::Infinity),
2422         (m_normal_value, p_zero, "-0x0p+0", Status::OK, Category::Zero),
2423         (m_normal_value, m_zero, "0x0p+0", Status::OK, Category::Zero),
2424         (m_normal_value, qnan, "nan", Status::OK, Category::NaN),
2425         /*
2426         // See Note 1.
2427         (m_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2428                 */
2429         (m_normal_value, p_normal_value, "-0x1p+0", Status::OK, Category::Normal),
2430         (m_normal_value, m_normal_value, "0x1p+0", Status::OK, Category::Normal),
2431         (m_normal_value, p_largest_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2432         (m_normal_value, m_largest_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2433         (m_normal_value, p_smallest_value, "-0x1p-149", Status::OK, Category::Normal),
2434         (m_normal_value, m_smallest_value, "0x1p-149", Status::OK, Category::Normal),
2435         (m_normal_value, p_smallest_normalized, "-0x1p-126", Status::OK, Category::Normal),
2436         (m_normal_value, m_smallest_normalized, "0x1p-126", Status::OK, Category::Normal),
2437         (p_largest_value, p_inf, "inf", Status::OK, Category::Infinity),
2438         (p_largest_value, m_inf, "-inf", Status::OK, Category::Infinity),
2439         (p_largest_value, p_zero, "0x0p+0", Status::OK, Category::Zero),
2440         (p_largest_value, m_zero, "-0x0p+0", Status::OK, Category::Zero),
2441         (p_largest_value, qnan, "nan", Status::OK, Category::NaN),
2442         /*
2443         // See Note 1.
2444         (p_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2445                 */
2446         (p_largest_value, p_normal_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2447         (p_largest_value, m_normal_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2448         (p_largest_value, p_largest_value, "inf", overflow_status, Category::Infinity),
2449         (p_largest_value, m_largest_value, "-inf", overflow_status, Category::Infinity),
2450         (p_largest_value, p_smallest_value, "0x1.fffffep-22", Status::OK, Category::Normal),
2451         (p_largest_value, m_smallest_value, "-0x1.fffffep-22", Status::OK, Category::Normal),
2452         (p_largest_value, p_smallest_normalized, "0x1.fffffep+1", Status::OK, Category::Normal),
2453         (p_largest_value, m_smallest_normalized, "-0x1.fffffep+1", Status::OK, Category::Normal),
2454         (m_largest_value, p_inf, "-inf", Status::OK, Category::Infinity),
2455         (m_largest_value, m_inf, "inf", Status::OK, Category::Infinity),
2456         (m_largest_value, p_zero, "-0x0p+0", Status::OK, Category::Zero),
2457         (m_largest_value, m_zero, "0x0p+0", Status::OK, Category::Zero),
2458         (m_largest_value, qnan, "nan", Status::OK, Category::NaN),
2459         /*
2460         // See Note 1.
2461         (m_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2462                 */
2463         (m_largest_value, p_normal_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2464         (m_largest_value, m_normal_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2465         (m_largest_value, p_largest_value, "-inf", overflow_status, Category::Infinity),
2466         (m_largest_value, m_largest_value, "inf", overflow_status, Category::Infinity),
2467         (m_largest_value, p_smallest_value, "-0x1.fffffep-22", Status::OK, Category::Normal),
2468         (m_largest_value, m_smallest_value, "0x1.fffffep-22", Status::OK, Category::Normal),
2469         (m_largest_value, p_smallest_normalized, "-0x1.fffffep+1", Status::OK, Category::Normal),
2470         (m_largest_value, m_smallest_normalized, "0x1.fffffep+1", Status::OK, Category::Normal),
2471         (p_smallest_value, p_inf, "inf", Status::OK, Category::Infinity),
2472         (p_smallest_value, m_inf, "-inf", Status::OK, Category::Infinity),
2473         (p_smallest_value, p_zero, "0x0p+0", Status::OK, Category::Zero),
2474         (p_smallest_value, m_zero, "-0x0p+0", Status::OK, Category::Zero),
2475         (p_smallest_value, qnan, "nan", Status::OK, Category::NaN),
2476         /*
2477         // See Note 1.
2478         (p_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2479                 */
2480         (p_smallest_value, p_normal_value, "0x1p-149", Status::OK, Category::Normal),
2481         (p_smallest_value, m_normal_value, "-0x1p-149", Status::OK, Category::Normal),
2482         (p_smallest_value, p_largest_value, "0x1.fffffep-22", Status::OK, Category::Normal),
2483         (p_smallest_value, m_largest_value, "-0x1.fffffep-22", Status::OK, Category::Normal),
2484         (p_smallest_value, p_smallest_value, "0x0p+0", underflow_status, Category::Zero),
2485         (p_smallest_value, m_smallest_value, "-0x0p+0", underflow_status, Category::Zero),
2486         (p_smallest_value, p_smallest_normalized, "0x0p+0", underflow_status, Category::Zero),
2487         (p_smallest_value, m_smallest_normalized, "-0x0p+0", underflow_status, Category::Zero),
2488         (m_smallest_value, p_inf, "-inf", Status::OK, Category::Infinity),
2489         (m_smallest_value, m_inf, "inf", Status::OK, Category::Infinity),
2490         (m_smallest_value, p_zero, "-0x0p+0", Status::OK, Category::Zero),
2491         (m_smallest_value, m_zero, "0x0p+0", Status::OK, Category::Zero),
2492         (m_smallest_value, qnan, "nan", Status::OK, Category::NaN),
2493         /*
2494         // See Note 1.
2495         (m_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2496                 */
2497         (m_smallest_value, p_normal_value, "-0x1p-149", Status::OK, Category::Normal),
2498         (m_smallest_value, m_normal_value, "0x1p-149", Status::OK, Category::Normal),
2499         (m_smallest_value, p_largest_value, "-0x1.fffffep-22", Status::OK, Category::Normal),
2500         (m_smallest_value, m_largest_value, "0x1.fffffep-22", Status::OK, Category::Normal),
2501         (m_smallest_value, p_smallest_value, "-0x0p+0", underflow_status, Category::Zero),
2502         (m_smallest_value, m_smallest_value, "0x0p+0", underflow_status, Category::Zero),
2503         (m_smallest_value, p_smallest_normalized, "-0x0p+0", underflow_status, Category::Zero),
2504         (m_smallest_value, m_smallest_normalized, "0x0p+0", underflow_status, Category::Zero),
2505         (p_smallest_normalized, p_inf, "inf", Status::OK, Category::Infinity),
2506         (p_smallest_normalized, m_inf, "-inf", Status::OK, Category::Infinity),
2507         (p_smallest_normalized, p_zero, "0x0p+0", Status::OK, Category::Zero),
2508         (p_smallest_normalized, m_zero, "-0x0p+0", Status::OK, Category::Zero),
2509         (p_smallest_normalized, qnan, "nan", Status::OK, Category::NaN),
2510         /*
2511         // See Note 1.
2512         (p_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
2513                 */
2514         (p_smallest_normalized, p_normal_value, "0x1p-126", Status::OK, Category::Normal),
2515         (p_smallest_normalized, m_normal_value, "-0x1p-126", Status::OK, Category::Normal),
2516         (p_smallest_normalized, p_largest_value, "0x1.fffffep+1", Status::OK, Category::Normal),
2517         (p_smallest_normalized, m_largest_value, "-0x1.fffffep+1", Status::OK, Category::Normal),
2518         (p_smallest_normalized, p_smallest_value, "0x0p+0", underflow_status, Category::Zero),
2519         (p_smallest_normalized, m_smallest_value, "-0x0p+0", underflow_status, Category::Zero),
2520         (p_smallest_normalized, p_smallest_normalized, "0x0p+0", underflow_status, Category::Zero),
2521         (p_smallest_normalized, m_smallest_normalized, "-0x0p+0", underflow_status, Category::Zero),
2522         (m_smallest_normalized, p_inf, "-inf", Status::OK, Category::Infinity),
2523         (m_smallest_normalized, m_inf, "inf", Status::OK, Category::Infinity),
2524         (m_smallest_normalized, p_zero, "-0x0p+0", Status::OK, Category::Zero),
2525         (m_smallest_normalized, m_zero, "0x0p+0", Status::OK, Category::Zero),
2526         (m_smallest_normalized, qnan, "nan", Status::OK, Category::NaN),
2527         /*
2528         // See Note 1.
2529         (m_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
2530                 */
2531         (m_smallest_normalized, p_normal_value, "-0x1p-126", Status::OK, Category::Normal),
2532         (m_smallest_normalized, m_normal_value, "0x1p-126", Status::OK, Category::Normal),
2533         (m_smallest_normalized, p_largest_value, "-0x1.fffffep+1", Status::OK, Category::Normal),
2534         (m_smallest_normalized, m_largest_value, "0x1.fffffep+1", Status::OK, Category::Normal),
2535         (m_smallest_normalized, p_smallest_value, "-0x0p+0", underflow_status, Category::Zero),
2536         (m_smallest_normalized, m_smallest_value, "0x0p+0", underflow_status, Category::Zero),
2537         (m_smallest_normalized, p_smallest_normalized, "-0x0p+0", underflow_status, Category::Zero),
2538         (m_smallest_normalized, m_smallest_normalized, "0x0p+0", underflow_status, Category::Zero),
2539     ];
2540
2541     for (x, y, e_result, e_status, e_category) in special_cases {
2542         let status;
2543         let result = unpack!(status=, x * y);
2544         assert_eq!(status, e_status);
2545         assert_eq!(result.category(), e_category);
2546         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
2547     }
2548 }
2549
2550 #[test]
2551 fn divide() {
2552     // Test Special Cases against each other and normal values.
2553
2554     // FIXMES/NOTES:
2555     // 1. Since we perform only default exception handling all operations with
2556     // signaling NaNs should have a result that is a quiet NaN. Currently they
2557     // return sNaN.
2558
2559     let p_inf = Single::INFINITY;
2560     let m_inf = -Single::INFINITY;
2561     let p_zero = Single::ZERO;
2562     let m_zero = -Single::ZERO;
2563     let qnan = Single::NAN;
2564     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
2565     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
2566     let p_largest_value = Single::largest();
2567     let m_largest_value = -Single::largest();
2568     let p_smallest_value = Single::SMALLEST;
2569     let m_smallest_value = -Single::SMALLEST;
2570     let p_smallest_normalized = Single::smallest_normalized();
2571     let m_smallest_normalized = -Single::smallest_normalized();
2572
2573     let overflow_status = Status::OVERFLOW | Status::INEXACT;
2574     let underflow_status = Status::UNDERFLOW | Status::INEXACT;
2575
2576     let special_cases = [
2577         (p_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2578         (p_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2579         (p_inf, p_zero, "inf", Status::OK, Category::Infinity),
2580         (p_inf, m_zero, "-inf", Status::OK, Category::Infinity),
2581         (p_inf, qnan, "nan", Status::OK, Category::NaN),
2582         /*
2583         // See Note 1.
2584         (p_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
2585                 */
2586         (p_inf, p_normal_value, "inf", Status::OK, Category::Infinity),
2587         (p_inf, m_normal_value, "-inf", Status::OK, Category::Infinity),
2588         (p_inf, p_largest_value, "inf", Status::OK, Category::Infinity),
2589         (p_inf, m_largest_value, "-inf", Status::OK, Category::Infinity),
2590         (p_inf, p_smallest_value, "inf", Status::OK, Category::Infinity),
2591         (p_inf, m_smallest_value, "-inf", Status::OK, Category::Infinity),
2592         (p_inf, p_smallest_normalized, "inf", Status::OK, Category::Infinity),
2593         (p_inf, m_smallest_normalized, "-inf", Status::OK, Category::Infinity),
2594         (m_inf, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2595         (m_inf, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2596         (m_inf, p_zero, "-inf", Status::OK, Category::Infinity),
2597         (m_inf, m_zero, "inf", Status::OK, Category::Infinity),
2598         (m_inf, qnan, "nan", Status::OK, Category::NaN),
2599         /*
2600         // See Note 1.
2601         (m_inf, snan, "nan", Status::INVALID_OP, Category::NaN),
2602                 */
2603         (m_inf, p_normal_value, "-inf", Status::OK, Category::Infinity),
2604         (m_inf, m_normal_value, "inf", Status::OK, Category::Infinity),
2605         (m_inf, p_largest_value, "-inf", Status::OK, Category::Infinity),
2606         (m_inf, m_largest_value, "inf", Status::OK, Category::Infinity),
2607         (m_inf, p_smallest_value, "-inf", Status::OK, Category::Infinity),
2608         (m_inf, m_smallest_value, "inf", Status::OK, Category::Infinity),
2609         (m_inf, p_smallest_normalized, "-inf", Status::OK, Category::Infinity),
2610         (m_inf, m_smallest_normalized, "inf", Status::OK, Category::Infinity),
2611         (p_zero, p_inf, "0x0p+0", Status::OK, Category::Zero),
2612         (p_zero, m_inf, "-0x0p+0", Status::OK, Category::Zero),
2613         (p_zero, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2614         (p_zero, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2615         (p_zero, qnan, "nan", Status::OK, Category::NaN),
2616         /*
2617         // See Note 1.
2618         (p_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
2619                 */
2620         (p_zero, p_normal_value, "0x0p+0", Status::OK, Category::Zero),
2621         (p_zero, m_normal_value, "-0x0p+0", Status::OK, Category::Zero),
2622         (p_zero, p_largest_value, "0x0p+0", Status::OK, Category::Zero),
2623         (p_zero, m_largest_value, "-0x0p+0", Status::OK, Category::Zero),
2624         (p_zero, p_smallest_value, "0x0p+0", Status::OK, Category::Zero),
2625         (p_zero, m_smallest_value, "-0x0p+0", Status::OK, Category::Zero),
2626         (p_zero, p_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
2627         (p_zero, m_smallest_normalized, "-0x0p+0", Status::OK, Category::Zero),
2628         (m_zero, p_inf, "-0x0p+0", Status::OK, Category::Zero),
2629         (m_zero, m_inf, "0x0p+0", Status::OK, Category::Zero),
2630         (m_zero, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2631         (m_zero, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2632         (m_zero, qnan, "nan", Status::OK, Category::NaN),
2633         /*
2634         // See Note 1.
2635         (m_zero, snan, "nan", Status::INVALID_OP, Category::NaN),
2636                 */
2637         (m_zero, p_normal_value, "-0x0p+0", Status::OK, Category::Zero),
2638         (m_zero, m_normal_value, "0x0p+0", Status::OK, Category::Zero),
2639         (m_zero, p_largest_value, "-0x0p+0", Status::OK, Category::Zero),
2640         (m_zero, m_largest_value, "0x0p+0", Status::OK, Category::Zero),
2641         (m_zero, p_smallest_value, "-0x0p+0", Status::OK, Category::Zero),
2642         (m_zero, m_smallest_value, "0x0p+0", Status::OK, Category::Zero),
2643         (m_zero, p_smallest_normalized, "-0x0p+0", Status::OK, Category::Zero),
2644         (m_zero, m_smallest_normalized, "0x0p+0", Status::OK, Category::Zero),
2645         (qnan, p_inf, "nan", Status::OK, Category::NaN),
2646         (qnan, m_inf, "nan", Status::OK, Category::NaN),
2647         (qnan, p_zero, "nan", Status::OK, Category::NaN),
2648         (qnan, m_zero, "nan", Status::OK, Category::NaN),
2649         (qnan, qnan, "nan", Status::OK, Category::NaN),
2650         /*
2651         // See Note 1.
2652         (qnan, snan, "nan", Status::INVALID_OP, Category::NaN),
2653                 */
2654         (qnan, p_normal_value, "nan", Status::OK, Category::NaN),
2655         (qnan, m_normal_value, "nan", Status::OK, Category::NaN),
2656         (qnan, p_largest_value, "nan", Status::OK, Category::NaN),
2657         (qnan, m_largest_value, "nan", Status::OK, Category::NaN),
2658         (qnan, p_smallest_value, "nan", Status::OK, Category::NaN),
2659         (qnan, m_smallest_value, "nan", Status::OK, Category::NaN),
2660         (qnan, p_smallest_normalized, "nan", Status::OK, Category::NaN),
2661         (qnan, m_smallest_normalized, "nan", Status::OK, Category::NaN),
2662         /*
2663         // See Note 1.
2664         (snan, p_inf, "nan", Status::INVALID_OP, Category::NaN),
2665         (snan, m_inf, "nan", Status::INVALID_OP, Category::NaN),
2666         (snan, p_zero, "nan", Status::INVALID_OP, Category::NaN),
2667         (snan, m_zero, "nan", Status::INVALID_OP, Category::NaN),
2668         (snan, qnan, "nan", Status::INVALID_OP, Category::NaN),
2669         (snan, snan, "nan", Status::INVALID_OP, Category::NaN),
2670         (snan, p_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2671         (snan, m_normal_value, "nan", Status::INVALID_OP, Category::NaN),
2672         (snan, p_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2673         (snan, m_largest_value, "nan", Status::INVALID_OP, Category::NaN),
2674         (snan, p_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2675         (snan, m_smallest_value, "nan", Status::INVALID_OP, Category::NaN),
2676         (snan, p_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2677         (snan, m_smallest_normalized, "nan", Status::INVALID_OP, Category::NaN),
2678                 */
2679         (p_normal_value, p_inf, "0x0p+0", Status::OK, Category::Zero),
2680         (p_normal_value, m_inf, "-0x0p+0", Status::OK, Category::Zero),
2681         (p_normal_value, p_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2682         (p_normal_value, m_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2683         (p_normal_value, qnan, "nan", Status::OK, Category::NaN),
2684         /*
2685         // See Note 1.
2686         (p_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2687                 */
2688         (p_normal_value, p_normal_value, "0x1p+0", Status::OK, Category::Normal),
2689         (p_normal_value, m_normal_value, "-0x1p+0", Status::OK, Category::Normal),
2690         (p_normal_value, p_largest_value, "0x1p-128", underflow_status, Category::Normal),
2691         (p_normal_value, m_largest_value, "-0x1p-128", underflow_status, Category::Normal),
2692         (p_normal_value, p_smallest_value, "inf", overflow_status, Category::Infinity),
2693         (p_normal_value, m_smallest_value, "-inf", overflow_status, Category::Infinity),
2694         (p_normal_value, p_smallest_normalized, "0x1p+126", Status::OK, Category::Normal),
2695         (p_normal_value, m_smallest_normalized, "-0x1p+126", Status::OK, Category::Normal),
2696         (m_normal_value, p_inf, "-0x0p+0", Status::OK, Category::Zero),
2697         (m_normal_value, m_inf, "0x0p+0", Status::OK, Category::Zero),
2698         (m_normal_value, p_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2699         (m_normal_value, m_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2700         (m_normal_value, qnan, "nan", Status::OK, Category::NaN),
2701         /*
2702         // See Note 1.
2703         (m_normal_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2704                 */
2705         (m_normal_value, p_normal_value, "-0x1p+0", Status::OK, Category::Normal),
2706         (m_normal_value, m_normal_value, "0x1p+0", Status::OK, Category::Normal),
2707         (m_normal_value, p_largest_value, "-0x1p-128", underflow_status, Category::Normal),
2708         (m_normal_value, m_largest_value, "0x1p-128", underflow_status, Category::Normal),
2709         (m_normal_value, p_smallest_value, "-inf", overflow_status, Category::Infinity),
2710         (m_normal_value, m_smallest_value, "inf", overflow_status, Category::Infinity),
2711         (m_normal_value, p_smallest_normalized, "-0x1p+126", Status::OK, Category::Normal),
2712         (m_normal_value, m_smallest_normalized, "0x1p+126", Status::OK, Category::Normal),
2713         (p_largest_value, p_inf, "0x0p+0", Status::OK, Category::Zero),
2714         (p_largest_value, m_inf, "-0x0p+0", Status::OK, Category::Zero),
2715         (p_largest_value, p_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2716         (p_largest_value, m_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2717         (p_largest_value, qnan, "nan", Status::OK, Category::NaN),
2718         /*
2719         // See Note 1.
2720         (p_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2721                 */
2722         (p_largest_value, p_normal_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2723         (p_largest_value, m_normal_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2724         (p_largest_value, p_largest_value, "0x1p+0", Status::OK, Category::Normal),
2725         (p_largest_value, m_largest_value, "-0x1p+0", Status::OK, Category::Normal),
2726         (p_largest_value, p_smallest_value, "inf", overflow_status, Category::Infinity),
2727         (p_largest_value, m_smallest_value, "-inf", overflow_status, Category::Infinity),
2728         (p_largest_value, p_smallest_normalized, "inf", overflow_status, Category::Infinity),
2729         (p_largest_value, m_smallest_normalized, "-inf", overflow_status, Category::Infinity),
2730         (m_largest_value, p_inf, "-0x0p+0", Status::OK, Category::Zero),
2731         (m_largest_value, m_inf, "0x0p+0", Status::OK, Category::Zero),
2732         (m_largest_value, p_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2733         (m_largest_value, m_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2734         (m_largest_value, qnan, "nan", Status::OK, Category::NaN),
2735         /*
2736         // See Note 1.
2737         (m_largest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2738                 */
2739         (m_largest_value, p_normal_value, "-0x1.fffffep+127", Status::OK, Category::Normal),
2740         (m_largest_value, m_normal_value, "0x1.fffffep+127", Status::OK, Category::Normal),
2741         (m_largest_value, p_largest_value, "-0x1p+0", Status::OK, Category::Normal),
2742         (m_largest_value, m_largest_value, "0x1p+0", Status::OK, Category::Normal),
2743         (m_largest_value, p_smallest_value, "-inf", overflow_status, Category::Infinity),
2744         (m_largest_value, m_smallest_value, "inf", overflow_status, Category::Infinity),
2745         (m_largest_value, p_smallest_normalized, "-inf", overflow_status, Category::Infinity),
2746         (m_largest_value, m_smallest_normalized, "inf", overflow_status, Category::Infinity),
2747         (p_smallest_value, p_inf, "0x0p+0", Status::OK, Category::Zero),
2748         (p_smallest_value, m_inf, "-0x0p+0", Status::OK, Category::Zero),
2749         (p_smallest_value, p_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2750         (p_smallest_value, m_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2751         (p_smallest_value, qnan, "nan", Status::OK, Category::NaN),
2752         /*
2753         // See Note 1.
2754         (p_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2755                 */
2756         (p_smallest_value, p_normal_value, "0x1p-149", Status::OK, Category::Normal),
2757         (p_smallest_value, m_normal_value, "-0x1p-149", Status::OK, Category::Normal),
2758         (p_smallest_value, p_largest_value, "0x0p+0", underflow_status, Category::Zero),
2759         (p_smallest_value, m_largest_value, "-0x0p+0", underflow_status, Category::Zero),
2760         (p_smallest_value, p_smallest_value, "0x1p+0", Status::OK, Category::Normal),
2761         (p_smallest_value, m_smallest_value, "-0x1p+0", Status::OK, Category::Normal),
2762         (p_smallest_value, p_smallest_normalized, "0x1p-23", Status::OK, Category::Normal),
2763         (p_smallest_value, m_smallest_normalized, "-0x1p-23", Status::OK, Category::Normal),
2764         (m_smallest_value, p_inf, "-0x0p+0", Status::OK, Category::Zero),
2765         (m_smallest_value, m_inf, "0x0p+0", Status::OK, Category::Zero),
2766         (m_smallest_value, p_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2767         (m_smallest_value, m_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2768         (m_smallest_value, qnan, "nan", Status::OK, Category::NaN),
2769         /*
2770         // See Note 1.
2771         (m_smallest_value, snan, "nan", Status::INVALID_OP, Category::NaN),
2772                 */
2773         (m_smallest_value, p_normal_value, "-0x1p-149", Status::OK, Category::Normal),
2774         (m_smallest_value, m_normal_value, "0x1p-149", Status::OK, Category::Normal),
2775         (m_smallest_value, p_largest_value, "-0x0p+0", underflow_status, Category::Zero),
2776         (m_smallest_value, m_largest_value, "0x0p+0", underflow_status, Category::Zero),
2777         (m_smallest_value, p_smallest_value, "-0x1p+0", Status::OK, Category::Normal),
2778         (m_smallest_value, m_smallest_value, "0x1p+0", Status::OK, Category::Normal),
2779         (m_smallest_value, p_smallest_normalized, "-0x1p-23", Status::OK, Category::Normal),
2780         (m_smallest_value, m_smallest_normalized, "0x1p-23", Status::OK, Category::Normal),
2781         (p_smallest_normalized, p_inf, "0x0p+0", Status::OK, Category::Zero),
2782         (p_smallest_normalized, m_inf, "-0x0p+0", Status::OK, Category::Zero),
2783         (p_smallest_normalized, p_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2784         (p_smallest_normalized, m_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2785         (p_smallest_normalized, qnan, "nan", Status::OK, Category::NaN),
2786         /*
2787         // See Note 1.
2788         (p_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
2789                 */
2790         (p_smallest_normalized, p_normal_value, "0x1p-126", Status::OK, Category::Normal),
2791         (p_smallest_normalized, m_normal_value, "-0x1p-126", Status::OK, Category::Normal),
2792         (p_smallest_normalized, p_largest_value, "0x0p+0", underflow_status, Category::Zero),
2793         (p_smallest_normalized, m_largest_value, "-0x0p+0", underflow_status, Category::Zero),
2794         (p_smallest_normalized, p_smallest_value, "0x1p+23", Status::OK, Category::Normal),
2795         (p_smallest_normalized, m_smallest_value, "-0x1p+23", Status::OK, Category::Normal),
2796         (p_smallest_normalized, p_smallest_normalized, "0x1p+0", Status::OK, Category::Normal),
2797         (p_smallest_normalized, m_smallest_normalized, "-0x1p+0", Status::OK, Category::Normal),
2798         (m_smallest_normalized, p_inf, "-0x0p+0", Status::OK, Category::Zero),
2799         (m_smallest_normalized, m_inf, "0x0p+0", Status::OK, Category::Zero),
2800         (m_smallest_normalized, p_zero, "-inf", Status::DIV_BY_ZERO, Category::Infinity),
2801         (m_smallest_normalized, m_zero, "inf", Status::DIV_BY_ZERO, Category::Infinity),
2802         (m_smallest_normalized, qnan, "nan", Status::OK, Category::NaN),
2803         /*
2804         // See Note 1.
2805         (m_smallest_normalized, snan, "nan", Status::INVALID_OP, Category::NaN),
2806                 */
2807         (m_smallest_normalized, p_normal_value, "-0x1p-126", Status::OK, Category::Normal),
2808         (m_smallest_normalized, m_normal_value, "0x1p-126", Status::OK, Category::Normal),
2809         (m_smallest_normalized, p_largest_value, "-0x0p+0", underflow_status, Category::Zero),
2810         (m_smallest_normalized, m_largest_value, "0x0p+0", underflow_status, Category::Zero),
2811         (m_smallest_normalized, p_smallest_value, "-0x1p+23", Status::OK, Category::Normal),
2812         (m_smallest_normalized, m_smallest_value, "0x1p+23", Status::OK, Category::Normal),
2813         (m_smallest_normalized, p_smallest_normalized, "-0x1p+0", Status::OK, Category::Normal),
2814         (m_smallest_normalized, m_smallest_normalized, "0x1p+0", Status::OK, Category::Normal),
2815     ];
2816
2817     for (x, y, e_result, e_status, e_category) in special_cases {
2818         let status;
2819         let result = unpack!(status=, x / y);
2820         assert_eq!(status, e_status);
2821         assert_eq!(result.category(), e_category);
2822         assert!(result.bitwise_eq(e_result.parse::<Single>().unwrap()));
2823     }
2824 }
2825
2826 #[test]
2827 fn operator_overloads() {
2828     // This is mostly testing that these operator overloads compile.
2829     let one = "0x1p+0".parse::<Single>().unwrap();
2830     let two = "0x2p+0".parse::<Single>().unwrap();
2831     assert!(two.bitwise_eq((one + one).value));
2832     assert!(one.bitwise_eq((two - one).value));
2833     assert!(two.bitwise_eq((one * two).value));
2834     assert!(one.bitwise_eq((two / two).value));
2835 }
2836
2837 #[test]
2838 fn abs() {
2839     let p_inf = Single::INFINITY;
2840     let m_inf = -Single::INFINITY;
2841     let p_zero = Single::ZERO;
2842     let m_zero = -Single::ZERO;
2843     let p_qnan = Single::NAN;
2844     let m_qnan = -Single::NAN;
2845     let p_snan = Single::snan(None);
2846     let m_snan = -Single::snan(None);
2847     let p_normal_value = "0x1p+0".parse::<Single>().unwrap();
2848     let m_normal_value = "-0x1p+0".parse::<Single>().unwrap();
2849     let p_largest_value = Single::largest();
2850     let m_largest_value = -Single::largest();
2851     let p_smallest_value = Single::SMALLEST;
2852     let m_smallest_value = -Single::SMALLEST;
2853     let p_smallest_normalized = Single::smallest_normalized();
2854     let m_smallest_normalized = -Single::smallest_normalized();
2855
2856     assert!(p_inf.bitwise_eq(p_inf.abs()));
2857     assert!(p_inf.bitwise_eq(m_inf.abs()));
2858     assert!(p_zero.bitwise_eq(p_zero.abs()));
2859     assert!(p_zero.bitwise_eq(m_zero.abs()));
2860     assert!(p_qnan.bitwise_eq(p_qnan.abs()));
2861     assert!(p_qnan.bitwise_eq(m_qnan.abs()));
2862     assert!(p_snan.bitwise_eq(p_snan.abs()));
2863     assert!(p_snan.bitwise_eq(m_snan.abs()));
2864     assert!(p_normal_value.bitwise_eq(p_normal_value.abs()));
2865     assert!(p_normal_value.bitwise_eq(m_normal_value.abs()));
2866     assert!(p_largest_value.bitwise_eq(p_largest_value.abs()));
2867     assert!(p_largest_value.bitwise_eq(m_largest_value.abs()));
2868     assert!(p_smallest_value.bitwise_eq(p_smallest_value.abs()));
2869     assert!(p_smallest_value.bitwise_eq(m_smallest_value.abs()));
2870     assert!(p_smallest_normalized.bitwise_eq(p_smallest_normalized.abs(),));
2871     assert!(p_smallest_normalized.bitwise_eq(m_smallest_normalized.abs(),));
2872 }
2873
2874 #[test]
2875 fn neg() {
2876     let one = "1.0".parse::<Single>().unwrap();
2877     let neg_one = "-1.0".parse::<Single>().unwrap();
2878     let zero = Single::ZERO;
2879     let neg_zero = -Single::ZERO;
2880     let inf = Single::INFINITY;
2881     let neg_inf = -Single::INFINITY;
2882     let qnan = Single::NAN;
2883     let neg_qnan = -Single::NAN;
2884
2885     assert!(neg_one.bitwise_eq(-one));
2886     assert!(one.bitwise_eq(-neg_one));
2887     assert!(neg_zero.bitwise_eq(-zero));
2888     assert!(zero.bitwise_eq(-neg_zero));
2889     assert!(neg_inf.bitwise_eq(-inf));
2890     assert!(inf.bitwise_eq(-neg_inf));
2891     assert!(neg_inf.bitwise_eq(-inf));
2892     assert!(inf.bitwise_eq(-neg_inf));
2893     assert!(neg_qnan.bitwise_eq(-qnan));
2894     assert!(qnan.bitwise_eq(-neg_qnan));
2895 }
2896
2897 #[test]
2898 fn ilogb() {
2899     assert_eq!(-1074, Double::SMALLEST.ilogb());
2900     assert_eq!(-1074, (-Double::SMALLEST).ilogb());
2901     assert_eq!(-1023, "0x1.ffffffffffffep-1024".parse::<Double>().unwrap().ilogb());
2902     assert_eq!(-1023, "0x1.ffffffffffffep-1023".parse::<Double>().unwrap().ilogb());
2903     assert_eq!(-1023, "-0x1.ffffffffffffep-1023".parse::<Double>().unwrap().ilogb());
2904     assert_eq!(-51, "0x1p-51".parse::<Double>().unwrap().ilogb());
2905     assert_eq!(-1023, "0x1.c60f120d9f87cp-1023".parse::<Double>().unwrap().ilogb());
2906     assert_eq!(-2, "0x0.ffffp-1".parse::<Double>().unwrap().ilogb());
2907     assert_eq!(-1023, "0x1.fffep-1023".parse::<Double>().unwrap().ilogb());
2908     assert_eq!(1023, Double::largest().ilogb());
2909     assert_eq!(1023, (-Double::largest()).ilogb());
2910
2911     assert_eq!(0, "0x1p+0".parse::<Single>().unwrap().ilogb());
2912     assert_eq!(0, "-0x1p+0".parse::<Single>().unwrap().ilogb());
2913     assert_eq!(42, "0x1p+42".parse::<Single>().unwrap().ilogb());
2914     assert_eq!(-42, "0x1p-42".parse::<Single>().unwrap().ilogb());
2915
2916     assert_eq!(IEK_INF, Single::INFINITY.ilogb());
2917     assert_eq!(IEK_INF, (-Single::INFINITY).ilogb());
2918     assert_eq!(IEK_ZERO, Single::ZERO.ilogb());
2919     assert_eq!(IEK_ZERO, (-Single::ZERO).ilogb());
2920     assert_eq!(IEK_NAN, Single::NAN.ilogb());
2921     assert_eq!(IEK_NAN, Single::snan(None).ilogb());
2922
2923     assert_eq!(127, Single::largest().ilogb());
2924     assert_eq!(127, (-Single::largest()).ilogb());
2925
2926     assert_eq!(-149, Single::SMALLEST.ilogb());
2927     assert_eq!(-149, (-Single::SMALLEST).ilogb());
2928     assert_eq!(-126, Single::smallest_normalized().ilogb());
2929     assert_eq!(-126, (-Single::smallest_normalized()).ilogb());
2930 }
2931
2932 #[test]
2933 fn scalbn() {
2934     assert!(
2935         "0x1p+0"
2936             .parse::<Single>()
2937             .unwrap()
2938             .bitwise_eq("0x1p+0".parse::<Single>().unwrap().scalbn(0),)
2939     );
2940     assert!(
2941         "0x1p+42"
2942             .parse::<Single>()
2943             .unwrap()
2944             .bitwise_eq("0x1p+0".parse::<Single>().unwrap().scalbn(42),)
2945     );
2946     assert!(
2947         "0x1p-42"
2948             .parse::<Single>()
2949             .unwrap()
2950             .bitwise_eq("0x1p+0".parse::<Single>().unwrap().scalbn(-42),)
2951     );
2952
2953     let p_inf = Single::INFINITY;
2954     let m_inf = -Single::INFINITY;
2955     let p_zero = Single::ZERO;
2956     let m_zero = -Single::ZERO;
2957     let p_qnan = Single::NAN;
2958     let m_qnan = -Single::NAN;
2959     let snan = Single::snan(None);
2960
2961     assert!(p_inf.bitwise_eq(p_inf.scalbn(0)));
2962     assert!(m_inf.bitwise_eq(m_inf.scalbn(0)));
2963     assert!(p_zero.bitwise_eq(p_zero.scalbn(0)));
2964     assert!(m_zero.bitwise_eq(m_zero.scalbn(0)));
2965     assert!(p_qnan.bitwise_eq(p_qnan.scalbn(0)));
2966     assert!(m_qnan.bitwise_eq(m_qnan.scalbn(0)));
2967     assert!(!snan.scalbn(0).is_signaling());
2968
2969     let scalbn_snan = snan.scalbn(1);
2970     assert!(scalbn_snan.is_nan() && !scalbn_snan.is_signaling());
2971
2972     // Make sure highest bit of payload is preserved.
2973     let payload = (1 << 50) | (1 << 49) | (1234 << 32) | 1;
2974
2975     let snan_with_payload = Double::snan(Some(payload));
2976     let quiet_payload = snan_with_payload.scalbn(1);
2977     assert!(quiet_payload.is_nan() && !quiet_payload.is_signaling());
2978     assert_eq!(payload, quiet_payload.to_bits() & ((1 << 51) - 1));
2979
2980     assert!(p_inf.bitwise_eq("0x1p+0".parse::<Single>().unwrap().scalbn(128),));
2981     assert!(m_inf.bitwise_eq("-0x1p+0".parse::<Single>().unwrap().scalbn(128),));
2982     assert!(p_inf.bitwise_eq("0x1p+127".parse::<Single>().unwrap().scalbn(1),));
2983     assert!(p_zero.bitwise_eq("0x1p-127".parse::<Single>().unwrap().scalbn(-127),));
2984     assert!(m_zero.bitwise_eq("-0x1p-127".parse::<Single>().unwrap().scalbn(-127),));
2985     assert!(
2986         "-0x1p-149"
2987             .parse::<Single>()
2988             .unwrap()
2989             .bitwise_eq("-0x1p-127".parse::<Single>().unwrap().scalbn(-22),)
2990     );
2991     assert!(p_zero.bitwise_eq("0x1p-126".parse::<Single>().unwrap().scalbn(-24),));
2992
2993     let smallest_f64 = Double::SMALLEST;
2994     let neg_smallest_f64 = -Double::SMALLEST;
2995
2996     let largest_f64 = Double::largest();
2997     let neg_largest_f64 = -Double::largest();
2998
2999     let largest_denormal_f64 = "0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
3000     let neg_largest_denormal_f64 = "-0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
3001
3002     assert!(smallest_f64.bitwise_eq("0x1p-1074".parse::<Double>().unwrap().scalbn(0),));
3003     assert!(neg_smallest_f64.bitwise_eq("-0x1p-1074".parse::<Double>().unwrap().scalbn(0),));
3004
3005     assert!("0x1p+1023".parse::<Double>().unwrap().bitwise_eq(smallest_f64.scalbn(2097,),));
3006
3007     assert!(smallest_f64.scalbn(-2097).is_pos_zero());
3008     assert!(smallest_f64.scalbn(-2098).is_pos_zero());
3009     assert!(smallest_f64.scalbn(-2099).is_pos_zero());
3010     assert!("0x1p+1022".parse::<Double>().unwrap().bitwise_eq(smallest_f64.scalbn(2096,),));
3011     assert!("0x1p+1023".parse::<Double>().unwrap().bitwise_eq(smallest_f64.scalbn(2097,),));
3012     assert!(smallest_f64.scalbn(2098).is_infinite());
3013     assert!(smallest_f64.scalbn(2099).is_infinite());
3014
3015     // Test for integer overflows when adding to exponent.
3016     assert!(smallest_f64.scalbn(-ExpInt::MAX).is_pos_zero());
3017     assert!(largest_f64.scalbn(ExpInt::MAX).is_infinite());
3018
3019     assert!(largest_denormal_f64.bitwise_eq(largest_denormal_f64.scalbn(0),));
3020     assert!(neg_largest_denormal_f64.bitwise_eq(neg_largest_denormal_f64.scalbn(0),));
3021
3022     assert!(
3023         "0x1.ffffffffffffep-1022"
3024             .parse::<Double>()
3025             .unwrap()
3026             .bitwise_eq(largest_denormal_f64.scalbn(1))
3027     );
3028     assert!(
3029         "-0x1.ffffffffffffep-1021"
3030             .parse::<Double>()
3031             .unwrap()
3032             .bitwise_eq(neg_largest_denormal_f64.scalbn(2))
3033     );
3034
3035     assert!(
3036         "0x1.ffffffffffffep+1"
3037             .parse::<Double>()
3038             .unwrap()
3039             .bitwise_eq(largest_denormal_f64.scalbn(1024))
3040     );
3041     assert!(largest_denormal_f64.scalbn(-1023).is_pos_zero());
3042     assert!(largest_denormal_f64.scalbn(-1024).is_pos_zero());
3043     assert!(largest_denormal_f64.scalbn(-2048).is_pos_zero());
3044     assert!(largest_denormal_f64.scalbn(2047).is_infinite());
3045     assert!(largest_denormal_f64.scalbn(2098).is_infinite());
3046     assert!(largest_denormal_f64.scalbn(2099).is_infinite());
3047
3048     assert!(
3049         "0x1.ffffffffffffep-2"
3050             .parse::<Double>()
3051             .unwrap()
3052             .bitwise_eq(largest_denormal_f64.scalbn(1021))
3053     );
3054     assert!(
3055         "0x1.ffffffffffffep-1"
3056             .parse::<Double>()
3057             .unwrap()
3058             .bitwise_eq(largest_denormal_f64.scalbn(1022))
3059     );
3060     assert!(
3061         "0x1.ffffffffffffep+0"
3062             .parse::<Double>()
3063             .unwrap()
3064             .bitwise_eq(largest_denormal_f64.scalbn(1023))
3065     );
3066     assert!(
3067         "0x1.ffffffffffffep+1023"
3068             .parse::<Double>()
3069             .unwrap()
3070             .bitwise_eq(largest_denormal_f64.scalbn(2046))
3071     );
3072     assert!("0x1p+974".parse::<Double>().unwrap().bitwise_eq(smallest_f64.scalbn(2048,),));
3073
3074     let random_denormal_f64 = "0x1.c60f120d9f87cp+51".parse::<Double>().unwrap();
3075     assert!(
3076         "0x1.c60f120d9f87cp-972"
3077             .parse::<Double>()
3078             .unwrap()
3079             .bitwise_eq(random_denormal_f64.scalbn(-1023))
3080     );
3081     assert!(
3082         "0x1.c60f120d9f87cp-1"
3083             .parse::<Double>()
3084             .unwrap()
3085             .bitwise_eq(random_denormal_f64.scalbn(-52))
3086     );
3087     assert!(
3088         "0x1.c60f120d9f87cp-2"
3089             .parse::<Double>()
3090             .unwrap()
3091             .bitwise_eq(random_denormal_f64.scalbn(-53))
3092     );
3093     assert!(
3094         "0x1.c60f120d9f87cp+0"
3095             .parse::<Double>()
3096             .unwrap()
3097             .bitwise_eq(random_denormal_f64.scalbn(-51))
3098     );
3099
3100     assert!(random_denormal_f64.scalbn(-2097).is_pos_zero());
3101     assert!(random_denormal_f64.scalbn(-2090).is_pos_zero());
3102
3103     assert!("-0x1p-1073".parse::<Double>().unwrap().bitwise_eq(neg_largest_f64.scalbn(-2097),));
3104
3105     assert!("-0x1p-1024".parse::<Double>().unwrap().bitwise_eq(neg_largest_f64.scalbn(-2048),));
3106
3107     assert!("0x1p-1073".parse::<Double>().unwrap().bitwise_eq(largest_f64.scalbn(-2097,),));
3108
3109     assert!("0x1p-1074".parse::<Double>().unwrap().bitwise_eq(largest_f64.scalbn(-2098,),));
3110     assert!("-0x1p-1074".parse::<Double>().unwrap().bitwise_eq(neg_largest_f64.scalbn(-2098),));
3111     assert!(neg_largest_f64.scalbn(-2099).is_neg_zero());
3112     assert!(largest_f64.scalbn(1).is_infinite());
3113
3114     assert!(
3115         "0x1p+0"
3116             .parse::<Double>()
3117             .unwrap()
3118             .bitwise_eq("0x1p+52".parse::<Double>().unwrap().scalbn(-52),)
3119     );
3120
3121     assert!(
3122         "0x1p-103"
3123             .parse::<Double>()
3124             .unwrap()
3125             .bitwise_eq("0x1p-51".parse::<Double>().unwrap().scalbn(-52),)
3126     );
3127 }
3128
3129 #[test]
3130 fn frexp() {
3131     let p_zero = Double::ZERO;
3132     let m_zero = -Double::ZERO;
3133     let one = Double::from_f64(1.0);
3134     let m_one = Double::from_f64(-1.0);
3135
3136     let largest_denormal = "0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
3137     let neg_largest_denormal = "-0x1.ffffffffffffep-1023".parse::<Double>().unwrap();
3138
3139     let smallest = Double::SMALLEST;
3140     let neg_smallest = -Double::SMALLEST;
3141
3142     let largest = Double::largest();
3143     let neg_largest = -Double::largest();
3144
3145     let p_inf = Double::INFINITY;
3146     let m_inf = -Double::INFINITY;
3147
3148     let p_qnan = Double::NAN;
3149     let m_qnan = -Double::NAN;
3150     let snan = Double::snan(None);
3151
3152     // Make sure highest bit of payload is preserved.
3153     let payload = (1 << 50) | (1 << 49) | (1234 << 32) | 1;
3154
3155     let snan_with_payload = Double::snan(Some(payload));
3156
3157     let mut exp = 0;
3158
3159     let frac = p_zero.frexp(&mut exp);
3160     assert_eq!(0, exp);
3161     assert!(frac.is_pos_zero());
3162
3163     let frac = m_zero.frexp(&mut exp);
3164     assert_eq!(0, exp);
3165     assert!(frac.is_neg_zero());
3166
3167     let frac = one.frexp(&mut exp);
3168     assert_eq!(1, exp);
3169     assert!("0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
3170
3171     let frac = m_one.frexp(&mut exp);
3172     assert_eq!(1, exp);
3173     assert!("-0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
3174
3175     let frac = largest_denormal.frexp(&mut exp);
3176     assert_eq!(-1022, exp);
3177     assert!("0x1.ffffffffffffep-1".parse::<Double>().unwrap().bitwise_eq(frac));
3178
3179     let frac = neg_largest_denormal.frexp(&mut exp);
3180     assert_eq!(-1022, exp);
3181     assert!("-0x1.ffffffffffffep-1".parse::<Double>().unwrap().bitwise_eq(frac));
3182
3183     let frac = smallest.frexp(&mut exp);
3184     assert_eq!(-1073, exp);
3185     assert!("0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
3186
3187     let frac = neg_smallest.frexp(&mut exp);
3188     assert_eq!(-1073, exp);
3189     assert!("-0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
3190
3191     let frac = largest.frexp(&mut exp);
3192     assert_eq!(1024, exp);
3193     assert!("0x1.fffffffffffffp-1".parse::<Double>().unwrap().bitwise_eq(frac));
3194
3195     let frac = neg_largest.frexp(&mut exp);
3196     assert_eq!(1024, exp);
3197     assert!("-0x1.fffffffffffffp-1".parse::<Double>().unwrap().bitwise_eq(frac));
3198
3199     let frac = p_inf.frexp(&mut exp);
3200     assert_eq!(IEK_INF, exp);
3201     assert!(frac.is_infinite() && !frac.is_negative());
3202
3203     let frac = m_inf.frexp(&mut exp);
3204     assert_eq!(IEK_INF, exp);
3205     assert!(frac.is_infinite() && frac.is_negative());
3206
3207     let frac = p_qnan.frexp(&mut exp);
3208     assert_eq!(IEK_NAN, exp);
3209     assert!(frac.is_nan());
3210
3211     let frac = m_qnan.frexp(&mut exp);
3212     assert_eq!(IEK_NAN, exp);
3213     assert!(frac.is_nan());
3214
3215     let frac = snan.frexp(&mut exp);
3216     assert_eq!(IEK_NAN, exp);
3217     assert!(frac.is_nan() && !frac.is_signaling());
3218
3219     let frac = snan_with_payload.frexp(&mut exp);
3220     assert_eq!(IEK_NAN, exp);
3221     assert!(frac.is_nan() && !frac.is_signaling());
3222     assert_eq!(payload, frac.to_bits() & ((1 << 51) - 1));
3223
3224     let frac = "0x0.ffffp-1".parse::<Double>().unwrap().frexp(&mut exp);
3225     assert_eq!(-1, exp);
3226     assert!("0x1.fffep-1".parse::<Double>().unwrap().bitwise_eq(frac));
3227
3228     let frac = "0x1p-51".parse::<Double>().unwrap().frexp(&mut exp);
3229     assert_eq!(-50, exp);
3230     assert!("0x1p-1".parse::<Double>().unwrap().bitwise_eq(frac));
3231
3232     let frac = "0x1.c60f120d9f87cp+51".parse::<Double>().unwrap().frexp(&mut exp);
3233     assert_eq!(52, exp);
3234     assert!("0x1.c60f120d9f87cp-1".parse::<Double>().unwrap().bitwise_eq(frac));
3235 }
3236
3237 #[test]
3238 fn modulo() {
3239     let mut status;
3240     {
3241         let f1 = "1.5".parse::<Double>().unwrap();
3242         let f2 = "1.0".parse::<Double>().unwrap();
3243         let expected = "0.5".parse::<Double>().unwrap();
3244         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3245         assert_eq!(status, Status::OK);
3246     }
3247     {
3248         let f1 = "0.5".parse::<Double>().unwrap();
3249         let f2 = "1.0".parse::<Double>().unwrap();
3250         let expected = "0.5".parse::<Double>().unwrap();
3251         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3252         assert_eq!(status, Status::OK);
3253     }
3254     {
3255         let f1 = "0x1.3333333333333p-2".parse::<Double>().unwrap(); // 0.3
3256         let f2 = "0x1.47ae147ae147bp-7".parse::<Double>().unwrap(); // 0.01
3257         // 0.009999999999999983
3258         let expected = "0x1.47ae147ae1471p-7".parse::<Double>().unwrap();
3259         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3260         assert_eq!(status, Status::OK);
3261     }
3262     {
3263         let f1 = "0x1p64".parse::<Double>().unwrap(); // 1.8446744073709552e19
3264         let f2 = "1.5".parse::<Double>().unwrap();
3265         let expected = "1.0".parse::<Double>().unwrap();
3266         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3267         assert_eq!(status, Status::OK);
3268     }
3269     {
3270         let f1 = "0x1p1000".parse::<Double>().unwrap();
3271         let f2 = "0x1p-1000".parse::<Double>().unwrap();
3272         let expected = "0.0".parse::<Double>().unwrap();
3273         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3274         assert_eq!(status, Status::OK);
3275     }
3276     {
3277         let f1 = "0.0".parse::<Double>().unwrap();
3278         let f2 = "1.0".parse::<Double>().unwrap();
3279         let expected = "0.0".parse::<Double>().unwrap();
3280         assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3281         assert_eq!(status, Status::OK);
3282     }
3283     {
3284         let f1 = "1.0".parse::<Double>().unwrap();
3285         let f2 = "0.0".parse::<Double>().unwrap();
3286         assert!(unpack!(status=, f1 % f2).is_nan());
3287         assert_eq!(status, Status::INVALID_OP);
3288     }
3289     {
3290         let f1 = "0.0".parse::<Double>().unwrap();
3291         let f2 = "0.0".parse::<Double>().unwrap();
3292         assert!(unpack!(status=, f1 % f2).is_nan());
3293         assert_eq!(status, Status::INVALID_OP);
3294     }
3295     {
3296         let f1 = Double::INFINITY;
3297         let f2 = "1.0".parse::<Double>().unwrap();
3298         assert!(unpack!(status=, f1 % f2).is_nan());
3299         assert_eq!(status, Status::INVALID_OP);
3300     }
3301 }