build.rs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. #![feature(i128_type, i128)]
  2. extern crate cast;
  3. extern crate rand;
  4. use std::collections::HashMap;
  5. use std::fmt::Write as FmtWrite;
  6. use std::fs::{self, OpenOptions};
  7. use std::io::Write;
  8. use std::hash::{Hash, Hasher};
  9. use std::path::PathBuf;
  10. use std::{env, mem};
  11. use std::fmt;
  12. use self::cast::{f32, f64, u32, u64, u128, i32, i64, i128};
  13. use self::rand::Rng;
  14. const NTESTS: usize = 1_000;
  15. fn main() {
  16. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  17. let out_file = out_dir.join("generated.rs");
  18. drop(fs::remove_file(&out_file));
  19. let target = env::var("TARGET").unwrap();
  20. let target_arch_arm =
  21. target.contains("arm") ||
  22. target.contains("thumb");
  23. let target_arch_mips = target.contains("mips");
  24. // TODO accept NaNs. We don't do that right now because we can't check
  25. // for NaN-ness on the thumb targets (due to missing intrinsics)
  26. // float/add.rs
  27. gen(|(a, b): (MyF64, MyF64)| {
  28. let c = a.0 + b.0;
  29. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  30. None
  31. } else {
  32. Some(c)
  33. }
  34. },
  35. "compiler_builtins::float::add::__adddf3(a, b)");
  36. gen(|(a, b): (MyF32, MyF32)| {
  37. let c = a.0 + b.0;
  38. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  39. None
  40. } else {
  41. Some(c)
  42. }
  43. },
  44. "compiler_builtins::float::add::__addsf3(a, b)");
  45. if target_arch_arm {
  46. gen(|(a, b): (MyF64, MyF64)| {
  47. let c = a.0 + b.0;
  48. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  49. None
  50. } else {
  51. Some(c)
  52. }
  53. },
  54. "compiler_builtins::float::add::__adddf3vfp(a, b)");
  55. gen(|(a, b): (LargeF32, LargeF32)| {
  56. let c = a.0 + b.0;
  57. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  58. None
  59. } else {
  60. Some(c)
  61. }
  62. },
  63. "compiler_builtins::float::add::__addsf3vfp(a, b)");
  64. }
  65. // float/cmp.rs
  66. gen(|(a, b): (MyF64, MyF64)| {
  67. let (a, b) = (a.0, b.0);
  68. if a.is_nan() || b.is_nan() {
  69. return None;
  70. }
  71. if a.is_nan() || b.is_nan() {
  72. Some(-1)
  73. } else if a < b {
  74. Some(-1)
  75. } else if a > b {
  76. Some(1)
  77. } else {
  78. Some(0)
  79. }
  80. },
  81. "compiler_builtins::float::cmp::__gedf2(a, b)");
  82. gen(|(a, b): (MyF32, MyF32)| {
  83. let (a, b) = (a.0, b.0);
  84. if a.is_nan() || b.is_nan() {
  85. return None;
  86. }
  87. if a.is_nan() || b.is_nan() {
  88. Some(-1)
  89. } else if a < b {
  90. Some(-1)
  91. } else if a > b {
  92. Some(1)
  93. } else {
  94. Some(0)
  95. }
  96. },
  97. "compiler_builtins::float::cmp::__gesf2(a, b)");
  98. gen(|(a, b): (MyF64, MyF64)| {
  99. let (a, b) = (a.0, b.0);
  100. if a.is_nan() || b.is_nan() {
  101. return None;
  102. }
  103. if a.is_nan() || b.is_nan() {
  104. Some(1)
  105. } else if a < b {
  106. Some(-1)
  107. } else if a > b {
  108. Some(1)
  109. } else {
  110. Some(0)
  111. }
  112. },
  113. "compiler_builtins::float::cmp::__ledf2(a, b)");
  114. gen(|(a, b): (MyF32, MyF32)| {
  115. let (a, b) = (a.0, b.0);
  116. if a.is_nan() || b.is_nan() {
  117. return None;
  118. }
  119. if a.is_nan() || b.is_nan() {
  120. Some(1)
  121. } else if a < b {
  122. Some(-1)
  123. } else if a > b {
  124. Some(1)
  125. } else {
  126. Some(0)
  127. }
  128. },
  129. "compiler_builtins::float::cmp::__lesf2(a, b)");
  130. if target_arch_arm {
  131. gen(|(a, b): (MyF32, MyF32)| {
  132. if a.0.is_nan() || b.0.is_nan() {
  133. return None;
  134. }
  135. let c = (a.0 <= b.0) as i32;
  136. Some(c)
  137. },
  138. "compiler_builtins::float::cmp::__aeabi_fcmple(a, b)");
  139. gen(|(a, b): (MyF32, MyF32)| {
  140. if a.0.is_nan() || b.0.is_nan() {
  141. return None;
  142. }
  143. let c = (a.0 >= b.0) as i32;
  144. Some(c)
  145. },
  146. "compiler_builtins::float::cmp::__aeabi_fcmpge(a, b)");
  147. gen(|(a, b): (MyF32, MyF32)| {
  148. if a.0.is_nan() || b.0.is_nan() {
  149. return None;
  150. }
  151. let c = (a.0 == b.0) as i32;
  152. Some(c)
  153. },
  154. "compiler_builtins::float::cmp::__aeabi_fcmpeq(a, b)");
  155. gen(|(a, b): (MyF32, MyF32)| {
  156. if a.0.is_nan() || b.0.is_nan() {
  157. return None;
  158. }
  159. let c = (a.0 < b.0) as i32;
  160. Some(c)
  161. },
  162. "compiler_builtins::float::cmp::__aeabi_fcmplt(a, b)");
  163. gen(|(a, b): (MyF32, MyF32)| {
  164. if a.0.is_nan() || b.0.is_nan() {
  165. return None;
  166. }
  167. let c = (a.0 > b.0) as i32;
  168. Some(c)
  169. },
  170. "compiler_builtins::float::cmp::__aeabi_fcmpgt(a, b)");
  171. gen(|(a, b): (MyF64, MyF64)| {
  172. if a.0.is_nan() || b.0.is_nan() {
  173. return None;
  174. }
  175. let c = (a.0 <= b.0) as i32;
  176. Some(c)
  177. },
  178. "compiler_builtins::float::cmp::__aeabi_dcmple(a, b)");
  179. gen(|(a, b): (MyF64, MyF64)| {
  180. if a.0.is_nan() || b.0.is_nan() {
  181. return None;
  182. }
  183. let c = (a.0 >= b.0) as i32;
  184. Some(c)
  185. },
  186. "compiler_builtins::float::cmp::__aeabi_dcmpge(a, b)");
  187. gen(|(a, b): (MyF64, MyF64)| {
  188. if a.0.is_nan() || b.0.is_nan() {
  189. return None;
  190. }
  191. let c = (a.0 == b.0) as i32;
  192. Some(c)
  193. },
  194. "compiler_builtins::float::cmp::__aeabi_dcmpeq(a, b)");
  195. gen(|(a, b): (MyF64, MyF64)| {
  196. if a.0.is_nan() || b.0.is_nan() {
  197. return None;
  198. }
  199. let c = (a.0 < b.0) as i32;
  200. Some(c)
  201. },
  202. "compiler_builtins::float::cmp::__aeabi_dcmplt(a, b)");
  203. gen(|(a, b): (MyF64, MyF64)| {
  204. if a.0.is_nan() || b.0.is_nan() {
  205. return None;
  206. }
  207. let c = (a.0 > b.0) as i32;
  208. Some(c)
  209. },
  210. "compiler_builtins::float::cmp::__aeabi_dcmpgt(a, b)");
  211. }
  212. // float/conv.rs
  213. gen(|a: MyF64| i64(a.0).ok(),
  214. "compiler_builtins::float::conv::__fixdfdi(a)");
  215. gen(|a: MyF64| i32(a.0).ok(),
  216. "compiler_builtins::float::conv::__fixdfsi(a)");
  217. gen(|a: MyF32| i64(a.0).ok(),
  218. "compiler_builtins::float::conv::__fixsfdi(a)");
  219. gen(|a: MyF32| i32(a.0).ok(),
  220. "compiler_builtins::float::conv::__fixsfsi(a)");
  221. gen(|a: MyF32| i128(a.0).ok(),
  222. "compiler_builtins::float::conv::__fixsfti(a)");
  223. gen(|a: MyF64| i128(a.0).ok(),
  224. "compiler_builtins::float::conv::__fixdfti(a)");
  225. gen(|a: MyF64| u64(a.0).ok(),
  226. "compiler_builtins::float::conv::__fixunsdfdi(a)");
  227. gen(|a: MyF64| u32(a.0).ok(),
  228. "compiler_builtins::float::conv::__fixunsdfsi(a)");
  229. gen(|a: MyF32| u64(a.0).ok(),
  230. "compiler_builtins::float::conv::__fixunssfdi(a)");
  231. gen(|a: MyF32| u32(a.0).ok(),
  232. "compiler_builtins::float::conv::__fixunssfsi(a)");
  233. gen(|a: MyF32| u128(a.0).ok(),
  234. "compiler_builtins::float::conv::__fixunssfti(a)");
  235. gen(|a: MyF64| u128(a.0).ok(),
  236. "compiler_builtins::float::conv::__fixunsdfti(a)");
  237. gen(|a: MyI64| Some(f64(a.0)),
  238. "compiler_builtins::float::conv::__floatdidf(a)");
  239. gen(|a: MyI32| Some(f64(a.0)),
  240. "compiler_builtins::float::conv::__floatsidf(a)");
  241. gen(|a: MyI32| Some(f32(a.0)),
  242. "compiler_builtins::float::conv::__floatsisf(a)");
  243. gen(|a: MyU64| Some(f64(a.0)),
  244. "compiler_builtins::float::conv::__floatundidf(a)");
  245. gen(|a: MyU32| Some(f64(a.0)),
  246. "compiler_builtins::float::conv::__floatunsidf(a)");
  247. gen(|a: MyU32| Some(f32(a.0)),
  248. "compiler_builtins::float::conv::__floatunsisf(a)");
  249. gen(|a: MyU128| f32(a.0).ok(),
  250. "compiler_builtins::float::conv::__floatuntisf(a)");
  251. if !target_arch_mips {
  252. gen(|a: MyI128| Some(f32(a.0)),
  253. "compiler_builtins::float::conv::__floattisf(a)");
  254. gen(|a: MyI128| Some(f64(a.0)),
  255. "compiler_builtins::float::conv::__floattidf(a)");
  256. gen(|a: MyU128| Some(f64(a.0)),
  257. "compiler_builtins::float::conv::__floatuntidf(a)");
  258. }
  259. // float/pow.rs
  260. gen(|(a, b): (MyF64, MyI32)| {
  261. let c = a.0.powi(b.0);
  262. if a.0.is_nan() || c.is_nan() {
  263. None
  264. } else {
  265. Some(c)
  266. }
  267. },
  268. "compiler_builtins::float::pow::__powidf2(a, b)");
  269. gen(|(a, b): (MyF32, MyI32)| {
  270. let c = a.0.powi(b.0);
  271. if a.0.is_nan() || c.is_nan() {
  272. None
  273. } else {
  274. Some(c)
  275. }
  276. },
  277. "compiler_builtins::float::pow::__powisf2(a, b)");
  278. // float/sub.rs
  279. gen(|(a, b): (MyF64, MyF64)| {
  280. let c = a.0 - b.0;
  281. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  282. None
  283. } else {
  284. Some(c)
  285. }
  286. },
  287. "compiler_builtins::float::sub::__subdf3(a, b)");
  288. gen(|(a, b): (MyF32, MyF32)| {
  289. let c = a.0 - b.0;
  290. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  291. None
  292. } else {
  293. Some(c)
  294. }
  295. },
  296. "compiler_builtins::float::sub::__subsf3(a, b)");
  297. if target_arch_arm {
  298. gen(|(a, b): (MyF64, MyF64)| {
  299. let c = a.0 - b.0;
  300. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  301. None
  302. } else {
  303. Some(c)
  304. }
  305. },
  306. "compiler_builtins::float::sub::__subdf3vfp(a, b)");
  307. gen(|(a, b): (LargeF32, LargeF32)| {
  308. let c = a.0 - b.0;
  309. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  310. None
  311. } else {
  312. Some(c)
  313. }
  314. },
  315. "compiler_builtins::float::sub::__subsf3vfp(a, b)");
  316. }
  317. // float/mul.rs
  318. gen(|(a, b): (MyF64, MyF64)| {
  319. let c = a.0 * b.0;
  320. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  321. None
  322. } else {
  323. Some(c)
  324. }
  325. },
  326. "compiler_builtins::float::mul::__muldf3(a, b)");
  327. gen(|(a, b): (LargeF32, LargeF32)| {
  328. let c = a.0 * b.0;
  329. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  330. None
  331. } else {
  332. Some(c)
  333. }
  334. },
  335. "compiler_builtins::float::mul::__mulsf3(a, b)");
  336. if target_arch_arm {
  337. gen(|(a, b): (MyF64, MyF64)| {
  338. let c = a.0 * b.0;
  339. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  340. None
  341. } else {
  342. Some(c)
  343. }
  344. },
  345. "compiler_builtins::float::mul::__muldf3vfp(a, b)");
  346. gen(|(a, b): (LargeF32, LargeF32)| {
  347. let c = a.0 * b.0;
  348. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  349. None
  350. } else {
  351. Some(c)
  352. }
  353. },
  354. "compiler_builtins::float::mul::__mulsf3vfp(a, b)");
  355. }
  356. // float/div.rs
  357. gen(|(a, b): (MyF64, MyF64)| {
  358. if b.0 == 0.0 {
  359. return None
  360. }
  361. let c = a.0 / b.0;
  362. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  363. c.abs() <= unsafe { mem::transmute(4503599627370495u64) }
  364. {
  365. None
  366. } else {
  367. Some(c)
  368. }
  369. },
  370. "compiler_builtins::float::div::__divdf3(a, b)");
  371. gen(|(a, b): (LargeF32, LargeF32)| {
  372. if b.0 == 0.0 {
  373. return None
  374. }
  375. let c = a.0 / b.0;
  376. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  377. c.abs() <= unsafe { mem::transmute(16777215u32) }
  378. {
  379. None
  380. } else {
  381. Some(c)
  382. }
  383. },
  384. "compiler_builtins::float::div::__divsf3(a, b)");
  385. if target_arch_arm {
  386. gen(|(a, b): (MyF64, MyF64)| {
  387. if b.0 == 0.0 {
  388. return None
  389. }
  390. let c = a.0 / b.0;
  391. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  392. c.abs() <= unsafe { mem::transmute(4503599627370495u64) }
  393. {
  394. None
  395. } else {
  396. Some(c)
  397. }
  398. },
  399. "compiler_builtins::float::div::__divdf3vfp(a, b)");
  400. gen(|(a, b): (LargeF32, LargeF32)| {
  401. if b.0 == 0.0 {
  402. return None
  403. }
  404. let c = a.0 / b.0;
  405. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  406. c.abs() <= unsafe { mem::transmute(16777215u32) }
  407. {
  408. None
  409. } else {
  410. Some(c)
  411. }
  412. },
  413. "compiler_builtins::float::div::__divsf3vfp(a, b)");
  414. }
  415. // int/addsub.rs
  416. gen(|(a, b): (MyU128, MyU128)| Some(a.0.wrapping_add(b.0)),
  417. "compiler_builtins::int::addsub::rust_u128_add(a, b)");
  418. gen(|(a, b): (MyI128, MyI128)| Some(a.0.wrapping_add(b.0)),
  419. "compiler_builtins::int::addsub::rust_i128_add(a, b)");
  420. gen(|(a, b): (MyU128, MyU128)| Some(a.0.overflowing_add(b.0)),
  421. "compiler_builtins::int::addsub::rust_u128_addo(a, b)");
  422. gen(|(a, b): (MyI128, MyI128)| Some(a.0.overflowing_add(b.0)),
  423. "compiler_builtins::int::addsub::rust_i128_addo(a, b)");
  424. gen(|(a, b): (MyU128, MyU128)| Some(a.0.wrapping_sub(b.0)),
  425. "compiler_builtins::int::addsub::rust_u128_sub(a, b)");
  426. gen(|(a, b): (MyI128, MyI128)| Some(a.0.wrapping_sub(b.0)),
  427. "compiler_builtins::int::addsub::rust_i128_sub(a, b)");
  428. gen(|(a, b): (MyU128, MyU128)| Some(a.0.overflowing_sub(b.0)),
  429. "compiler_builtins::int::addsub::rust_u128_subo(a, b)");
  430. gen(|(a, b): (MyI128, MyI128)| Some(a.0.overflowing_sub(b.0)),
  431. "compiler_builtins::int::addsub::rust_i128_subo(a, b)");
  432. // int/mul.rs
  433. gen(|(a, b): (MyU64, MyU64)| Some(a.0.wrapping_mul(b.0)),
  434. "compiler_builtins::int::mul::__muldi3(a, b)");
  435. gen(|(a, b): (MyI64, MyI64)| Some(a.0.overflowing_mul(b.0)),
  436. "{
  437. let mut o = 2;
  438. let c = compiler_builtins::int::mul::__mulodi4(a, b, &mut o);
  439. (c, match o { 0 => false, 1 => true, _ => panic!() })
  440. }");
  441. gen(|(a, b): (MyI32, MyI32)| Some(a.0.overflowing_mul(b.0)),
  442. "{
  443. let mut o = 2;
  444. let c = compiler_builtins::int::mul::__mulosi4(a, b, &mut o);
  445. (c, match o { 0 => false, 1 => true, _ => panic!() })
  446. }");
  447. gen(|(a, b): (MyI128, MyI128)| Some(a.0.wrapping_mul(b.0)),
  448. "compiler_builtins::int::mul::__multi3(a, b)");
  449. if !target_arch_mips { // FIXME(#137)
  450. gen(|(a, b): (MyI128, MyI128)| Some(a.0.overflowing_mul(b.0)),
  451. "{
  452. let mut o = 2;
  453. let c = compiler_builtins::int::mul::__muloti4(a, b, &mut o);
  454. (c, match o { 0 => false, 1 => true, _ => panic!() })
  455. }");
  456. }
  457. // int/sdiv.rs
  458. gen(|(a, b): (MyI64, MyI64)| {
  459. if b.0 == 0 {
  460. None
  461. } else {
  462. Some(a.0 / b.0)
  463. }
  464. },
  465. "compiler_builtins::int::sdiv::__divdi3(a, b)");
  466. gen(|(a, b): (MyI64, MyI64)| {
  467. if b.0 == 0 {
  468. None
  469. } else {
  470. Some((a.0 / b.0, a.0 % b.0))
  471. }
  472. },
  473. "{
  474. let mut r = 0;
  475. (compiler_builtins::int::sdiv::__divmoddi4(a, b, &mut r), r)
  476. }");
  477. gen(|(a, b): (MyI32, MyI32)| {
  478. if b.0 == 0 {
  479. None
  480. } else {
  481. Some((a.0 / b.0, a.0 % b.0))
  482. }
  483. },
  484. "{
  485. let mut r = 0;
  486. (compiler_builtins::int::sdiv::__divmodsi4(a, b, &mut r), r)
  487. }");
  488. gen(|(a, b): (MyI32, MyI32)| {
  489. if b.0 == 0 {
  490. None
  491. } else {
  492. Some(a.0 / b.0)
  493. }
  494. },
  495. "compiler_builtins::int::sdiv::__divsi3(a, b)");
  496. gen(|(a, b): (MyI32, MyI32)| {
  497. if b.0 == 0 {
  498. None
  499. } else {
  500. Some(a.0 % b.0)
  501. }
  502. },
  503. "compiler_builtins::int::sdiv::__modsi3(a, b)");
  504. gen(|(a, b): (MyI64, MyI64)| {
  505. if b.0 == 0 {
  506. None
  507. } else {
  508. Some(a.0 % b.0)
  509. }
  510. },
  511. "compiler_builtins::int::sdiv::__moddi3(a, b)");
  512. if !target_arch_mips { // FIXME(#137)
  513. gen(|(a, b): (MyI128, MyI128)| {
  514. if b.0 == 0 {
  515. None
  516. } else {
  517. Some(a.0 / b.0)
  518. }
  519. },
  520. "compiler_builtins::int::sdiv::__divti3(a, b)");
  521. gen(|(a, b): (MyI128, MyI128)| {
  522. if b.0 == 0 {
  523. None
  524. } else {
  525. Some(a.0 % b.0)
  526. }
  527. },
  528. "compiler_builtins::int::sdiv::__modti3(a, b)");
  529. }
  530. // int/shift.rs
  531. gen(|(a, b): (MyU64, MyU32)| Some(a.0 << (b.0 % 64)),
  532. "compiler_builtins::int::shift::__ashldi3(a, b % 64)");
  533. gen(|(a, b): (MyU128, MyU32)| Some(a.0 << (b.0 % 128)),
  534. "compiler_builtins::int::shift::__ashlti3(a, b % 128)");
  535. gen(|(a, b): (MyI64, MyU32)| Some(a.0 >> (b.0 % 64)),
  536. "compiler_builtins::int::shift::__ashrdi3(a, b % 64)");
  537. gen(|(a, b): (MyI128, MyU32)| Some(a.0 >> (b.0 % 128)),
  538. "compiler_builtins::int::shift::__ashrti3(a, b % 128)");
  539. gen(|(a, b): (MyU64, MyU32)| Some(a.0 >> (b.0 % 64)),
  540. "compiler_builtins::int::shift::__lshrdi3(a, b % 64)");
  541. gen(|(a, b): (MyU128, MyU32)| Some(a.0 >> (b.0 % 128)),
  542. "compiler_builtins::int::shift::__lshrti3(a, b % 128)");
  543. // int/udiv.rs
  544. gen(|(a, b): (MyU64, MyU64)| {
  545. if b.0 == 0 {
  546. None
  547. } else {
  548. Some(a.0 / b.0)
  549. }
  550. },
  551. "compiler_builtins::int::udiv::__udivdi3(a, b)");
  552. gen(|(a, b): (MyU64, MyU64)| {
  553. if b.0 == 0 {
  554. None
  555. } else {
  556. Some((a.0 / b.0, a.0 % b.0))
  557. }
  558. },
  559. "{
  560. let mut r = 0;
  561. (compiler_builtins::int::udiv::__udivmoddi4(a, b, Some(&mut r)), r)
  562. }");
  563. gen(|(a, b): (MyU32, MyU32)| {
  564. if b.0 == 0 {
  565. None
  566. } else {
  567. Some((a.0 / b.0, a.0 % b.0))
  568. }
  569. },
  570. "{
  571. let mut r = 0;
  572. (compiler_builtins::int::udiv::__udivmodsi4(a, b, Some(&mut r)), r)
  573. }");
  574. gen(|(a, b): (MyU32, MyU32)| {
  575. if b.0 == 0 {
  576. None
  577. } else {
  578. Some(a.0 / b.0)
  579. }
  580. },
  581. "compiler_builtins::int::udiv::__udivsi3(a, b)");
  582. gen(|(a, b): (MyU32, MyU32)| {
  583. if b.0 == 0 {
  584. None
  585. } else {
  586. Some(a.0 % b.0)
  587. }
  588. },
  589. "compiler_builtins::int::udiv::__umodsi3(a, b)");
  590. gen(|(a, b): (MyU64, MyU64)| {
  591. if b.0 == 0 {
  592. None
  593. } else {
  594. Some(a.0 % b.0)
  595. }
  596. },
  597. "compiler_builtins::int::udiv::__umoddi3(a, b)");
  598. if !target_arch_mips { // FIXME(#137)
  599. gen(|(a, b): (MyU128, MyU128)| {
  600. if b.0 == 0 {
  601. None
  602. } else {
  603. Some(a.0 / b.0)
  604. }
  605. },
  606. "compiler_builtins::int::udiv::__udivti3(a, b)");
  607. gen(|(a, b): (MyU128, MyU128)| {
  608. if b.0 == 0 {
  609. None
  610. } else {
  611. Some(a.0 % b.0)
  612. }
  613. },
  614. "compiler_builtins::int::udiv::__umodti3(a, b)");
  615. gen(|(a, b): (MyU128, MyU128)| {
  616. if b.0 == 0 {
  617. None
  618. } else {
  619. Some((a.0 / b.0, a.0 % b.0))
  620. }
  621. },
  622. "{
  623. let mut r = 0;
  624. (compiler_builtins::int::udiv::__udivmodti4(a, b, Some(&mut r)), r)
  625. }");
  626. }
  627. }
  628. macro_rules! gen_float {
  629. ($name:ident,
  630. $fty:ident,
  631. $uty:ident,
  632. $bits:expr,
  633. $significand_bits:expr) => {
  634. pub fn $name<R>(rng: &mut R) -> $fty
  635. where
  636. R: Rng,
  637. {
  638. const BITS: u8 = $bits;
  639. const SIGNIFICAND_BITS: u8 = $significand_bits;
  640. const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
  641. const SIGN_MASK: $uty = (1 << (BITS - 1));
  642. const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
  643. fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
  644. unsafe {
  645. mem::transmute(((sign as $uty) << (BITS - 1)) |
  646. ((exponent & EXPONENT_MASK) <<
  647. SIGNIFICAND_BITS) |
  648. (significand & SIGNIFICAND_MASK))
  649. }
  650. }
  651. if rng.gen_weighted_bool(10) {
  652. // Special values
  653. *rng.choose(&[-0.0,
  654. 0.0,
  655. ::std::$fty::NAN,
  656. ::std::$fty::INFINITY,
  657. -::std::$fty::INFINITY])
  658. .unwrap()
  659. } else if rng.gen_weighted_bool(10) {
  660. // NaN patterns
  661. mk_f32(rng.gen(), rng.gen(), 0)
  662. } else if rng.gen() {
  663. // Denormalized
  664. mk_f32(rng.gen(), 0, rng.gen())
  665. } else {
  666. // Random anything
  667. mk_f32(rng.gen(), rng.gen(), rng.gen())
  668. }
  669. }
  670. }
  671. }
  672. gen_float!(gen_f32, f32, u32, 32, 23);
  673. gen_float!(gen_f64, f64, u64, 64, 52);
  674. macro_rules! gen_large_float {
  675. ($name:ident,
  676. $fty:ident,
  677. $uty:ident,
  678. $bits:expr,
  679. $significand_bits:expr) => {
  680. pub fn $name<R>(rng: &mut R) -> $fty
  681. where
  682. R: Rng,
  683. {
  684. const BITS: u8 = $bits;
  685. const SIGNIFICAND_BITS: u8 = $significand_bits;
  686. const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
  687. const SIGN_MASK: $uty = (1 << (BITS - 1));
  688. const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
  689. fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
  690. unsafe {
  691. mem::transmute(((sign as $uty) << (BITS - 1)) |
  692. ((exponent & EXPONENT_MASK) <<
  693. SIGNIFICAND_BITS) |
  694. (significand & SIGNIFICAND_MASK))
  695. }
  696. }
  697. if rng.gen_weighted_bool(10) {
  698. // Special values
  699. *rng.choose(&[-0.0,
  700. 0.0,
  701. ::std::$fty::NAN,
  702. ::std::$fty::INFINITY,
  703. -::std::$fty::INFINITY])
  704. .unwrap()
  705. } else if rng.gen_weighted_bool(10) {
  706. // NaN patterns
  707. mk_f32(rng.gen(), rng.gen(), 0)
  708. } else if rng.gen() {
  709. // Denormalized
  710. mk_f32(rng.gen(), 0, rng.gen())
  711. } else {
  712. // Random anything
  713. rng.gen::<$fty>()
  714. }
  715. }
  716. }
  717. }
  718. gen_large_float!(gen_large_f32, f32, u32, 32, 23);
  719. gen_large_float!(gen_large_f64, f64, u64, 64, 52);
  720. trait TestInput: rand::Rand + Hash + Eq + fmt::Debug {
  721. fn ty_name() -> String;
  722. fn generate_lets(container: &str, cnt: &mut u8) -> String;
  723. fn generate_static(&self, dst: &mut String);
  724. }
  725. trait TestOutput {
  726. fn ty_name() -> String;
  727. fn generate_static(&self, dst: &mut String);
  728. fn generate_expr(container: &str) -> String;
  729. }
  730. fn gen<F, A, R>(mut generate: F, test: &str)
  731. where F: FnMut(A) -> Option<R>,
  732. A: TestInput + Copy,
  733. R: TestOutput,
  734. {
  735. let rng = &mut rand::thread_rng();
  736. let testname = test.split("::")
  737. .last()
  738. .unwrap()
  739. .split("(")
  740. .next()
  741. .unwrap();
  742. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  743. let out_file = out_dir.join("generated.rs");
  744. let mut testcases = HashMap::new();
  745. let mut n = NTESTS;
  746. while n > 0 {
  747. let input: A = rng.gen();
  748. if testcases.contains_key(&input) {
  749. continue
  750. }
  751. let output = match generate(input) {
  752. Some(o) => o,
  753. None => continue,
  754. };
  755. testcases.insert(input, output);
  756. n -= 1;
  757. }
  758. let mut contents = String::new();
  759. contents.push_str(&format!("mod {} {{\nuse super::*;\n", testname));
  760. contents.push_str("#[test]\n");
  761. contents.push_str("fn test() {\n");
  762. contents.push_str(&format!("static TESTS: [({}, {}); {}] = [\n",
  763. A::ty_name(),
  764. R::ty_name(),
  765. NTESTS));
  766. for (input, output) in testcases {
  767. contents.push_str(" (");
  768. input.generate_static(&mut contents);
  769. contents.push_str(", ");
  770. output.generate_static(&mut contents);
  771. contents.push_str("),\n");
  772. }
  773. contents.push_str("];\n");
  774. contents.push_str(&format!(r#"
  775. for &(inputs, output) in TESTS.iter() {{
  776. {}
  777. assert_eq!({}, {}, "inputs {{:?}}", inputs)
  778. }}
  779. "#,
  780. A::generate_lets("inputs", &mut 0),
  781. R::generate_expr("output"),
  782. test,
  783. ));
  784. contents.push_str("\n}\n");
  785. contents.push_str("\n}\n");
  786. OpenOptions::new()
  787. .write(true)
  788. .append(true)
  789. .create(true)
  790. .open(out_file)
  791. .unwrap()
  792. .write_all(contents.as_bytes())
  793. .unwrap();
  794. }
  795. macro_rules! my_float {
  796. ($(struct $name:ident($inner:ident) = $gen:ident;)*) => ($(
  797. #[derive(Debug, Clone, Copy)]
  798. struct $name($inner);
  799. impl TestInput for $name {
  800. fn ty_name() -> String {
  801. format!("u{}", &stringify!($inner)[1..])
  802. }
  803. fn generate_lets(container: &str, cnt: &mut u8) -> String {
  804. let me = *cnt;
  805. *cnt += 1;
  806. format!("let {} = {}::from_bits({});\n",
  807. (b'a' + me) as char,
  808. stringify!($inner),
  809. container)
  810. }
  811. fn generate_static(&self, dst: &mut String) {
  812. write!(dst, "{}", self.0.to_bits()).unwrap();
  813. }
  814. }
  815. impl rand::Rand for $name {
  816. fn rand<R: rand::Rng>(r: &mut R) -> $name {
  817. $name($gen(r))
  818. }
  819. }
  820. impl Hash for $name {
  821. fn hash<H: Hasher>(&self, h: &mut H) {
  822. self.0.to_bits().hash(h)
  823. }
  824. }
  825. impl PartialEq for $name {
  826. fn eq(&self, other: &$name) -> bool {
  827. self.0.to_bits() == other.0.to_bits()
  828. }
  829. }
  830. impl Eq for $name {}
  831. )*)
  832. }
  833. my_float! {
  834. struct MyF64(f64) = gen_f64;
  835. struct LargeF64(f64) = gen_large_f64;
  836. struct MyF32(f32) = gen_f32;
  837. struct LargeF32(f32) = gen_large_f32;
  838. }
  839. macro_rules! my_integer {
  840. ($(struct $name:ident($inner:ident);)*) => ($(
  841. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
  842. struct $name($inner);
  843. impl TestInput for $name {
  844. fn ty_name() -> String {
  845. stringify!($inner).to_string()
  846. }
  847. fn generate_lets(container: &str, cnt: &mut u8) -> String {
  848. let me = *cnt;
  849. *cnt += 1;
  850. format!("let {} = {};\n",
  851. (b'a' + me) as char,
  852. container)
  853. }
  854. fn generate_static(&self, dst: &mut String) {
  855. write!(dst, "{}", self.0).unwrap();
  856. }
  857. }
  858. impl rand::Rand for $name {
  859. fn rand<R: rand::Rng>(rng: &mut R) -> $name {
  860. let bits = (0 as $inner).count_zeros();
  861. let mut mk = || {
  862. if rng.gen_weighted_bool(10) {
  863. *rng.choose(&[
  864. ::std::$inner::MAX >> (bits / 2),
  865. 0,
  866. ::std::$inner::MIN >> (bits / 2),
  867. ]).unwrap()
  868. } else {
  869. rng.gen::<$inner>()
  870. }
  871. };
  872. let a = mk();
  873. let b = mk();
  874. $name((a << (bits / 2)) | (b & (!0 << (bits / 2))))
  875. }
  876. }
  877. )*)
  878. }
  879. my_integer! {
  880. struct MyI32(i32);
  881. struct MyI64(i64);
  882. struct MyI128(i128);
  883. struct MyU32(u32);
  884. struct MyU64(u64);
  885. struct MyU128(u128);
  886. }
  887. impl<A, B> TestInput for (A, B)
  888. where A: TestInput,
  889. B: TestInput,
  890. {
  891. fn ty_name() -> String {
  892. format!("({}, {})", A::ty_name(), B::ty_name())
  893. }
  894. fn generate_lets(container: &str, cnt: &mut u8) -> String {
  895. format!("{}{}",
  896. A::generate_lets(&format!("{}.0", container), cnt),
  897. B::generate_lets(&format!("{}.1", container), cnt))
  898. }
  899. fn generate_static(&self, dst: &mut String) {
  900. dst.push_str("(");
  901. self.0.generate_static(dst);
  902. dst.push_str(", ");
  903. self.1.generate_static(dst);
  904. dst.push_str(")");
  905. }
  906. }
  907. impl TestOutput for f64 {
  908. fn ty_name() -> String {
  909. "u64".to_string()
  910. }
  911. fn generate_static(&self, dst: &mut String) {
  912. write!(dst, "{}", self.to_bits()).unwrap();
  913. }
  914. fn generate_expr(container: &str) -> String {
  915. format!("f64::from_bits({})", container)
  916. }
  917. }
  918. impl TestOutput for f32 {
  919. fn ty_name() -> String {
  920. "u32".to_string()
  921. }
  922. fn generate_static(&self, dst: &mut String) {
  923. write!(dst, "{}", self.to_bits()).unwrap();
  924. }
  925. fn generate_expr(container: &str) -> String {
  926. format!("f32::from_bits({})", container)
  927. }
  928. }
  929. macro_rules! plain_test_output {
  930. ($($i:tt)*) => ($(
  931. impl TestOutput for $i {
  932. fn ty_name() -> String {
  933. stringify!($i).to_string()
  934. }
  935. fn generate_static(&self, dst: &mut String) {
  936. write!(dst, "{}", self).unwrap();
  937. }
  938. fn generate_expr(container: &str) -> String {
  939. container.to_string()
  940. }
  941. }
  942. )*)
  943. }
  944. plain_test_output!(i32 i64 i128 u32 u64 u128 bool);
  945. impl<A, B> TestOutput for (A, B)
  946. where A: TestOutput,
  947. B: TestOutput,
  948. {
  949. fn ty_name() -> String {
  950. format!("({}, {})", A::ty_name(), B::ty_name())
  951. }
  952. fn generate_static(&self, dst: &mut String) {
  953. dst.push_str("(");
  954. self.0.generate_static(dst);
  955. dst.push_str(", ");
  956. self.1.generate_static(dst);
  957. dst.push_str(")");
  958. }
  959. fn generate_expr(container: &str) -> String {
  960. container.to_string()
  961. }
  962. }