-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.rs
210 lines (191 loc) · 5.62 KB
/
build.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#[macro_use]
extern crate quote;
extern crate syn;
use std::{env, fmt};
use std::io::Write;
use std::fs::File;
use std::path::{Path, PathBuf};
struct Q {
bits: u8,
fbits: u8,
}
impl Q {
fn ibits(&self) -> u8 {
self.bits - self.fbits
}
}
impl fmt::Display for Q {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "I{}F{}", self.ibits(), self.fbits)
}
}
enum Primitive {
I8,
I16,
I32,
I64,
Isize,
U8,
U16,
U32,
U64,
Usize,
}
impl Primitive {
fn ibits(&self) -> u8 {
match *self {
Primitive::I8 => 8,
Primitive::I16 => 16,
Primitive::I32 => 32,
Primitive::I64 => 64,
Primitive::Isize => 64,
Primitive::U8 => 9,
Primitive::U16 => 17,
Primitive::U32 => 33,
Primitive::U64 => 65,
Primitive::Usize => 65,
}
}
fn is_ixx(&self) -> bool {
match *self {
Primitive::I8 => true,
Primitive::I16 => true,
Primitive::I32 => true,
Primitive::I64 => true,
Primitive::Isize => true,
_ => false,
}
}
}
impl fmt::Display for Primitive {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match *self {
Primitive::I8 => "i8",
Primitive::I16 => "i16",
Primitive::I32 => "i32",
Primitive::I64 => "i64",
Primitive::Isize => "isize",
Primitive::U8 => "u8",
Primitive::U16 => "u16",
Primitive::U32 => "u32",
Primitive::U64 => "u64",
Primitive::Usize => "usize",
};
f.write_str(s)
}
}
const PRIMITIVES: [Primitive; 10] = [
Primitive::I8,
Primitive::I16,
Primitive::I32,
Primitive::I64,
Primitive::Isize,
Primitive::U8,
Primitive::U16,
Primitive::U32,
Primitive::U64,
Primitive::Usize,
];
fn main() {
let mut qs = vec![];
for bits in &[8, 16, 32] {
for fbits in 1..*bits {
qs.push(
Q {
bits: *bits,
fbits: fbits,
},
)
}
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let cross_tests: Vec<_> = qs.iter().flat_map(|qx| {
let test_name = syn::Ident::from(format!("i{}f{}", qx.ibits(), qx.fbits));
let qx_name = syn::Ident::from(format!("{}", qx));
let asserts: Vec<_> = qs.iter().map(move |qy| {
let qy_name = syn::Ident::from(format!("{}", qy));
if qx.ibits() < qy.ibits() {
quote! {
assert_eq!(f64(#qx_name(#qy_name(0.5_f64).unwrap()).unwrap()), 0.5);
}
} else {
quote! {
assert_eq!(f64(#qx_name(#qy_name(0.5_f64).unwrap())), 0.5);
}
}
}).collect();
quote! {
#[test]
fn #test_name() {
#(#asserts)*
}
}
}).collect();
let cross_tokens = quote! { #(#cross_tests)* };
let file_path = Path::new(&out_dir).join("cross.rs");
let mut f = File::create(&file_path).unwrap();
f.write_all(cross_tokens.to_string().as_bytes()).unwrap();
let to_ixx_tests: Vec<_> = qs.iter().flat_map(|q| {
let test_name = syn::Ident::from(format!("i{}f{}", q.ibits(), q.fbits));
let q_name = syn::Ident::from(format!("{}", q));
let asserts: Vec<_> = PRIMITIVES.iter().map(move |p| {
let p_name = syn::Ident::from(format!("{}", p));
let (f, i): (f64, u8) = if q.ibits() == 1 {
(0.4, 0)
} else {
(1.1, 1)
};
if p.ibits() < q.ibits() || !p.is_ixx() {
quote! {
assert_eq!(#p_name(#q_name(#f).unwrap()).unwrap(), #i as #p_name);
}
} else {
quote! {
assert_eq!(#p_name(#q_name(#f).unwrap()), #i as #p_name);
}
}
}).collect();
quote! {
#[test]
fn #test_name() {
#(#asserts)*
}
}
}).collect();
let to_ixx_tokens = quote! { #(#to_ixx_tests)* };
let file_path = Path::new(&out_dir).join("to-ixx.rs");
let mut f = File::create(&file_path).unwrap();
f.write_all(to_ixx_tokens.to_string().as_bytes()).unwrap();
let from_ixx_tests: Vec<_> = qs.iter().flat_map(|q| {
let test_name = syn::Ident::from(format!("i{}f{}", q.ibits(), q.fbits));
let q_name = syn::Ident::from(format!("{}", q));
let asserts: Vec<_> = PRIMITIVES.iter().map(move |p| {
let p_name = syn::Ident::from(format!("{}", p));
let i = if q.ibits() == 1 {
0
} else {
1
};
if p.ibits() <= q.ibits() {
quote! {
assert_eq!(f64(#q_name(#i as #p_name)), #i as f64);
}
} else {
quote! {
assert_eq!(f64(#q_name(#i as #p_name).unwrap()), #i as f64);
}
}
}).collect();
quote! {
#[test]
fn #test_name() {
#(#asserts)*
}
}
}).collect();
let from_ixx_tokens = quote! { #(#from_ixx_tests)* };
let file_path = Path::new(&out_dir).join("from-ixx.rs");
let mut f = File::create(&file_path).unwrap();
f.write_all(from_ixx_tokens.to_string().as_bytes()).unwrap();
println!("cargo:rerun-if-changed=build.rs");
}