build.rs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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/extend.rs
  213. gen(|a: MyF32| {
  214. if a.0.is_nan() {
  215. return None;
  216. }
  217. Some(f64(a.0))
  218. },
  219. "compiler_builtins::float::extend::__extendsfdf2(a)");
  220. // float/conv.rs
  221. gen(|a: MyF64| i64(a.0).ok(),
  222. "compiler_builtins::float::conv::__fixdfdi(a)");
  223. gen(|a: MyF64| i32(a.0).ok(),
  224. "compiler_builtins::float::conv::__fixdfsi(a)");
  225. gen(|a: MyF32| i64(a.0).ok(),
  226. "compiler_builtins::float::conv::__fixsfdi(a)");
  227. gen(|a: MyF32| i32(a.0).ok(),
  228. "compiler_builtins::float::conv::__fixsfsi(a)");
  229. gen(|a: MyF32| i128(a.0).ok(),
  230. "compiler_builtins::float::conv::__fixsfti(a)");
  231. gen(|a: MyF64| i128(a.0).ok(),
  232. "compiler_builtins::float::conv::__fixdfti(a)");
  233. gen(|a: MyF64| u64(a.0).ok(),
  234. "compiler_builtins::float::conv::__fixunsdfdi(a)");
  235. gen(|a: MyF64| u32(a.0).ok(),
  236. "compiler_builtins::float::conv::__fixunsdfsi(a)");
  237. gen(|a: MyF32| u64(a.0).ok(),
  238. "compiler_builtins::float::conv::__fixunssfdi(a)");
  239. gen(|a: MyF32| u32(a.0).ok(),
  240. "compiler_builtins::float::conv::__fixunssfsi(a)");
  241. gen(|a: MyF32| u128(a.0).ok(),
  242. "compiler_builtins::float::conv::__fixunssfti(a)");
  243. gen(|a: MyF64| u128(a.0).ok(),
  244. "compiler_builtins::float::conv::__fixunsdfti(a)");
  245. gen(|a: MyI64| Some(f64(a.0)),
  246. "compiler_builtins::float::conv::__floatdidf(a)");
  247. gen(|a: MyI32| Some(f64(a.0)),
  248. "compiler_builtins::float::conv::__floatsidf(a)");
  249. gen(|a: MyI32| Some(f32(a.0)),
  250. "compiler_builtins::float::conv::__floatsisf(a)");
  251. gen(|a: MyU64| Some(f64(a.0)),
  252. "compiler_builtins::float::conv::__floatundidf(a)");
  253. gen(|a: MyU32| Some(f64(a.0)),
  254. "compiler_builtins::float::conv::__floatunsidf(a)");
  255. gen(|a: MyU32| Some(f32(a.0)),
  256. "compiler_builtins::float::conv::__floatunsisf(a)");
  257. gen(|a: MyU128| f32(a.0).ok(),
  258. "compiler_builtins::float::conv::__floatuntisf(a)");
  259. if !target_arch_mips {
  260. gen(|a: MyI128| Some(f32(a.0)),
  261. "compiler_builtins::float::conv::__floattisf(a)");
  262. gen(|a: MyI128| Some(f64(a.0)),
  263. "compiler_builtins::float::conv::__floattidf(a)");
  264. gen(|a: MyU128| Some(f64(a.0)),
  265. "compiler_builtins::float::conv::__floatuntidf(a)");
  266. }
  267. // float/pow.rs
  268. gen(|(a, b): (MyF64, MyI32)| {
  269. let c = a.0.powi(b.0);
  270. if a.0.is_nan() || c.is_nan() {
  271. None
  272. } else {
  273. Some(c)
  274. }
  275. },
  276. "compiler_builtins::float::pow::__powidf2(a, b)");
  277. gen(|(a, b): (MyF32, MyI32)| {
  278. let c = a.0.powi(b.0);
  279. if a.0.is_nan() || c.is_nan() {
  280. None
  281. } else {
  282. Some(c)
  283. }
  284. },
  285. "compiler_builtins::float::pow::__powisf2(a, b)");
  286. // float/sub.rs
  287. gen(|(a, b): (MyF64, MyF64)| {
  288. let c = a.0 - b.0;
  289. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  290. None
  291. } else {
  292. Some(c)
  293. }
  294. },
  295. "compiler_builtins::float::sub::__subdf3(a, b)");
  296. gen(|(a, b): (MyF32, MyF32)| {
  297. let c = a.0 - b.0;
  298. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  299. None
  300. } else {
  301. Some(c)
  302. }
  303. },
  304. "compiler_builtins::float::sub::__subsf3(a, b)");
  305. if target_arch_arm {
  306. gen(|(a, b): (MyF64, MyF64)| {
  307. let c = a.0 - b.0;
  308. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  309. None
  310. } else {
  311. Some(c)
  312. }
  313. },
  314. "compiler_builtins::float::sub::__subdf3vfp(a, b)");
  315. gen(|(a, b): (LargeF32, LargeF32)| {
  316. let c = a.0 - b.0;
  317. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  318. None
  319. } else {
  320. Some(c)
  321. }
  322. },
  323. "compiler_builtins::float::sub::__subsf3vfp(a, b)");
  324. }
  325. // float/mul.rs
  326. gen(|(a, b): (MyF64, MyF64)| {
  327. let c = a.0 * b.0;
  328. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  329. None
  330. } else {
  331. Some(c)
  332. }
  333. },
  334. "compiler_builtins::float::mul::__muldf3(a, b)");
  335. gen(|(a, b): (LargeF32, LargeF32)| {
  336. let c = a.0 * b.0;
  337. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  338. None
  339. } else {
  340. Some(c)
  341. }
  342. },
  343. "compiler_builtins::float::mul::__mulsf3(a, b)");
  344. if target_arch_arm {
  345. gen(|(a, b): (MyF64, MyF64)| {
  346. let c = a.0 * b.0;
  347. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  348. None
  349. } else {
  350. Some(c)
  351. }
  352. },
  353. "compiler_builtins::float::mul::__muldf3vfp(a, b)");
  354. gen(|(a, b): (LargeF32, LargeF32)| {
  355. let c = a.0 * b.0;
  356. if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
  357. None
  358. } else {
  359. Some(c)
  360. }
  361. },
  362. "compiler_builtins::float::mul::__mulsf3vfp(a, b)");
  363. }
  364. // float/div.rs
  365. gen(|(a, b): (MyF64, MyF64)| {
  366. if b.0 == 0.0 {
  367. return None
  368. }
  369. let c = a.0 / b.0;
  370. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  371. c.abs() <= unsafe { mem::transmute(4503599627370495u64) }
  372. {
  373. None
  374. } else {
  375. Some(c)
  376. }
  377. },
  378. "compiler_builtins::float::div::__divdf3(a, b)");
  379. gen(|(a, b): (LargeF32, LargeF32)| {
  380. if b.0 == 0.0 {
  381. return None
  382. }
  383. let c = a.0 / b.0;
  384. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  385. c.abs() <= unsafe { mem::transmute(16777215u32) }
  386. {
  387. None
  388. } else {
  389. Some(c)
  390. }
  391. },
  392. "compiler_builtins::float::div::__divsf3(a, b)");
  393. if target_arch_arm {
  394. gen(|(a, b): (MyF64, MyF64)| {
  395. if b.0 == 0.0 {
  396. return None
  397. }
  398. let c = a.0 / b.0;
  399. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  400. c.abs() <= unsafe { mem::transmute(4503599627370495u64) }
  401. {
  402. None
  403. } else {
  404. Some(c)
  405. }
  406. },
  407. "compiler_builtins::float::div::__divdf3vfp(a, b)");
  408. gen(|(a, b): (LargeF32, LargeF32)| {
  409. if b.0 == 0.0 {
  410. return None
  411. }
  412. let c = a.0 / b.0;
  413. if a.0.is_nan() || b.0.is_nan() || c.is_nan() ||
  414. c.abs() <= unsafe { mem::transmute(16777215u32) }
  415. {
  416. None
  417. } else {
  418. Some(c)
  419. }
  420. },
  421. "compiler_builtins::float::div::__divsf3vfp(a, b)");
  422. }
  423. // int/addsub.rs
  424. gen(|(a, b): (MyU128, MyU128)| Some(a.0.wrapping_add(b.0)),
  425. "compiler_builtins::int::addsub::rust_u128_add(a, b)");
  426. gen(|(a, b): (MyI128, MyI128)| Some(a.0.wrapping_add(b.0)),
  427. "compiler_builtins::int::addsub::rust_i128_add(a, b)");
  428. gen(|(a, b): (MyU128, MyU128)| Some(a.0.overflowing_add(b.0)),
  429. "compiler_builtins::int::addsub::rust_u128_addo(a, b)");
  430. gen(|(a, b): (MyI128, MyI128)| Some(a.0.overflowing_add(b.0)),
  431. "compiler_builtins::int::addsub::rust_i128_addo(a, b)");
  432. gen(|(a, b): (MyU128, MyU128)| Some(a.0.wrapping_sub(b.0)),
  433. "compiler_builtins::int::addsub::rust_u128_sub(a, b)");
  434. gen(|(a, b): (MyI128, MyI128)| Some(a.0.wrapping_sub(b.0)),
  435. "compiler_builtins::int::addsub::rust_i128_sub(a, b)");
  436. gen(|(a, b): (MyU128, MyU128)| Some(a.0.overflowing_sub(b.0)),
  437. "compiler_builtins::int::addsub::rust_u128_subo(a, b)");
  438. gen(|(a, b): (MyI128, MyI128)| Some(a.0.overflowing_sub(b.0)),
  439. "compiler_builtins::int::addsub::rust_i128_subo(a, b)");
  440. // int/mul.rs
  441. gen(|(a, b): (MyU64, MyU64)| Some(a.0.wrapping_mul(b.0)),
  442. "compiler_builtins::int::mul::__muldi3(a, b)");
  443. gen(|(a, b): (MyI64, MyI64)| Some(a.0.overflowing_mul(b.0)),
  444. "{
  445. let mut o = 2;
  446. let c = compiler_builtins::int::mul::__mulodi4(a, b, &mut o);
  447. (c, match o { 0 => false, 1 => true, _ => panic!() })
  448. }");
  449. gen(|(a, b): (MyI32, MyI32)| Some(a.0.overflowing_mul(b.0)),
  450. "{
  451. let mut o = 2;
  452. let c = compiler_builtins::int::mul::__mulosi4(a, b, &mut o);
  453. (c, match o { 0 => false, 1 => true, _ => panic!() })
  454. }");
  455. gen(|(a, b): (MyI128, MyI128)| Some(a.0.wrapping_mul(b.0)),
  456. "compiler_builtins::int::mul::__multi3(a, b)");
  457. if !target_arch_mips { // FIXME(#137)
  458. gen(|(a, b): (MyI128, MyI128)| Some(a.0.overflowing_mul(b.0)),
  459. "{
  460. let mut o = 2;
  461. let c = compiler_builtins::int::mul::__muloti4(a, b, &mut o);
  462. (c, match o { 0 => false, 1 => true, _ => panic!() })
  463. }");
  464. }
  465. // int/sdiv.rs
  466. gen(|(a, b): (MyI64, MyI64)| {
  467. if b.0 == 0 {
  468. None
  469. } else {
  470. Some(a.0 / b.0)
  471. }
  472. },
  473. "compiler_builtins::int::sdiv::__divdi3(a, b)");
  474. gen(|(a, b): (MyI64, MyI64)| {
  475. if b.0 == 0 {
  476. None
  477. } else {
  478. Some((a.0 / b.0, a.0 % b.0))
  479. }
  480. },
  481. "{
  482. let mut r = 0;
  483. (compiler_builtins::int::sdiv::__divmoddi4(a, b, &mut r), r)
  484. }");
  485. gen(|(a, b): (MyI32, MyI32)| {
  486. if b.0 == 0 {
  487. None
  488. } else {
  489. Some((a.0 / b.0, a.0 % b.0))
  490. }
  491. },
  492. "{
  493. let mut r = 0;
  494. (compiler_builtins::int::sdiv::__divmodsi4(a, b, &mut r), r)
  495. }");
  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::__divsi3(a, b)");
  504. gen(|(a, b): (MyI32, MyI32)| {
  505. if b.0 == 0 {
  506. None
  507. } else {
  508. Some(a.0 % b.0)
  509. }
  510. },
  511. "compiler_builtins::int::sdiv::__modsi3(a, b)");
  512. gen(|(a, b): (MyI64, MyI64)| {
  513. if b.0 == 0 {
  514. None
  515. } else {
  516. Some(a.0 % b.0)
  517. }
  518. },
  519. "compiler_builtins::int::sdiv::__moddi3(a, b)");
  520. if !target_arch_mips { // FIXME(#137)
  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::__divti3(a, b)");
  529. gen(|(a, b): (MyI128, MyI128)| {
  530. if b.0 == 0 {
  531. None
  532. } else {
  533. Some(a.0 % b.0)
  534. }
  535. },
  536. "compiler_builtins::int::sdiv::__modti3(a, b)");
  537. }
  538. // int/shift.rs
  539. gen(|(a, b): (MyU64, MyU32)| Some(a.0 << (b.0 % 64)),
  540. "compiler_builtins::int::shift::__ashldi3(a, b % 64)");
  541. gen(|(a, b): (MyU128, MyU32)| Some(a.0 << (b.0 % 128)),
  542. "compiler_builtins::int::shift::__ashlti3(a, b % 128)");
  543. gen(|(a, b): (MyI64, MyU32)| Some(a.0 >> (b.0 % 64)),
  544. "compiler_builtins::int::shift::__ashrdi3(a, b % 64)");
  545. gen(|(a, b): (MyI128, MyU32)| Some(a.0 >> (b.0 % 128)),
  546. "compiler_builtins::int::shift::__ashrti3(a, b % 128)");
  547. gen(|(a, b): (MyU64, MyU32)| Some(a.0 >> (b.0 % 64)),
  548. "compiler_builtins::int::shift::__lshrdi3(a, b % 64)");
  549. gen(|(a, b): (MyU128, MyU32)| Some(a.0 >> (b.0 % 128)),
  550. "compiler_builtins::int::shift::__lshrti3(a, b % 128)");
  551. // int/udiv.rs
  552. gen(|(a, b): (MyU64, MyU64)| {
  553. if b.0 == 0 {
  554. None
  555. } else {
  556. Some(a.0 / b.0)
  557. }
  558. },
  559. "compiler_builtins::int::udiv::__udivdi3(a, b)");
  560. gen(|(a, b): (MyU64, MyU64)| {
  561. if b.0 == 0 {
  562. None
  563. } else {
  564. Some((a.0 / b.0, a.0 % b.0))
  565. }
  566. },
  567. "{
  568. let mut r = 0;
  569. (compiler_builtins::int::udiv::__udivmoddi4(a, b, Some(&mut r)), r)
  570. }");
  571. gen(|(a, b): (MyU32, MyU32)| {
  572. if b.0 == 0 {
  573. None
  574. } else {
  575. Some((a.0 / b.0, a.0 % b.0))
  576. }
  577. },
  578. "{
  579. let mut r = 0;
  580. (compiler_builtins::int::udiv::__udivmodsi4(a, b, Some(&mut r)), r)
  581. }");
  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::__udivsi3(a, b)");
  590. gen(|(a, b): (MyU32, MyU32)| {
  591. if b.0 == 0 {
  592. None
  593. } else {
  594. Some(a.0 % b.0)
  595. }
  596. },
  597. "compiler_builtins::int::udiv::__umodsi3(a, b)");
  598. gen(|(a, b): (MyU64, MyU64)| {
  599. if b.0 == 0 {
  600. None
  601. } else {
  602. Some(a.0 % b.0)
  603. }
  604. },
  605. "compiler_builtins::int::udiv::__umoddi3(a, b)");
  606. if !target_arch_mips { // FIXME(#137)
  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::__udivti3(a, b)");
  615. gen(|(a, b): (MyU128, MyU128)| {
  616. if b.0 == 0 {
  617. None
  618. } else {
  619. Some(a.0 % b.0)
  620. }
  621. },
  622. "compiler_builtins::int::udiv::__umodti3(a, b)");
  623. gen(|(a, b): (MyU128, MyU128)| {
  624. if b.0 == 0 {
  625. None
  626. } else {
  627. Some((a.0 / b.0, a.0 % b.0))
  628. }
  629. },
  630. "{
  631. let mut r = 0;
  632. (compiler_builtins::int::udiv::__udivmodti4(a, b, Some(&mut r)), r)
  633. }");
  634. }
  635. }
  636. macro_rules! gen_float {
  637. ($name:ident,
  638. $fty:ident,
  639. $uty:ident,
  640. $bits:expr,
  641. $significand_bits:expr) => {
  642. pub fn $name<R>(rng: &mut R) -> $fty
  643. where
  644. R: Rng,
  645. {
  646. const BITS: u8 = $bits;
  647. const SIGNIFICAND_BITS: u8 = $significand_bits;
  648. const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
  649. const SIGN_MASK: $uty = (1 << (BITS - 1));
  650. const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
  651. fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
  652. unsafe {
  653. mem::transmute(((sign as $uty) << (BITS - 1)) |
  654. ((exponent & EXPONENT_MASK) <<
  655. SIGNIFICAND_BITS) |
  656. (significand & SIGNIFICAND_MASK))
  657. }
  658. }
  659. if rng.gen_weighted_bool(10) {
  660. // Special values
  661. *rng.choose(&[-0.0,
  662. 0.0,
  663. ::std::$fty::MIN,
  664. ::std::$fty::MIN_POSITIVE,
  665. ::std::$fty::MAX,
  666. ::std::$fty::NAN,
  667. ::std::$fty::INFINITY,
  668. -::std::$fty::INFINITY])
  669. .unwrap()
  670. } else if rng.gen_weighted_bool(10) {
  671. // NaN patterns
  672. mk_f32(rng.gen(), rng.gen(), 0)
  673. } else if rng.gen() {
  674. // Denormalized
  675. mk_f32(rng.gen(), 0, rng.gen())
  676. } else {
  677. // Random anything
  678. mk_f32(rng.gen(), rng.gen(), rng.gen())
  679. }
  680. }
  681. }
  682. }
  683. gen_float!(gen_f32, f32, u32, 32, 23);
  684. gen_float!(gen_f64, f64, u64, 64, 52);
  685. macro_rules! gen_large_float {
  686. ($name:ident,
  687. $fty:ident,
  688. $uty:ident,
  689. $bits:expr,
  690. $significand_bits:expr) => {
  691. pub fn $name<R>(rng: &mut R) -> $fty
  692. where
  693. R: Rng,
  694. {
  695. const BITS: u8 = $bits;
  696. const SIGNIFICAND_BITS: u8 = $significand_bits;
  697. const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
  698. const SIGN_MASK: $uty = (1 << (BITS - 1));
  699. const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
  700. fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
  701. unsafe {
  702. mem::transmute(((sign as $uty) << (BITS - 1)) |
  703. ((exponent & EXPONENT_MASK) <<
  704. SIGNIFICAND_BITS) |
  705. (significand & SIGNIFICAND_MASK))
  706. }
  707. }
  708. if rng.gen_weighted_bool(10) {
  709. // Special values
  710. *rng.choose(&[-0.0,
  711. 0.0,
  712. ::std::$fty::MIN,
  713. ::std::$fty::MIN_POSITIVE,
  714. ::std::$fty::MAX,
  715. ::std::$fty::NAN,
  716. ::std::$fty::INFINITY,
  717. -::std::$fty::INFINITY])
  718. .unwrap()
  719. } else if rng.gen_weighted_bool(10) {
  720. // NaN patterns
  721. mk_f32(rng.gen(), rng.gen(), 0)
  722. } else if rng.gen() {
  723. // Denormalized
  724. mk_f32(rng.gen(), 0, rng.gen())
  725. } else {
  726. // Random anything
  727. rng.gen::<$fty>()
  728. }
  729. }
  730. }
  731. }
  732. gen_large_float!(gen_large_f32, f32, u32, 32, 23);
  733. gen_large_float!(gen_large_f64, f64, u64, 64, 52);
  734. trait TestInput: rand::Rand + Hash + Eq + fmt::Debug {
  735. fn ty_name() -> String;
  736. fn generate_lets(container: &str, cnt: &mut u8) -> String;
  737. fn generate_static(&self, dst: &mut String);
  738. }
  739. trait TestOutput {
  740. fn ty_name() -> String;
  741. fn generate_static(&self, dst: &mut String);
  742. fn generate_expr(container: &str) -> String;
  743. }
  744. fn gen<F, A, R>(mut generate: F, test: &str)
  745. where F: FnMut(A) -> Option<R>,
  746. A: TestInput + Copy,
  747. R: TestOutput,
  748. {
  749. let rng = &mut rand::thread_rng();
  750. let testname = test.split("::")
  751. .last()
  752. .unwrap()
  753. .split("(")
  754. .next()
  755. .unwrap();
  756. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  757. let out_file = out_dir.join("generated.rs");
  758. let mut testcases = HashMap::new();
  759. let mut n = NTESTS;
  760. while n > 0 {
  761. let input: A = rng.gen();
  762. if testcases.contains_key(&input) {
  763. continue
  764. }
  765. let output = match generate(input) {
  766. Some(o) => o,
  767. None => continue,
  768. };
  769. testcases.insert(input, output);
  770. n -= 1;
  771. }
  772. let mut contents = String::new();
  773. contents.push_str(&format!("mod {} {{\nuse super::*;\n", testname));
  774. contents.push_str("#[test]\n");
  775. contents.push_str("fn test() {\n");
  776. contents.push_str(&format!("static TESTS: [({}, {}); {}] = [\n",
  777. A::ty_name(),
  778. R::ty_name(),
  779. NTESTS));
  780. for (input, output) in testcases {
  781. contents.push_str(" (");
  782. input.generate_static(&mut contents);
  783. contents.push_str(", ");
  784. output.generate_static(&mut contents);
  785. contents.push_str("),\n");
  786. }
  787. contents.push_str("];\n");
  788. contents.push_str(&format!(r#"
  789. for &(inputs, output) in TESTS.iter() {{
  790. {}
  791. assert_eq!({}, {}, "inputs {{:?}}", inputs)
  792. }}
  793. "#,
  794. A::generate_lets("inputs", &mut 0),
  795. R::generate_expr("output"),
  796. test,
  797. ));
  798. contents.push_str("\n}\n");
  799. contents.push_str("\n}\n");
  800. OpenOptions::new()
  801. .write(true)
  802. .append(true)
  803. .create(true)
  804. .open(out_file)
  805. .unwrap()
  806. .write_all(contents.as_bytes())
  807. .unwrap();
  808. }
  809. macro_rules! my_float {
  810. ($(struct $name:ident($inner:ident) = $gen:ident;)*) => ($(
  811. #[derive(Debug, Clone, Copy)]
  812. struct $name($inner);
  813. impl TestInput for $name {
  814. fn ty_name() -> String {
  815. format!("u{}", &stringify!($inner)[1..])
  816. }
  817. fn generate_lets(container: &str, cnt: &mut u8) -> String {
  818. let me = *cnt;
  819. *cnt += 1;
  820. format!("let {} = {}::from_bits({});\n",
  821. (b'a' + me) as char,
  822. stringify!($inner),
  823. container)
  824. }
  825. fn generate_static(&self, dst: &mut String) {
  826. write!(dst, "{}", self.0.to_bits()).unwrap();
  827. }
  828. }
  829. impl rand::Rand for $name {
  830. fn rand<R: rand::Rng>(r: &mut R) -> $name {
  831. $name($gen(r))
  832. }
  833. }
  834. impl Hash for $name {
  835. fn hash<H: Hasher>(&self, h: &mut H) {
  836. self.0.to_bits().hash(h)
  837. }
  838. }
  839. impl PartialEq for $name {
  840. fn eq(&self, other: &$name) -> bool {
  841. self.0.to_bits() == other.0.to_bits()
  842. }
  843. }
  844. impl Eq for $name {}
  845. )*)
  846. }
  847. my_float! {
  848. struct MyF64(f64) = gen_f64;
  849. struct LargeF64(f64) = gen_large_f64;
  850. struct MyF32(f32) = gen_f32;
  851. struct LargeF32(f32) = gen_large_f32;
  852. }
  853. macro_rules! my_integer {
  854. ($(struct $name:ident($inner:ident);)*) => ($(
  855. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
  856. struct $name($inner);
  857. impl TestInput for $name {
  858. fn ty_name() -> String {
  859. stringify!($inner).to_string()
  860. }
  861. fn generate_lets(container: &str, cnt: &mut u8) -> String {
  862. let me = *cnt;
  863. *cnt += 1;
  864. format!("let {} = {};\n",
  865. (b'a' + me) as char,
  866. container)
  867. }
  868. fn generate_static(&self, dst: &mut String) {
  869. write!(dst, "{}", self.0).unwrap();
  870. }
  871. }
  872. impl rand::Rand for $name {
  873. fn rand<R: rand::Rng>(rng: &mut R) -> $name {
  874. let bits = (0 as $inner).count_zeros();
  875. let mut mk = || {
  876. if rng.gen_weighted_bool(10) {
  877. *rng.choose(&[
  878. ::std::$inner::MAX >> (bits / 2),
  879. 0,
  880. ::std::$inner::MIN >> (bits / 2),
  881. ]).unwrap()
  882. } else {
  883. rng.gen::<$inner>()
  884. }
  885. };
  886. let a = mk();
  887. let b = mk();
  888. $name((a << (bits / 2)) | (b & (!0 << (bits / 2))))
  889. }
  890. }
  891. )*)
  892. }
  893. my_integer! {
  894. struct MyI32(i32);
  895. struct MyI64(i64);
  896. struct MyI128(i128);
  897. struct MyU32(u32);
  898. struct MyU64(u64);
  899. struct MyU128(u128);
  900. }
  901. impl<A, B> TestInput for (A, B)
  902. where A: TestInput,
  903. B: TestInput,
  904. {
  905. fn ty_name() -> String {
  906. format!("({}, {})", A::ty_name(), B::ty_name())
  907. }
  908. fn generate_lets(container: &str, cnt: &mut u8) -> String {
  909. format!("{}{}",
  910. A::generate_lets(&format!("{}.0", container), cnt),
  911. B::generate_lets(&format!("{}.1", container), cnt))
  912. }
  913. fn generate_static(&self, dst: &mut String) {
  914. dst.push_str("(");
  915. self.0.generate_static(dst);
  916. dst.push_str(", ");
  917. self.1.generate_static(dst);
  918. dst.push_str(")");
  919. }
  920. }
  921. impl TestOutput for f64 {
  922. fn ty_name() -> String {
  923. "u64".to_string()
  924. }
  925. fn generate_static(&self, dst: &mut String) {
  926. write!(dst, "{}", self.to_bits()).unwrap();
  927. }
  928. fn generate_expr(container: &str) -> String {
  929. format!("f64::from_bits({})", container)
  930. }
  931. }
  932. impl TestOutput for f32 {
  933. fn ty_name() -> String {
  934. "u32".to_string()
  935. }
  936. fn generate_static(&self, dst: &mut String) {
  937. write!(dst, "{}", self.to_bits()).unwrap();
  938. }
  939. fn generate_expr(container: &str) -> String {
  940. format!("f32::from_bits({})", container)
  941. }
  942. }
  943. macro_rules! plain_test_output {
  944. ($($i:tt)*) => ($(
  945. impl TestOutput for $i {
  946. fn ty_name() -> String {
  947. stringify!($i).to_string()
  948. }
  949. fn generate_static(&self, dst: &mut String) {
  950. write!(dst, "{}", self).unwrap();
  951. }
  952. fn generate_expr(container: &str) -> String {
  953. container.to_string()
  954. }
  955. }
  956. )*)
  957. }
  958. plain_test_output!(i32 i64 i128 u32 u64 u128 bool);
  959. impl<A, B> TestOutput for (A, B)
  960. where A: TestOutput,
  961. B: TestOutput,
  962. {
  963. fn ty_name() -> String {
  964. format!("({}, {})", A::ty_name(), B::ty_name())
  965. }
  966. fn generate_static(&self, dst: &mut String) {
  967. dst.push_str("(");
  968. self.0.generate_static(dst);
  969. dst.push_str(", ");
  970. self.1.generate_static(dst);
  971. dst.push_str(")");
  972. }
  973. fn generate_expr(container: &str) -> String {
  974. container.to_string()
  975. }
  976. }