Skip to content

Commit

Permalink
add arithmetic_misc test
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneirical committed Aug 8, 2024
1 parent 937ecb4 commit e46f9cc
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ run_test! {fuzz,test1,stable}
run_test! {intrinsics,addr_of,stable}
run_test! {intrinsics,alloc,stable}
run_test! {intrinsics,arith_offset,stable}
run_test! {intrinsics,arithmetic_misc,stable}
run_test! {intrinsics,assert,stable}
run_test! {intrinsics,atomics,stable}
run_test! {intrinsics,bswap,stable}
Expand All @@ -703,7 +704,7 @@ run_test! {intrinsics,cmp_bytes,stable}
run_test! {intrinsics,copy_nonoverlaping,stable}
run_test! {intrinsics,ctpop,stable}
run_test! {intrinsics,malloc,stable}
run_test! {intrinsics,offset_of,stable}
run_test! {intrinsics,offset_of,unstable}
run_test! {intrinsics,overflow_ops,stable}
run_test! {intrinsics,pow_sqrt,stable}
run_test! {intrinsics,printf,stable}
Expand Down
78 changes: 78 additions & 0 deletions test/intrinsics/arithmetic_misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#![feature(
lang_items,
adt_const_params,
associated_type_defaults,
core_intrinsics,
start,
unsized_const_params
)]
#![allow(internal_features, incomplete_features, unused_variables, dead_code)]
#![no_std]
include!("../common.rs");
extern crate core;

use core::intrinsics::copysignf32;
use core::intrinsics::copysignf64;
use core::intrinsics::fmaf32;
use core::intrinsics::fmaf64;
use core::intrinsics::maxnumf32;
use core::intrinsics::maxnumf64;
use core::intrinsics::minnumf32;
use core::intrinsics::minnumf64;

fn main() {
let x = 1.0_f32;
let y = 2.0_f32;
test_eq!(maxnumf32(x, y), black_box(y));
let x = 1.0_f64;
let y = 2.0_f64;
test_eq!(maxnumf64(x, y), black_box(y));
let x = 1.0_f32;
let y = 2.0_f32;
test_eq!(minnumf32(x, y), black_box(x));
let x = 1.0_f64;
let y = 2.0_f64;
test_eq!(minnumf64(x, y), black_box(x));

let m = 10.0_f32;
let x = 4.0_f32;
let b = 60.0_f32;
let result = unsafe { fmaf32(m, x, b) };
test_eq!(fmaf32(m, x, b), black_box(100.0));
test_eq!(m * x + b, black_box(100.0));
let one_plus_eps = 1.0_f32 + f32::EPSILON;
let one_minus_eps = 1.0_f32 - f32::EPSILON;
let minus_one = -1.0_f32;
let result = unsafe { fmaf32(one_plus_eps, one_minus_eps, minus_one) };
// The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
test_eq!(result, black_box(-f32::EPSILON * f32::EPSILON));
// Different rounding with the non-fused multiply and add.
test_eq!(one_plus_eps * one_minus_eps + minus_one, black_box(0.0));
let m = 10.0_f64;
let x = 4.0_f64;
let b = 60.0_f64;
let result = unsafe { fmaf64(m, x, b) };
test_eq!(result, black_box(100.0));
test_eq!(m * x + b, black_box(100.0));
let one_plus_eps = 1.0_f64 + f64::EPSILON;
let one_minus_eps = 1.0_f64 - f64::EPSILON;
let minus_one = -1.0_f64;
let result = unsafe { fmaf64(one_plus_eps, one_minus_eps, minus_one) };
// The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
test_eq!(result, black_box(-f64::EPSILON * f64::EPSILON));
// Different rounding with the non-fused multiply and add.
test_eq!(one_plus_eps * one_minus_eps + minus_one, black_box(0.0));

let f = 3.5_f32;
test_eq!(unsafe { copysignf32(f, 0.42) }, black_box(3.5_f32));
test_eq!(unsafe { copysignf32(f, -0.42) }, black_box(-3.5_f32));
test_eq!(unsafe { copysignf32(-f, 0.42) }, black_box(3.5_f32));
test_eq!(unsafe { copysignf32(-f, -0.42) }, black_box(-3.5_f32));
test!(unsafe { copysignf32(f32::NAN, 1.0) }.is_nan());
let f = 3.5_f64;
test_eq!(unsafe { copysignf64(f, 0.42) }, black_box(3.5_f64));
test_eq!(unsafe { copysignf64(f, -0.42) }, black_box(-3.5_f64));
test_eq!(unsafe { copysignf64(-f, 0.42) }, black_box(3.5_f64));
test_eq!(unsafe { copysignf64(-f, -0.42) }, black_box(-3.5_f64));
test!(unsafe { copysignf64(f64::NAN, 1.0) }.is_nan());
}
17 changes: 10 additions & 7 deletions test/intrinsics/overflow_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ include!("../common.rs");
extern crate core;

