@@ -311,6 +311,96 @@ fn test_f64() {
311
311
) ;
312
312
}
313
313
314
+ #[ allow( unused) ]
315
+ fn test_casts ( ) {
316
+ let all1_payload_32 = u32_ones ( 22 ) ;
317
+ let all1_payload_64 = u64_ones ( 51 ) ;
318
+ let left1_payload_64 = ( all1_payload_32 as u64 ) << ( 51 - 22 ) ;
319
+
320
+ // 64-to-32
321
+ check_all_outcomes (
322
+ HashSet :: from_iter ( [ F32 :: nan ( Pos , Quiet , 0 ) , F32 :: nan ( Neg , Quiet , 0 ) ] ) ,
323
+ || F32 :: from ( F64 :: nan ( Pos , Quiet , 0 ) . as_f64 ( ) as f32 ) ,
324
+ ) ;
325
+ // The preferred payload is always a possibility.
326
+ check_all_outcomes (
327
+ HashSet :: from_iter ( [
328
+ F32 :: nan ( Pos , Quiet , 0 ) ,
329
+ F32 :: nan ( Neg , Quiet , 0 ) ,
330
+ F32 :: nan ( Pos , Quiet , all1_payload_32) ,
331
+ F32 :: nan ( Neg , Quiet , all1_payload_32) ,
332
+ ] ) ,
333
+ || F32 :: from ( F64 :: nan ( Pos , Quiet , all1_payload_64) . as_f64 ( ) as f32 ) ,
334
+ ) ;
335
+ // If the input is signaling, then the output *may* also be signaling.
336
+ check_all_outcomes (
337
+ HashSet :: from_iter ( [
338
+ F32 :: nan ( Pos , Quiet , 0 ) ,
339
+ F32 :: nan ( Neg , Quiet , 0 ) ,
340
+ F32 :: nan ( Pos , Quiet , all1_payload_32) ,
341
+ F32 :: nan ( Neg , Quiet , all1_payload_32) ,
342
+ F32 :: nan ( Pos , Signaling , all1_payload_32) ,
343
+ F32 :: nan ( Neg , Signaling , all1_payload_32) ,
344
+ ] ) ,
345
+ || F32 :: from ( F64 :: nan ( Pos , Signaling , all1_payload_64) . as_f64 ( ) as f32 ) ,
346
+ ) ;
347
+ // Check that the low bits are gone (not the high bits).
348
+ check_all_outcomes (
349
+ HashSet :: from_iter ( [
350
+ F32 :: nan ( Pos , Quiet , 0 ) ,
351
+ F32 :: nan ( Neg , Quiet , 0 ) ,
352
+ ] ) ,
353
+ || F32 :: from ( F64 :: nan ( Pos , Quiet , 1 ) . as_f64 ( ) as f32 ) ,
354
+ ) ;
355
+ check_all_outcomes (
356
+ HashSet :: from_iter ( [
357
+ F32 :: nan ( Pos , Quiet , 0 ) ,
358
+ F32 :: nan ( Neg , Quiet , 0 ) ,
359
+ F32 :: nan ( Pos , Quiet , 1 ) ,
360
+ F32 :: nan ( Neg , Quiet , 1 ) ,
361
+ ] ) ,
362
+ || F32 :: from ( F64 :: nan ( Pos , Quiet , 1 << ( 51 -22 ) ) . as_f64 ( ) as f32 ) ,
363
+ ) ;
364
+ check_all_outcomes (
365
+ HashSet :: from_iter ( [
366
+ F32 :: nan ( Pos , Quiet , 0 ) ,
367
+ F32 :: nan ( Neg , Quiet , 0 ) ,
368
+ // The `1` payload becomes `0`, and the `0` payload cannot be signaling,
369
+ // so these are the only options.
370
+ ] ) ,
371
+ || F32 :: from ( F64 :: nan ( Pos , Signaling , 1 ) . as_f64 ( ) as f32 ) ,
372
+ ) ;
373
+
374
+ // 32-to-64
375
+ check_all_outcomes (
376
+ HashSet :: from_iter ( [ F64 :: nan ( Pos , Quiet , 0 ) , F64 :: nan ( Neg , Quiet , 0 ) ] ) ,
377
+ || F64 :: from ( F32 :: nan ( Pos , Quiet , 0 ) . as_f32 ( ) as f64 ) ,
378
+ ) ;
379
+ // The preferred payload is always a possibility.
380
+ // Also checks that 0s are added on the right.
381
+ check_all_outcomes (
382
+ HashSet :: from_iter ( [
383
+ F64 :: nan ( Pos , Quiet , 0 ) ,
384
+ F64 :: nan ( Neg , Quiet , 0 ) ,
385
+ F64 :: nan ( Pos , Quiet , left1_payload_64) ,
386
+ F64 :: nan ( Neg , Quiet , left1_payload_64) ,
387
+ ] ) ,
388
+ || F64 :: from ( F32 :: nan ( Pos , Quiet , all1_payload_32) . as_f32 ( ) as f64 ) ,
389
+ ) ;
390
+ // If the input is signaling, then the output *may* also be signaling.
391
+ check_all_outcomes (
392
+ HashSet :: from_iter ( [
393
+ F64 :: nan ( Pos , Quiet , 0 ) ,
394
+ F64 :: nan ( Neg , Quiet , 0 ) ,
395
+ F64 :: nan ( Pos , Quiet , left1_payload_64) ,
396
+ F64 :: nan ( Neg , Quiet , left1_payload_64) ,
397
+ F64 :: nan ( Pos , Signaling , left1_payload_64) ,
398
+ F64 :: nan ( Neg , Signaling , left1_payload_64) ,
399
+ ] ) ,
400
+ || F64 :: from ( F32 :: nan ( Pos , Signaling , all1_payload_32) . as_f32 ( ) as f64 ) ,
401
+ ) ;
402
+ }
403
+
314
404
fn main ( ) {
315
405
// Check our constants against std, just to be sure.
316
406
// We add 1 since our numbers are the number of bits stored
@@ -321,4 +411,5 @@ fn main() {
321
411
322
412
test_f32 ( ) ;
323
413
test_f64 ( ) ;
414
+ test_casts ( ) ;
324
415
}
0 commit comments