Skip to content

Commit e46f9cc

Browse files
committed
add arithmetic_misc test
1 parent 937ecb4 commit e46f9cc

File tree

6 files changed

+131
-28
lines changed

6 files changed

+131
-28
lines changed

src/compile_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ run_test! {fuzz,test1,stable}
694694
run_test! {intrinsics,addr_of,stable}
695695
run_test! {intrinsics,alloc,stable}
696696
run_test! {intrinsics,arith_offset,stable}
697+
run_test! {intrinsics,arithmetic_misc,stable}
697698
run_test! {intrinsics,assert,stable}
698699
run_test! {intrinsics,atomics,stable}
699700
run_test! {intrinsics,bswap,stable}
@@ -703,7 +704,7 @@ run_test! {intrinsics,cmp_bytes,stable}
703704
run_test! {intrinsics,copy_nonoverlaping,stable}
704705
run_test! {intrinsics,ctpop,stable}
705706
run_test! {intrinsics,malloc,stable}
706-
run_test! {intrinsics,offset_of,stable}
707+
run_test! {intrinsics,offset_of,unstable}
707708
run_test! {intrinsics,overflow_ops,stable}
708709
run_test! {intrinsics,pow_sqrt,stable}
709710
run_test! {intrinsics,printf,stable}

test/intrinsics/arithmetic_misc.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#![feature(
2+
lang_items,
3+
adt_const_params,
4+
associated_type_defaults,
5+
core_intrinsics,
6+
start,
7+
unsized_const_params
8+
)]
9+
#![allow(internal_features, incomplete_features, unused_variables, dead_code)]
10+
#![no_std]
11+
include!("../common.rs");
12+
extern crate core;
13+
14+
use core::intrinsics::copysignf32;
15+
use core::intrinsics::copysignf64;
16+
use core::intrinsics::fmaf32;
17+
use core::intrinsics::fmaf64;
18+
use core::intrinsics::maxnumf32;
19+
use core::intrinsics::maxnumf64;
20+
use core::intrinsics::minnumf32;
21+
use core::intrinsics::minnumf64;
22+
23+
fn main() {
24+
let x = 1.0_f32;
25+
let y = 2.0_f32;
26+
test_eq!(maxnumf32(x, y), black_box(y));
27+
let x = 1.0_f64;
28+
let y = 2.0_f64;
29+
test_eq!(maxnumf64(x, y), black_box(y));
30+
let x = 1.0_f32;
31+
let y = 2.0_f32;
32+
test_eq!(minnumf32(x, y), black_box(x));
33+
let x = 1.0_f64;
34+
let y = 2.0_f64;
35+
test_eq!(minnumf64(x, y), black_box(x));
36+
37+
let m = 10.0_f32;
38+
let x = 4.0_f32;
39+
let b = 60.0_f32;
40+
let result = unsafe { fmaf32(m, x, b) };
41+
test_eq!(fmaf32(m, x, b), black_box(100.0));
42+
test_eq!(m * x + b, black_box(100.0));
43+
let one_plus_eps = 1.0_f32 + f32::EPSILON;
44+
let one_minus_eps = 1.0_f32 - f32::EPSILON;
45+
let minus_one = -1.0_f32;
46+
let result = unsafe { fmaf32(one_plus_eps, one_minus_eps, minus_one) };
47+
// The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
48+
test_eq!(result, black_box(-f32::EPSILON * f32::EPSILON));
49+
// Different rounding with the non-fused multiply and add.
50+
test_eq!(one_plus_eps * one_minus_eps + minus_one, black_box(0.0));
51+
let m = 10.0_f64;
52+
let x = 4.0_f64;
53+
let b = 60.0_f64;
54+
let result = unsafe { fmaf64(m, x, b) };
55+
test_eq!(result, black_box(100.0));
56+
test_eq!(m * x + b, black_box(100.0));
57+
let one_plus_eps = 1.0_f64 + f64::EPSILON;
58+
let one_minus_eps = 1.0_f64 - f64::EPSILON;
59+
let minus_one = -1.0_f64;
60+
let result = unsafe { fmaf64(one_plus_eps, one_minus_eps, minus_one) };
61+
// The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
62+
test_eq!(result, black_box(-f64::EPSILON * f64::EPSILON));
63+
// Different rounding with the non-fused multiply and add.
64+
test_eq!(one_plus_eps * one_minus_eps + minus_one, black_box(0.0));
65+
66+
let f = 3.5_f32;
67+
test_eq!(unsafe { copysignf32(f, 0.42) }, black_box(3.5_f32));
68+
test_eq!(unsafe { copysignf32(f, -0.42) }, black_box(-3.5_f32));
69+
test_eq!(unsafe { copysignf32(-f, 0.42) }, black_box(3.5_f32));
70+
test_eq!(unsafe { copysignf32(-f, -0.42) }, black_box(-3.5_f32));
71+
test!(unsafe { copysignf32(f32::NAN, 1.0) }.is_nan());
72+
let f = 3.5_f64;
73+
test_eq!(unsafe { copysignf64(f, 0.42) }, black_box(3.5_f64));
74+
test_eq!(unsafe { copysignf64(f, -0.42) }, black_box(-3.5_f64));
75+
test_eq!(unsafe { copysignf64(-f, 0.42) }, black_box(3.5_f64));
76+
test_eq!(unsafe { copysignf64(-f, -0.42) }, black_box(-3.5_f64));
77+
test!(unsafe { copysignf64(f64::NAN, 1.0) }.is_nan());
78+
}

