-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathmain.rs
163 lines (137 loc) · 5.87 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#![feature(core_intrinsics)]
fn i128_to_u64(u: i128) -> Option<u64> {
let min = u64::MIN as i128;
//let max = u64::MAX as i128;
let max = 18446744073709551612_i128;
//println!("{} < {} => {}", u, min, u < min);
//println!("max: {:b}", u64::MAX);
println!("max: {:b}", max);
println!("max: {}", max);
//println!("{}", u < min);
//println!("{}", u > max);
if u < min || u > max {
None
} else {
Some(u as u64)
}
}
fn main() {
/*test_float!(f64, f64, f64::INFINITY, f64::NEG_INFINITY, f64::NAN);
($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => {*/
/*assert_eq!((0.0 as f64).min(0.0), 0.0);
assert!((0.0 as f64).min(0.0).is_sign_positive());
assert_eq!((-0.0 as f64).min(-0.0), -0.0);
assert!((-0.0 as f64).min(-0.0).is_sign_negative());
assert_eq!((9.0 as f64).min(9.0), 9.0);
assert_eq!((-9.0 as f64).min(0.0), -9.0);
assert_eq!((0.0 as f64).min(9.0), 0.0);
assert!((0.0 as f64).min(9.0).is_sign_positive());
assert_eq!((-0.0 as f64).min(9.0), -0.0);
assert!((-0.0 as f64).min(9.0).is_sign_negative());*/
//println!("{}", 9);
//println!("{}", 9.0_f32);
//assert_eq!(-9.0_f32, -9 as f32);
//assert_eq!("1", format!("{:.0}", 1.0f64));
//assert_eq!("9", format!("{:.0}", 9.4f64));
//assert_eq!("10", format!("{:.0}", 9.9f64));
//assert_eq!("9.8", format!("{:.1}", 9.849f64));
//assert_eq!("9.9", format!("{:.1}", 9.851f64));
//assert_eq!("1", format!("{:.0}", 0.5f64));
//assert_eq!(2.3f32.copysign(-1.0), -2.3f32);
/*let f = 9.4f32;
println!("{}", f);*/
//println!("{}", 9.4f32); // FIXME: this is using bytes_in_context(), but gives a wrong value.
/*extern {
//pub fn printf(format: *const i8, ...) -> i32;
pub fn printf(format: *const i8, arg: f64) -> i32;
}
unsafe {
printf(b"Num: %f\n\0" as *const _ as *const _, 9.4f64);
}
println!("{}", 9.4f64);*/
let mut value = 0;
let res = unsafe { std::intrinsics::atomic_cxchg(&mut value, 0, 1) };
println!("{:?}", res);
let res = unsafe { std::intrinsics::atomic_cxchg(&mut value, 0, 1) };
println!("{:?}", res);
use std::sync::atomic::{AtomicBool, Ordering};
let a = AtomicBool::new(false);
assert_eq!(a.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst), Ok(false));
assert_eq!(a.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst), Err(true));
a.store(false, Ordering::SeqCst);
assert_eq!(a.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst), Ok(false));
// FIXME: the code seems to be the same when using an integer, but somehow, it doesn't work for
// a float. Could it be related to the fact that floating-points use different registers?
/*let f = 1234567.89f64;
assert_eq!("1.23456789e6", format!("{:e}", f));
println!("{:e}", f);
println!("{:e}", 1234567.89f64);*/
//assert_eq!("1.23456789e6", format!("{:e}", 1234567.89f64));
/*assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
assert_eq!("1.23456789E3", format!("{:E}", 1234.56789f64));
assert_eq!("0.0", format!("{:?}", 0.0f64));
assert_eq!("1.01", format!("{:?}", 1.01f64));*/
/*assert_eq!((-0.0 as f64).min(-9.0), -9.0);
assert_eq!((f64::INFINITY as f64).min(9.0), 9.0);
assert_eq!((9.0 as f64).min(f64::INFINITY), 9.0);
assert_eq!((f64::INFINITY as f64).min(-9.0), -9.0);
assert_eq!((-9.0 as f64).min(f64::INFINITY), -9.0);
assert_eq!((f64::NEG_INFINITY as f64).min(9.0), f64::NEG_INFINITY);
assert_eq!((9.0 as f64).min(f64::NEG_INFINITY), f64::NEG_INFINITY);
assert_eq!((f64::NEG_INFINITY as f64).min(-9.0), f64::NEG_INFINITY);
assert_eq!((-9.0 as f64).min(f64::NEG_INFINITY), f64::NEG_INFINITY);*/
// Cranelift fmin has NaN propagation
//assert_eq!((f64::NAN as f64).min(9.0), 9.0);
//assert_eq!((f64::NAN as f64).min(-9.0), -9.0);
//assert_eq!((9.0 as f64).min(f64::NAN), 9.0);
//assert_eq!((-9.0 as f64).min(f64::NAN), -9.0);
//assert!((f64::NAN as f64).min(f64::NAN).is_nan());
/*let max: f64 = f32::MAX.into();
assert_eq!(max as f32, f32::MAX);
assert!(max.is_normal());
let min: f64 = f32::MIN.into();
assert_eq!(min as f32, f32::MIN);
assert!(min.is_normal());
let min_positive: f64 = f32::MIN_POSITIVE.into();
assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
assert!(min_positive.is_normal());
let epsilon: f64 = f32::EPSILON.into();
assert_eq!(epsilon as f32, f32::EPSILON);
assert!(epsilon.is_normal());
let zero: f64 = (0.0f32).into();
assert_eq!(zero as f32, 0.0f32);
assert!(zero.is_sign_positive());
let neg_zero: f64 = (-0.0f32).into();
assert_eq!(neg_zero as f32, -0.0f32);
assert!(neg_zero.is_sign_negative());
let infinity: f64 = f32::INFINITY.into();
assert_eq!(infinity as f32, f32::INFINITY);
assert!(infinity.is_infinite());
assert!(infinity.is_sign_positive());
let neg_infinity: f64 = f32::NEG_INFINITY.into();
assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
assert!(neg_infinity.is_infinite());
assert!(neg_infinity.is_sign_negative());
let nan: f64 = f32::NAN.into();
assert!(nan.is_nan());*/
/*use std::convert::TryFrom;
/*let max = <i128>::MAX;
let min = <i128>::MIN;*/
let zero: i128 = 0;
/*let t_max = <u64>::MAX;
let t_min = <u64>::MIN;
assert!(<u64 as TryFrom<i128>>::try_from(max).is_err());
assert!(<u64 as TryFrom<i128>>::try_from(min).is_err());*/
println!("{:?}", i128_to_u64(zero));
assert_eq!(<u64 as TryFrom<i128>>::try_from(zero).unwrap(), zero as u64);
/*assert_eq!(
<u64 as TryFrom<i128>>::try_from(t_max as i128).unwrap(),
t_max as u64
);
assert_eq!(
<u64 as TryFrom<i128>>::try_from(t_min as i128).unwrap(),
t_min as u64
);*/
*/
}