1
1
macro_rules! float_tests {
2
- { $vector: ident, $scalar: ident } => {
2
+ { $vector: ident, $scalar: ident, $int_vector : ident , $int_scalar : ident } => {
3
3
#[ cfg( test) ]
4
4
mod $vector {
5
5
use super :: * ;
@@ -24,6 +24,18 @@ macro_rules! float_tests {
24
24
slice. chunks_exact( lanes) . map( from_slice)
25
25
}
26
26
27
+ fn from_slice_int( slice: & [ $int_scalar] ) -> core_simd:: $int_vector {
28
+ let mut value = core_simd:: $int_vector:: default ( ) ;
29
+ let value_slice: & mut [ _] = value. as_mut( ) ;
30
+ value_slice. copy_from_slice( & slice[ 0 ..value_slice. len( ) ] ) ;
31
+ value
32
+ }
33
+
34
+ fn slice_chunks_int( slice: & [ $int_scalar] ) -> impl Iterator <Item = core_simd:: $int_vector> + ' _ {
35
+ let lanes = core:: mem:: size_of:: <core_simd:: $int_vector>( ) / core:: mem:: size_of:: <$int_scalar>( ) ;
36
+ slice. chunks_exact( lanes) . map( from_slice_int)
37
+ }
38
+
27
39
const A : [ $scalar; 16 ] = [ 0. , 1. , 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9. , 10. , 11. , 12. , 13. , 14. , 15. ] ;
28
40
const B : [ $scalar; 16 ] = [ 16. , 17. , 18. , 19. , 20. , 21. , 22. , 23. , 24. , 25. , 26. , 27. , 28. , 29. , 30. , 31. ] ;
29
41
const C : [ $scalar; 16 ] = [
@@ -322,6 +334,121 @@ macro_rules! float_tests {
322
334
assert_biteq!( v. abs( ) , expected) ;
323
335
}
324
336
}
337
+
338
+ #[ test]
339
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
340
+ fn ceil_odd_floats( ) {
341
+ for v in slice_chunks( & C ) {
342
+ let expected = apply_unary_lanewise( v, <$scalar>:: ceil) ;
343
+ assert_biteq!( v. ceil( ) , expected) ;
344
+ }
345
+ }
346
+
347
+ #[ test]
348
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
349
+ fn floor_odd_floats( ) {
350
+ for v in slice_chunks( & C ) {
351
+ let expected = apply_unary_lanewise( v, <$scalar>:: floor) ;
352
+ assert_biteq!( v. floor( ) , expected) ;
353
+ }
354
+ }
355
+
356
+ #[ test]
357
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
358
+ fn round_odd_floats( ) {
359
+ for v in slice_chunks( & C ) {
360
+ let expected = apply_unary_lanewise( v, <$scalar>:: round) ;
361
+ assert_biteq!( v. round( ) , expected) ;
362
+ }
363
+ }
364
+
365
+ #[ test]
366
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
367
+ fn round_mode( ) {
368
+ assert_biteq!( core_simd:: $vector:: splat( 1.5 ) . round( ) , core_simd:: $vector:: splat( 2.0 ) ) ;
369
+ assert_biteq!( core_simd:: $vector:: splat( 2.5 ) . round( ) , core_simd:: $vector:: splat( 3.0 ) ) ;
370
+ assert_biteq!( core_simd:: $vector:: splat( -1.5 ) . round( ) , core_simd:: $vector:: splat( -2.0 ) ) ;
371
+ assert_biteq!( core_simd:: $vector:: splat( -2.5 ) . round( ) , core_simd:: $vector:: splat( -3.0 ) ) ;
372
+ }
373
+
374
+ #[ test]
375
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
376
+ fn trunc_odd_floats( ) {
377
+ for v in slice_chunks( & C ) {
378
+ let expected = apply_unary_lanewise( v, <$scalar>:: trunc) ;
379
+ assert_biteq!( v. trunc( ) , expected) ;
380
+ }
381
+ }
382
+
383
+ #[ test]
384
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
385
+ fn fract_odd_floats( ) {
386
+ for v in slice_chunks( & C ) {
387
+ let expected = apply_unary_lanewise( v, <$scalar>:: fract) ;
388
+ assert_biteq!( v. fract( ) , expected) ;
389
+ }
390
+ }
391
+
392
+ #[ test]
393
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
394
+ fn to_int_unchecked( ) {
395
+ // The maximum integer that can be represented by the equivalently sized float has
396
+ // all of the mantissa digits set to 1, pushed up to the MSB.
397
+ const ALL_MANTISSA_BITS : $int_scalar = ( ( 1 << <$scalar>:: MANTISSA_DIGITS ) - 1 ) ;
398
+ const MAX_REPRESENTABLE_VALUE : $int_scalar =
399
+ ALL_MANTISSA_BITS << ( core:: mem:: size_of:: <$scalar>( ) * 8 - <$scalar>:: MANTISSA_DIGITS as usize - 1 ) ;
400
+ const VALUES : [ $scalar; 16 ] = [
401
+ -0.0 ,
402
+ 0.0 ,
403
+ -1.0 ,
404
+ 1.0 ,
405
+ ALL_MANTISSA_BITS as $scalar,
406
+ -ALL_MANTISSA_BITS as $scalar,
407
+ MAX_REPRESENTABLE_VALUE as $scalar,
408
+ -MAX_REPRESENTABLE_VALUE as $scalar,
409
+ ( MAX_REPRESENTABLE_VALUE / 2 ) as $scalar,
410
+ ( -MAX_REPRESENTABLE_VALUE / 2 ) as $scalar,
411
+ <$scalar>:: MIN_POSITIVE ,
412
+ -<$scalar>:: MIN_POSITIVE ,
413
+ <$scalar>:: EPSILON ,
414
+ -<$scalar>:: EPSILON ,
415
+ 100.0 / 3.0 ,
416
+ -100.0 / 3.0 ,
417
+ ] ;
418
+
419
+ for v in slice_chunks( & VALUES ) {
420
+ let expected = apply_unary_lanewise( v, |x| unsafe { x. to_int_unchecked( ) } ) ;
421
+ assert_biteq!( unsafe { v. to_int_unchecked( ) } , expected) ;
422
+ }
423
+ }
424
+
425
+ #[ test]
426
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
427
+ fn round_from_int( ) {
428
+ const VALUES : [ $int_scalar; 16 ] = [
429
+ 0 ,
430
+ 0 ,
431
+ 1 ,
432
+ -1 ,
433
+ 100 ,
434
+ -100 ,
435
+ 200 ,
436
+ -200 ,
437
+ 413 ,
438
+ -413 ,
439
+ 1017 ,
440
+ -1017 ,
441
+ 1234567 ,
442
+ -1234567 ,
443
+ <$int_scalar>:: MAX ,
444
+ <$int_scalar>:: MIN ,
445
+ ] ;
446
+
447
+ for v in slice_chunks_int( & VALUES ) {
448
+ let expected = apply_unary_lanewise( v, |x| x as $scalar) ;
449
+ assert_biteq!( core_simd:: $vector:: round_from_int( v) , expected) ;
450
+ }
451
+ }
325
452
}
326
453
}
327
454
}
0 commit comments