test/intrinsics/overflow_ops.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ include!("../common.rs");
1212
extern crate core;
1313

1414
use core::intrinsics::add_with_overflow;
15-
use core::intrinsics::sub_with_overflow;
1615
use core::intrinsics::mul_with_overflow;
16+
use core::intrinsics::sub_with_overflow;
1717

1818
fn main() {
19-
test_eq!(add_with_overflow(5u32, 2), black_box(7, false));
20-
test_eq!(add_with_overflow(u32::MAX, 1), black_box(0, true));
21-
test_eq!(sub_with_overflow(5u32, 2), black_box(3, false));
22-
test_eq!(sub_with_overflow(0u32, 1), black_box(u32::MAX, true));
23-
test_eq!(mul_with_overflow(5u32, 2), black_box(10, false));
24-
test_eq!(mul_with_overflow(1_000_000_000u32, 10), black_box(1410065408, true));
19+
test_eq!(add_with_overflow(5u32, 2), black_box((7, false)));
20+
test_eq!(add_with_overflow(u32::MAX, 1), black_box((0, true)));
21+
test_eq!(sub_with_overflow(5u32, 2), black_box((3, false)));
22+
test_eq!(sub_with_overflow(0u32, 1), black_box((u32::MAX, true)));
23+
test_eq!(mul_with_overflow(5u32, 2), black_box((10, false)));
24+
test_eq!(
25+
mul_with_overflow(1_000_000_000u32, 10),
26+
black_box((1410065408, true))
27+
);
2528
}

test/intrinsics/pow_sqrt.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
include!("../common.rs");
1212
extern crate core;
1313

14-
use core::intrinsics::sqrtf32;
15-
use core::intrinsics::sqrtf64;
14+
// use core::intrinsics::sqrtf32;
15+
// This intrinsic is already imported in common.rs.
16+
use core::intrinsics::exp2f32;
17+
use core::intrinsics::exp2f64;
1618
use core::intrinsics::powf32;
1719
use core::intrinsics::powf64;
1820
use core::intrinsics::powif32;
1921
use core::intrinsics::powif64;
20-
use core::intrinsics::exp2f32;
21-
use core::intrinsics::exp2f64;
22+
use core::intrinsics::sqrtf64;
23+
24+
use core::intrinsics::fabsf32;
25+
use core::intrinsics::fabsf64;
2226