use core::intrinsics::add_with_overflow;
use core::intrinsics::sub_with_overflow;
use core::intrinsics::mul_with_overflow;
use core::intrinsics::sub_with_overflow;

fn main() {
test_eq!(add_with_overflow(5u32, 2), black_box(7, false));
test_eq!(add_with_overflow(u32::MAX, 1), black_box(0, true));
test_eq!(sub_with_overflow(5u32, 2), black_box(3, false));
test_eq!(sub_with_overflow(0u32, 1), black_box(u32::MAX, true));
test_eq!(mul_with_overflow(5u32, 2), black_box(10, false));
test_eq!(mul_with_overflow(1_000_000_000u32, 10), black_box(1410065408, true));
test_eq!(add_with_overflow(5u32, 2), black_box((7, false)));
test_eq!(add_with_overflow(u32::MAX, 1), black_box((0, true)));
test_eq!(sub_with_overflow(5u32, 2), black_box((3, false)));
test_eq!(sub_with_overflow(0u32, 1), black_box((u32::MAX, true)));
test_eq!(mul_with_overflow(5u32, 2), black_box((10, false)));
test_eq!(
mul_with_overflow(1_000_000_000u32, 10),
black_box((1410065408, true))
);
}
24 changes: 14 additions & 10 deletions test/intrinsics/pow_sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
include!("../common.rs");
extern crate core;

use core::intrinsics::sqrtf32;
use core::intrinsics::sqrtf64;
// use core::intrinsics::sqrtf32;
// This intrinsic is already imported in common.rs.
use core::intrinsics::exp2f32;
use core::intrinsics::exp2f64;
use core::intrinsics::powf32;
use core::intrinsics::powf64;
use core::intrinsics::powif32;
use core::intrinsics::powif64;
use core::intrinsics::exp2f32;
use core::intrinsics::exp2f64;
use core::intrinsics::sqrtf64;

use core::intrinsics::fabsf32;
use core::intrinsics::fabsf64;

