@@ -294,6 +294,14 @@ pub trait SqrtHelper: Float {
294
294
const FINAL_ROUNDS : u32 ;
295
295
}
296
296
297
+ #[ cfg( f16_enabled) ]
298
+ impl SqrtHelper for f16 {
299
+ type ISet1 = u16 ; // unused
300
+ type ISet2 = u16 ; // unused
301
+
302
+ const FINAL_ROUNDS : u32 = 2 ;
303
+ }
304
+
297
305
impl SqrtHelper for f32 {
298
306
type ISet1 = u32 ; // unused
299
307
type ISet2 = u32 ; // unused
@@ -309,6 +317,16 @@ impl SqrtHelper for f64 {
309
317
const FINAL_ROUNDS : u32 = 2 ;
310
318
}
311
319
320
+ #[ cfg( f128_enabled) ]
321
+ impl SqrtHelper for f128 {
322
+ type ISet1 = u32 ;
323
+ type ISet2 = u64 ;
324
+
325
+ const SET1_ROUNDS : u32 = 1 ;
326
+ const SET2_ROUNDS : u32 = 2 ;
327
+ const FINAL_ROUNDS : u32 = 2 ;
328
+ }
329
+
312
330
/// A U0.16 representation of `1/sqrt(x)`.
313
331
///
314
332
// / The index is a 7-bit number consisting of a single exponent bit and 6 bits of significand.
@@ -355,6 +373,42 @@ mod tests {
355
373
}
356
374
}
357
375
376
+ #[ test]
377
+ #[ cfg( f16_enabled) ]
378
+ fn sanity_check_f16 ( ) {
379
+ assert_biteq ! ( sqrt( 100.0f16 ) , 10.0 ) ;
380
+ assert_biteq ! ( sqrt( 4.0f16 ) , 2.0 ) ;
381
+ }
382
+
383
+ #[ test]
384
+ #[ cfg( f16_enabled) ]
385
+ fn spec_tests_f16 ( ) {
386
+ spec_test :: < f16 > ( ) ;
387
+ }
388
+
389
+ #[ test]
390
+ #[ cfg( f16_enabled) ]
391
+ #[ allow( clippy:: approx_constant) ]
392
+ fn conformance_tests_f16 ( ) {
393
+ let cases = [
394
+ ( f16:: PI , 0x3f17_u16 ) ,
395
+ // 10_000.0, using a hex literal for MSRV hack (Rust < 1.67 checks literal widths as
396
+ // part of the AST, so the `cfg` is irrelevant here).
397
+ ( f16:: from_bits ( 0x70e2 ) , 0x5640_u16 ) ,
398
+ ( f16:: from_bits ( 0x0000000f ) , 0x13bf_u16 ) ,
399
+ ( f16:: INFINITY , f16:: INFINITY . to_bits ( ) ) ,
400
+ ] ;
401
+
402
+ for ( input, output) in cases {
403
+ assert_biteq ! (
404
+ sqrt( input) ,
405
+ f16:: from_bits( output) ,
406
+ "input: {input:?} ({:#018x})" ,
407
+ input. to_bits( )
408
+ ) ;
409
+ }
410
+ }
411
+
358
412
#[ test]
359
413
fn sanity_check_f32 ( ) {
360
414
assert_biteq ! ( sqrt( 100.0f32 ) , 10.0 ) ;
@@ -416,4 +470,42 @@ mod tests {
416
470
) ;
417
471
}
418
472
}
473
+
474
+ #[ test]
475
+ #[ cfg( f128_enabled) ]
476
+ fn sanity_check_f128 ( ) {
477
+ assert_biteq ! ( sqrt( 100.0f128 ) , 10.0 ) ;
478
+ assert_biteq ! ( sqrt( 4.0f128 ) , 2.0 ) ;
479
+ }
480
+
481
+ #[ test]
482
+ #[ cfg( f128_enabled) ]
483
+ fn spec_tests_f128 ( ) {
484
+ spec_test :: < f128 > ( ) ;
485
+ }
486
+
487
+ #[ test]
488
+ #[ cfg( f128_enabled) ]
489
+ #[ allow( clippy:: approx_constant) ]
490
+ fn conformance_tests_f128 ( ) {
491
+ let cases = [
492
+ ( f128:: PI , 0x3fffc5bf891b4ef6aa79c3b0520d5db9_u128 ) ,
493
+ // 10_000.0, see `f16` for reasoning.
494
+ (
495
+ f128:: from_bits ( 0x400c3880000000000000000000000000 ) ,
496
+ 0x40059000000000000000000000000000_u128 ,
497
+ ) ,
498
+ ( f128:: from_bits ( 0x0000000f ) , 0x1fc9efbdeb14f4ed9b17ae807907e1e9_u128 ) ,
499
+ ( f128:: INFINITY , f128:: INFINITY . to_bits ( ) ) ,
500
+ ] ;
501
+
502
+ for ( input, output) in cases {
503
+ assert_biteq ! (
504
+ sqrt( input) ,
505
+ f128:: from_bits( output) ,
506
+ "input: {input:?} ({:#018x})" ,
507
+ input. to_bits( )
508
+ ) ;
509
+ }
510
+ }
419
511
}
0 commit comments