2327
fn main() {
2428
let positive = 4.0_f32;
@@ -38,24 +42,24 @@ fn main() {
3842
test_eq!(sqrtf64(negative_zero), black_box(negative_zero));
3943

4044
let x = 2.0_f32;
41-
let abs_difference = (powf32(2.0) - (x * x)).abs();
45+
let abs_difference = fabsf32(powf32(x, 2.0) - (x * x));
4246
test!(abs_difference <= black_box(f32::EPSILON));
4347
let x = 2.0_f64;
44-
let abs_difference = (powf64(2.0) - (x * x)).abs();
48+
let abs_difference = fabsf64(powf64(x, 2.0) - (x * x));
4549
test!(abs_difference <= black_box(f64::EPSILON));
4650
let x = 2.0_f32;
47-
let abs_difference = (powif32(2) - (x * x)).abs();
51+
let abs_difference = fabsf32(powif32(x, 2) - (x * x));
4852
test!(abs_difference <= black_box(f32::EPSILON));
4953
let x = 2.0_f64;
50-
let abs_difference = (powif64(2) - (x * x)).abs();
54+
let abs_difference = fabsf64(powif64(x, 2) - (x * x));
5155
test!(abs_difference <= black_box(f64::EPSILON));
5256

5357
let f = 2.0f32;
5458
// 2^2 - 4 == 0
55-
let abs_difference = (exp2f32(f) - 4.0).abs();
59+
let abs_difference = fabsf32(exp2f32(f) - 4.0);
5660
test!(abs_difference <= black_box(f32::EPSILON));
5761
let f = 2.0f64;
5862
// 2^2 - 4 == 0
59-
let abs_difference = (exp2f64(f) - 4.0).abs();
63+
let abs_difference = fabsf64(exp2f64(f) - 4.0);
6064
test!(abs_difference <= black_box(f64::EPSILON));
6165
}

test/intrinsics/round.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
include!("../common.rs");
1212
extern crate core;
1313

14+
use core::intrinsics::ceilf32;
15+
use core::intrinsics::ceilf64;
1416
use core::intrinsics::fabsf32;
1517
use core::intrinsics::fabsf64;
18+
use core::intrinsics::floorf32;
19+
use core::intrinsics::floorf64;
1620
use core::intrinsics::nearbyintf32;
1721
use core::intrinsics::nearbyintf64;
1822
use core::intrinsics::rintf32;
@@ -21,22 +25,20 @@ use core::intrinsics::roundevenf32;
2125
use core::intrinsics::roundevenf64;
2226
use core::intrinsics::roundf32;
2327
use core::intrinsics::roundf64;
24-
use core::intrinsics::floorf32;
25-
use core::intrinsics::floorf64;
26-
use core::intrinsics::ceilf32;
27-
use core::intrinsics::ceilf64;
28+
use core::intrinsics::truncf32;
29+
use core::intrinsics::truncf64;
2830

2931
fn main() {
3032
let x = 3.5_f32;
3133
let y = -3.5_f32;
3234
test_eq!(fabsf32(x), black_box(x));
3335
test_eq!(fabsf32(y), black_box(-y));
34-
test!(fabsf32(f32::NAN.is_nan()));
36+
test!(fabsf32(f32::NAN).is_nan());
3537
let x = 3.5_f64;
3638
let y = -3.5_f64;
3739
test_eq!(fabsf64(x), black_box(x));
3840
test_eq!(fabsf64(y), black_box(-y));
39-
test!(fabsf64(f64::NAN.is_nan()));
41+
test!(fabsf64(f64::NAN).is_nan());
4042
test_eq!(nearbyintf32(2.5f32), black_box(2.0));
4143
test_eq!(nearbyintf32(3.5f32), black_box(4.0));
4244
test_eq!(nearbyintf64(2.5f64), black_box(2.0));
@@ -113,4 +115,16 @@ fn main() {
113115
test_eq!(floorf64(f), black_box(3.0));
114116
test_eq!(floorf64(g), black_box(3.0));
115117
test_eq!(floorf64(h), black_box(-4.0));
118+
let f = 3.7_f32;
119+
let g = 3.0_f32;
120+
let h = -3.7_f32;
121+
assert_eq!(truncf32(f), black_box(3.0));
122+
assert_eq!(truncf32(g), black_box(3.0));
123+
assert_eq!(truncf32(h), black_box(-3.0));
124+
let f = 3.7_f64;
125+
let g = 3.0_f64;
126+
let h = -3.7_f64;
127+
assert_eq!(truncf64(f), black_box(3.0));
128+
assert_eq!(truncf64(g), black_box(3.0));
129+
assert_eq!(truncf64(h), black_box(-3.0));
116130
}

test/intrinsics/trigonometry.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ use core::intrinsics::cosf64;
1616
use core::intrinsics::sinf32;
1717
use core::intrinsics::sinf64;
1818

19+
use core::intrinsics::fabsf32;
20+
use core::intrinsics::fabsf64;
21+
1922
fn main() {
2023
let x = 2.0 * core::f32::consts::PI;
21-
let abs_difference = (cosf32(x) - 1.0).abs();
24+
let abs_difference = fabsf32(cosf32(x) - 1.0);
2225
test!(abs_difference <= black_box(f32::EPSILON));
2326
let x = 2.0 * core::f64::consts::PI;
24-
let abs_difference = (cosf64(x) - 1.0).abs();
27+
let abs_difference = fabsf64(cosf64(x) - 1.0);
2528
test!(abs_difference <= black_box(f64::EPSILON));
2629
let x = 2.0 * core::f32::consts::FRAC_PI_2;
27-
let abs_difference = (sinf32(x) - 1.0).abs();
30+
let abs_difference = fabsf32(sinf32(x) - 1.0);
2831
test!(abs_difference <= black_box(f32::EPSILON));
2932
let x = 2.0 * core::f64::consts::FRAC_PI_2;
30-
let abs_difference = (sinf64(x) - 1.0).abs();
33+
let abs_difference = fabsf64(sinf64(x) - 1.0);
3134
test!(abs_difference <= black_box(f64::EPSILON));
3235
}

0 commit comments

Comments
 (0)