fn main() {
let positive = 4.0_f32;
Expand All @@ -38,24 +42,24 @@ fn main() {
test_eq!(sqrtf64(negative_zero), black_box(negative_zero));

let x = 2.0_f32;
let abs_difference = (powf32(2.0) - (x * x)).abs();
let abs_difference = fabsf32(powf32(x, 2.0) - (x * x));
test!(abs_difference <= black_box(f32::EPSILON));
let x = 2.0_f64;
let abs_difference = (powf64(2.0) - (x * x)).abs();
let abs_difference = fabsf64(powf64(x, 2.0) - (x * x));
test!(abs_difference <= black_box(f64::EPSILON));
let x = 2.0_f32;
let abs_difference = (powif32(2) - (x * x)).abs();
let abs_difference = fabsf32(powif32(x, 2) - (x * x));
test!(abs_difference <= black_box(f32::EPSILON));
let x = 2.0_f64;
let abs_difference = (powif64(2) - (x * x)).abs();
let abs_difference = fabsf64(powif64(x, 2) - (x * x));
test!(abs_difference <= black_box(f64::EPSILON));

let f = 2.0f32;
// 2^2 - 4 == 0
let abs_difference = (exp2f32(f) - 4.0).abs();
let abs_difference = fabsf32(exp2f32(f) - 4.0);
test!(abs_difference <= black_box(f32::EPSILON));
let f = 2.0f64;
// 2^2 - 4 == 0
let abs_difference = (exp2f64(f) - 4.0).abs();
let abs_difference = fabsf64(exp2f64(f) - 4.0);
test!(abs_difference <= black_box(f64::EPSILON));
}
26 changes: 20 additions & 6 deletions test/intrinsics/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
include!("../common.rs");
extern crate core;

use core::intrinsics::ceilf32;
use core::intrinsics::ceilf64;
use core::intrinsics::fabsf32;
use core::intrinsics::fabsf64;
use core::intrinsics::floorf32;
use core::intrinsics::floorf64;
use core::intrinsics::nearbyintf32;
use core::intrinsics::nearbyintf64;
use core::intrinsics::rintf32;
Expand All @@ -21,22 +25,20 @@ use core::intrinsics::roundevenf32;
use core::intrinsics::roundevenf64;
use core::intrinsics::roundf32;
use core::intrinsics::roundf64;
use core::intrinsics::floorf32;
use core::intrinsics::floorf64;
use core::intrinsics::ceilf32;
use core::intrinsics::ceilf64;
use core::intrinsics::truncf32;
use core::intrinsics::truncf64;

fn main() {
let x = 3.5_f32;
let y = -3.5_f32;
test_eq!(fabsf32(x), black_box(x));
test_eq!(fabsf32(y), black_box(-y));
test!(fabsf32(f32::NAN.is_nan()));
test!(fabsf32(f32::NAN).is_nan());
let x = 3.5_f64;
let y = -3.5_f64;
test_eq!(fabsf64(x), black_box(x));
test_eq!(fabsf64(y), black_box(-y));
test!(fabsf64(f64::NAN.is_nan()));
test!(fabsf64(f64::NAN).is_nan());
test_eq!(nearbyintf32(2.5f32), black_box(2.0));
test_eq!(nearbyintf32(3.5f32), black_box(4.0));
test_eq!(nearbyintf64(2.5f64), black_box(2.0));
Expand Down Expand Up @@ -113,4 +115,16 @@ fn main() {
test_eq!(floorf64(f), black_box(3.0));
test_eq!(floorf64(g), black_box(3.0));
test_eq!(floorf64(h), black_box(-4.0));
let f = 3.7_f32;
let g = 3.0_f32;
let h = -3.7_f32;
assert_eq!(truncf32(f), black_box(3.0));
assert_eq!(truncf32(g), black_box(3.0));
assert_eq!(truncf32(h), black_box(-3.0));
let f = 3.7_f64;
let g = 3.0_f64;
let h = -3.7_f64;
assert_eq!(truncf64(f), black_box(3.0));
assert_eq!(truncf64(g), black_box(3.0));
assert_eq!(truncf64(h), black_box(-3.0));
}
11 changes: 7 additions & 4 deletions test/intrinsics/trigonometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ use core::intrinsics::cosf64;
use core::intrinsics::sinf32;
use core::intrinsics::sinf64;

use core::intrinsics::fabsf32;
use core::intrinsics::fabsf64;

fn main() {
let x = 2.0 * core::f32::consts::PI;
let abs_difference = (cosf32(x) - 1.0).abs();
let abs_difference = fabsf32(cosf32(x) - 1.0);
test!(abs_difference <= black_box(f32::EPSILON));
let x = 2.0 * core::f64::consts::PI;
let abs_difference = (cosf64(x) - 1.0).abs();
let abs_difference = fabsf64(cosf64(x) - 1.0);
test!(abs_difference <= black_box(f64::EPSILON));
let x = 2.0 * core::f32::consts::FRAC_PI_2;
let abs_difference = (sinf32(x) - 1.0).abs();
let abs_difference = fabsf32(sinf32(x) - 1.0);
test!(abs_difference <= black_box(f32::EPSILON));
let x = 2.0 * core::f64::consts::FRAC_PI_2;
let abs_difference = (sinf64(x) - 1.0).abs();
let abs_difference = fabsf64(sinf64(x) - 1.0);
test!(abs_difference <= black_box(f64::EPSILON));
}

0 comments on commit e46f9cc

Please sign in to comment.