build.rs 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978
  1. #![feature(i128_type)]
  2. use std::env;
  3. fn main() {
  4. println!("cargo:rerun-if-changed=build.rs");
  5. let target = env::var("TARGET").unwrap();
  6. // Emscripten's runtime includes all the builtins
  7. if target.contains("emscripten") {
  8. return;
  9. }
  10. // NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
  11. // target triple. This is usually correct for our built-in targets but can break in presence of
  12. // custom targets, which can have arbitrary names.
  13. let llvm_target = target.split('-').collect::<Vec<_>>();
  14. // Build test files
  15. tests::generate();
  16. // Build missing intrinsics from compiler-rt C source code
  17. #[cfg(feature = "c")]
  18. c::compile(&llvm_target);
  19. // To compile intrinsics.rs for thumb targets, where there is no libc
  20. if llvm_target[0].starts_with("thumb") {
  21. println!("cargo:rustc-cfg=thumb")
  22. }
  23. // compiler-rt `cfg`s away some intrinsics for thumbv6m because that target doesn't have full
  24. // THUMBv2 support. We have to cfg our code accordingly.
  25. if llvm_target[0] == "thumbv6m" {
  26. println!("cargo:rustc-cfg=thumbv6m")
  27. }
  28. }
  29. mod tests {
  30. extern crate rand;
  31. use std::collections::HashSet;
  32. use std::fmt::Write;
  33. use std::fs::File;
  34. use std::hash::Hash;
  35. use std::path::PathBuf;
  36. use std::{env, mem};
  37. use self::rand::Rng;
  38. const NTESTS: usize = 10_000;
  39. macro_rules! test {
  40. ($($intrinsic:ident,)+) => {
  41. $(
  42. mk_file::<$intrinsic>();
  43. )+
  44. }
  45. }
  46. pub fn generate() {
  47. // TODO move to main
  48. test! {
  49. // float/add.rs
  50. Adddf3,
  51. Addsf3,
  52. // float/conv.rs
  53. // Fixdfdi,
  54. // Fixdfsi,
  55. // Fixsfdi,
  56. // Fixsfsi,
  57. // Fixunsdfdi,
  58. // Fixunsdfsi,
  59. // Fixunssfdi,
  60. // Fixunssfsi,
  61. // Floatdidf,
  62. // Floatsidf,
  63. // Floatsisf,
  64. // Floatundidf,
  65. // Floatunsidf,
  66. // Floatunsisf,
  67. // float/pow.rs
  68. Powidf2,
  69. Powisf2,
  70. // float/sub.rs
  71. Subdf3,
  72. Subsf3,
  73. // int/mul.rs
  74. Muldi3,
  75. Mulodi4,
  76. Mulosi4,
  77. Muloti4,
  78. Multi3,
  79. // int/sdiv.rs
  80. Divdi3,
  81. Divmoddi4,
  82. Divmodsi4,
  83. Divsi3,
  84. Divti3,
  85. Moddi3,
  86. Modsi3,
  87. Modti3,
  88. // int/shift.rs
  89. Ashldi3,
  90. Ashlti3,
  91. Ashrdi3,
  92. Ashrti3,
  93. Lshrdi3,
  94. Lshrti3,
  95. // int/udiv.rs
  96. Udivdi3,
  97. Udivmoddi4,
  98. Udivmodsi4,
  99. Udivmodti4,
  100. Udivsi3,
  101. Udivti3,
  102. Umoddi3,
  103. Umodsi3,
  104. Umodti3,
  105. }
  106. }
  107. #[derive(Eq, Hash, PartialEq)]
  108. pub struct Adddf3 {
  109. a: u64,
  110. b: u64,
  111. c: u64,
  112. }
  113. impl TestCase for Adddf3 {
  114. fn name() -> &'static str {
  115. "adddf3"
  116. }
  117. fn generate<R>(rng: &mut R) -> Option<Self>
  118. where
  119. R: Rng,
  120. Self: Sized,
  121. {
  122. let a = gen_f64(rng);
  123. let b = gen_f64(rng);
  124. let c = a + b;
  125. // TODO accept NaNs. We don't do that right now because we can't check
  126. // for NaN-ness on the thumb targets (due to missing intrinsics)
  127. if a.is_nan() || b.is_nan() || c.is_nan() {
  128. return None;
  129. }
  130. Some(
  131. Adddf3 {
  132. a: to_u64(a),
  133. b: to_u64(b),
  134. c: to_u64(c),
  135. },
  136. )
  137. }
  138. fn to_string(&self, buffer: &mut String) {
  139. writeln!(
  140. buffer,
  141. "(({a}, {b}), {c}),",
  142. a = self.a,
  143. b = self.b,
  144. c = self.c
  145. )
  146. .unwrap();
  147. }
  148. fn prologue() -> &'static str {
  149. r#"
  150. #[cfg(all(target_arch = "arm",
  151. not(any(target_env = "gnu", target_env = "musl")),
  152. target_os = "linux",
  153. test))]
  154. use core::mem;
  155. #[cfg(not(all(target_arch = "arm",
  156. not(any(target_env = "gnu", target_env = "musl")),
  157. target_os = "linux",
  158. test)))]
  159. use std::mem;
  160. use compiler_builtins::float::add::__adddf3;
  161. fn mk_f64(x: u64) -> f64 {
  162. unsafe { mem::transmute(x) }
  163. }
  164. fn to_u64(x: f64) -> u64 {
  165. unsafe { mem::transmute(x) }
  166. }
  167. static TEST_CASES: &[((u64, u64), u64)] = &[
  168. "#
  169. }
  170. fn epilogue() -> &'static str {
  171. "
  172. ];
  173. #[test]
  174. fn adddf3() {
  175. for &((a, b), c) in TEST_CASES {
  176. let c_ = __adddf3(mk_f64(a), mk_f64(b));
  177. assert_eq!(((a, b), c), ((a, b), to_u64(c_)));
  178. }
  179. }
  180. "
  181. }
  182. }
  183. #[derive(Eq, Hash, PartialEq)]
  184. pub struct Addsf3 {
  185. a: u32,
  186. b: u32,
  187. c: u32,
  188. }
  189. impl TestCase for Addsf3 {
  190. fn name() -> &'static str {
  191. "addsf3"
  192. }
  193. fn generate<R>(rng: &mut R) -> Option<Self>
  194. where
  195. R: Rng,
  196. Self: Sized,
  197. {
  198. let a = gen_f32(rng);
  199. let b = gen_f32(rng);
  200. let c = a + b;
  201. // TODO accept NaNs. We don't do that right now because we can't check
  202. // for NaN-ness on the thumb targets (due to missing intrinsics)
  203. if a.is_nan() || b.is_nan() || c.is_nan() {
  204. return None;
  205. }
  206. Some(
  207. Addsf3 {
  208. a: to_u32(a),
  209. b: to_u32(b),
  210. c: to_u32(c),
  211. },
  212. )
  213. }
  214. fn to_string(&self, buffer: &mut String) {
  215. writeln!(
  216. buffer,
  217. "(({a}, {b}), {c}),",
  218. a = self.a,
  219. b = self.b,
  220. c = self.c
  221. )
  222. .unwrap();
  223. }
  224. fn prologue() -> &'static str {
  225. r#"
  226. #[cfg(all(target_arch = "arm",
  227. not(any(target_env = "gnu", target_env = "musl")),
  228. target_os = "linux",
  229. test))]
  230. use core::mem;
  231. #[cfg(not(all(target_arch = "arm",
  232. not(any(target_env = "gnu", target_env = "musl")),
  233. target_os = "linux",
  234. test)))]
  235. use std::mem;
  236. use compiler_builtins::float::add::__addsf3;
  237. fn mk_f32(x: u32) -> f32 {
  238. unsafe { mem::transmute(x) }
  239. }
  240. fn to_u32(x: f32) -> u32 {
  241. unsafe { mem::transmute(x) }
  242. }
  243. static TEST_CASES: &[((u32, u32), u32)] = &[
  244. "#
  245. }
  246. fn epilogue() -> &'static str {
  247. "
  248. ];
  249. #[test]
  250. fn addsf3() {
  251. for &((a, b), c) in TEST_CASES {
  252. let c_ = __addsf3(mk_f32(a), mk_f32(b));
  253. assert_eq!(((a, b), c), ((a, b), to_u32(c_)));
  254. }
  255. }
  256. "
  257. }
  258. }
  259. #[derive(Eq, Hash, PartialEq)]
  260. pub struct Ashldi3 {
  261. a: u64,
  262. b: u32,
  263. c: u64,
  264. }
  265. impl TestCase for Ashldi3 {
  266. fn name() -> &'static str {
  267. "ashldi3"
  268. }
  269. fn generate<R>(rng: &mut R) -> Option<Self>
  270. where
  271. R: Rng,
  272. Self: Sized,
  273. {
  274. let a = gen_u64(rng);
  275. let b = (rng.gen::<u8>() % 64) as u32;
  276. let c = a << b;
  277. Some(Ashldi3 { a, b, c })
  278. }
  279. fn to_string(&self, buffer: &mut String) {
  280. writeln!(
  281. buffer,
  282. "(({a}, {b}), {c}),",
  283. a = self.a,
  284. b = self.b,
  285. c = self.c
  286. )
  287. .unwrap();
  288. }
  289. fn prologue() -> &'static str {
  290. "
  291. use compiler_builtins::int::shift::__ashldi3;
  292. static TEST_CASES: &[((u64, u32), u64)] = &[
  293. "
  294. }
  295. fn epilogue() -> &'static str {
  296. "
  297. ];
  298. #[test]
  299. fn ashldi3() {
  300. for &((a, b), c) in TEST_CASES {
  301. let c_ = __ashldi3(a, b);
  302. assert_eq!(((a, b), c), ((a, b), c_));
  303. }
  304. }
  305. "
  306. }
  307. }
  308. #[derive(Eq, Hash, PartialEq)]
  309. pub struct Ashlti3 {
  310. a: u128,
  311. b: u32,
  312. c: u128,
  313. }
  314. impl TestCase for Ashlti3 {
  315. fn name() -> &'static str {
  316. "ashlti3"
  317. }
  318. fn generate<R>(rng: &mut R) -> Option<Self>
  319. where
  320. R: Rng,
  321. Self: Sized,
  322. {
  323. let a = gen_u128(rng);
  324. let b = (rng.gen::<u8>() % 128) as u32;
  325. let c = a << b;
  326. Some(Ashlti3 { a, b, c })
  327. }
  328. fn to_string(&self, buffer: &mut String) {
  329. writeln!(
  330. buffer,
  331. "(({a}, {b}), {c}),",
  332. a = self.a,
  333. b = self.b,
  334. c = self.c
  335. )
  336. .unwrap();
  337. }
  338. fn prologue() -> &'static str {
  339. "
  340. use compiler_builtins::int::shift::__ashlti3;
  341. static TEST_CASES: &[((u128, u32), u128)] = &[
  342. "
  343. }
  344. fn epilogue() -> &'static str {
  345. "
  346. ];
  347. #[test]
  348. fn ashlti3() {
  349. for &((a, b), c) in TEST_CASES {
  350. let c_ = __ashlti3(a, b);
  351. assert_eq!(((a, b), c), ((a, b), c_));
  352. }
  353. }
  354. "
  355. }
  356. }
  357. #[derive(Eq, Hash, PartialEq)]
  358. pub struct Ashrdi3 {
  359. a: i64,
  360. b: u32,
  361. c: i64,
  362. }
  363. impl TestCase for Ashrdi3 {
  364. fn name() -> &'static str {
  365. "ashrdi3"
  366. }
  367. fn generate<R>(rng: &mut R) -> Option<Self>
  368. where
  369. R: Rng,
  370. Self: Sized,
  371. {
  372. let a = gen_i64(rng);
  373. let b = (rng.gen::<u8>() % 64) as u32;
  374. let c = a >> b;
  375. Some(Ashrdi3 { a, b, c })
  376. }
  377. fn to_string(&self, buffer: &mut String) {
  378. writeln!(
  379. buffer,
  380. "(({a}, {b}), {c}),",
  381. a = self.a,
  382. b = self.b,
  383. c = self.c
  384. )
  385. .unwrap();
  386. }
  387. fn prologue() -> &'static str {
  388. "
  389. use compiler_builtins::int::shift::__ashrdi3;
  390. static TEST_CASES: &[((i64, u32), i64)] = &[
  391. "
  392. }
  393. fn epilogue() -> &'static str {
  394. "
  395. ];
  396. #[test]
  397. fn ashrdi3() {
  398. for &((a, b), c) in TEST_CASES {
  399. let c_ = __ashrdi3(a, b);
  400. assert_eq!(((a, b), c), ((a, b), c_));
  401. }
  402. }
  403. "
  404. }
  405. }
  406. #[derive(Eq, Hash, PartialEq)]
  407. pub struct Ashrti3 {
  408. a: i128,
  409. b: u32,
  410. c: i128,
  411. }
  412. impl TestCase for Ashrti3 {
  413. fn name() -> &'static str {
  414. "ashrti3"
  415. }
  416. fn generate<R>(rng: &mut R) -> Option<Self>
  417. where
  418. R: Rng,
  419. Self: Sized,
  420. {
  421. let a = gen_i128(rng);
  422. let b = (rng.gen::<u8>() % 128) as u32;
  423. let c = a >> b;
  424. Some(Ashrti3 { a, b, c })
  425. }
  426. fn to_string(&self, buffer: &mut String) {
  427. writeln!(
  428. buffer,
  429. "(({a}, {b}), {c}),",
  430. a = self.a,
  431. b = self.b,
  432. c = self.c
  433. )
  434. .unwrap();
  435. }
  436. fn prologue() -> &'static str {
  437. "
  438. use compiler_builtins::int::shift::__ashrti3;
  439. static TEST_CASES: &[((i128, u32), i128)] = &[
  440. "
  441. }
  442. fn epilogue() -> &'static str {
  443. "
  444. ];
  445. #[test]
  446. fn ashrti3() {
  447. for &((a, b), c) in TEST_CASES {
  448. let c_ = __ashrti3(a, b);
  449. assert_eq!(((a, b), c), ((a, b), c_));
  450. }
  451. }
  452. "
  453. }
  454. }
  455. #[derive(Eq, Hash, PartialEq)]
  456. pub struct Divmoddi4 {
  457. a: i64,
  458. b: i64,
  459. c: i64,
  460. rem: i64,
  461. }
  462. impl TestCase for Divmoddi4 {
  463. fn name() -> &'static str {
  464. "divmoddi4"
  465. }
  466. fn generate<R>(rng: &mut R) -> Option<Self>
  467. where
  468. R: Rng,
  469. Self: Sized,
  470. {
  471. let a = gen_i64(rng);
  472. let b = gen_i64(rng);
  473. if b == 0 {
  474. return None;
  475. }
  476. let c = a / b;
  477. let rem = a % b;
  478. Some(Divmoddi4 { a, b, c, rem })
  479. }
  480. fn to_string(&self, buffer: &mut String) {
  481. writeln!(
  482. buffer,
  483. "(({a}, {b}), ({c}, {rem})),",
  484. a = self.a,
  485. b = self.b,
  486. c = self.c,
  487. rem = self.rem
  488. )
  489. .unwrap();
  490. }
  491. fn prologue() -> &'static str {
  492. "
  493. use compiler_builtins::int::sdiv::__divmoddi4;
  494. static TEST_CASES: &[((i64, i64), (i64, i64))] = &[
  495. "
  496. }
  497. fn epilogue() -> &'static str {
  498. "
  499. ];
  500. #[test]
  501. fn divmoddi4() {
  502. for &((a, b), (c, rem)) in TEST_CASES {
  503. let mut rem_ = 0;
  504. let c_ = __divmoddi4(a, b, &mut rem_);
  505. assert_eq!(((a, b), (c, rem)), ((a, b), (c_, rem_)));
  506. }
  507. }
  508. "
  509. }
  510. }
  511. #[derive(Eq, Hash, PartialEq)]
  512. pub struct Divdi3 {
  513. a: i64,
  514. b: i64,
  515. c: i64,
  516. }
  517. impl TestCase for Divdi3 {
  518. fn name() -> &'static str {
  519. "divdi3"
  520. }
  521. fn generate<R>(rng: &mut R) -> Option<Self>
  522. where
  523. R: Rng,
  524. Self: Sized,
  525. {
  526. let a = gen_i64(rng);
  527. let b = gen_i64(rng);
  528. if b == 0 {
  529. return None;
  530. }
  531. let c = a / b;
  532. Some(Divdi3 { a, b, c })
  533. }
  534. fn to_string(&self, buffer: &mut String) {
  535. writeln!(
  536. buffer,
  537. "(({a}, {b}), {c}),",
  538. a = self.a,
  539. b = self.b,
  540. c = self.c
  541. )
  542. .unwrap();
  543. }
  544. fn prologue() -> &'static str {
  545. "
  546. use compiler_builtins::int::sdiv::__divdi3;
  547. static TEST_CASES: &[((i64, i64), i64)] = &[
  548. "
  549. }
  550. fn epilogue() -> &'static str {
  551. "
  552. ];
  553. #[test]
  554. fn divdi3() {
  555. for &((a, b), c) in TEST_CASES {
  556. let c_ = __divdi3(a, b);
  557. assert_eq!(((a, b), c), ((a, b), c_));
  558. }
  559. }
  560. "
  561. }
  562. }
  563. #[derive(Eq, Hash, PartialEq)]
  564. pub struct Divmodsi4 {
  565. a: i32,
  566. b: i32,
  567. c: i32,
  568. rem: i32,
  569. }
  570. impl TestCase for Divmodsi4 {
  571. fn name() -> &'static str {
  572. "divmodsi4"
  573. }
  574. fn generate<R>(rng: &mut R) -> Option<Self>
  575. where
  576. R: Rng,
  577. Self: Sized,
  578. {
  579. let a = gen_i32(rng);
  580. let b = gen_i32(rng);
  581. if b == 0 {
  582. return None;
  583. }
  584. let c = a / b;
  585. let rem = a % b;
  586. Some(Divmodsi4 { a, b, c, rem })
  587. }
  588. fn to_string(&self, buffer: &mut String) {
  589. writeln!(
  590. buffer,
  591. "(({a}, {b}), ({c}, {rem})),",
  592. a = self.a,
  593. b = self.b,
  594. c = self.c,
  595. rem = self.rem
  596. )
  597. .unwrap();
  598. }
  599. fn prologue() -> &'static str {
  600. "
  601. use compiler_builtins::int::sdiv::__divmodsi4;
  602. static TEST_CASES: &[((i32, i32), (i32, i32))] = &[
  603. "
  604. }
  605. fn epilogue() -> &'static str {
  606. "
  607. ];
  608. #[test]
  609. fn divmodsi4() {
  610. for &((a, b), (c, rem)) in TEST_CASES {
  611. let mut rem_ = 0;
  612. let c_ = __divmodsi4(a, b, &mut rem_);
  613. assert_eq!(((a, b), (c, rem)), ((a, b), (c_, rem_)));
  614. }
  615. }
  616. "
  617. }
  618. }
  619. #[derive(Eq, Hash, PartialEq)]
  620. pub struct Divsi3 {
  621. a: i32,
  622. b: i32,
  623. c: i32,
  624. }
  625. impl TestCase for Divsi3 {
  626. fn name() -> &'static str {
  627. "divsi3"
  628. }
  629. fn generate<R>(rng: &mut R) -> Option<Self>
  630. where
  631. R: Rng,
  632. Self: Sized,
  633. {
  634. let a = gen_i32(rng);
  635. let b = gen_i32(rng);
  636. if b == 0 {
  637. return None;
  638. }
  639. let c = a / b;
  640. Some(Divsi3 { a, b, c })
  641. }
  642. fn to_string(&self, buffer: &mut String) {
  643. writeln!(
  644. buffer,
  645. "(({a}, {b}), {c}),",
  646. a = self.a,
  647. b = self.b,
  648. c = self.c
  649. )
  650. .unwrap();
  651. }
  652. fn prologue() -> &'static str {
  653. "
  654. use compiler_builtins::int::sdiv::__divsi3;
  655. static TEST_CASES: &[((i32, i32), i32)] = &[
  656. "
  657. }
  658. fn epilogue() -> &'static str {
  659. "
  660. ];
  661. #[test]
  662. fn divsi3() {
  663. for &((a, b), c) in TEST_CASES {
  664. let c_ = __divsi3(a, b);
  665. assert_eq!(((a, b), c), ((a, b), c_));
  666. }
  667. }
  668. "
  669. }
  670. }
  671. #[derive(Eq, Hash, PartialEq)]
  672. pub struct Divti3 {
  673. a: i128,
  674. b: i128,
  675. c: i128,
  676. }
  677. impl TestCase for Divti3 {
  678. fn name() -> &'static str {
  679. "divti3"
  680. }
  681. fn generate<R>(rng: &mut R) -> Option<Self>
  682. where
  683. R: Rng,
  684. Self: Sized,
  685. {
  686. let a = gen_i128(rng);
  687. let b = gen_i128(rng);
  688. if b == 0 {
  689. return None;
  690. }
  691. let c = a / b;
  692. Some(Divti3 { a, b, c })
  693. }
  694. fn to_string(&self, buffer: &mut String) {
  695. writeln!(
  696. buffer,
  697. "(({a}, {b}), {c}),",
  698. a = self.a,
  699. b = self.b,
  700. c = self.c
  701. )
  702. .unwrap();
  703. }
  704. fn prologue() -> &'static str {
  705. "
  706. use compiler_builtins::int::sdiv::__divti3;
  707. static TEST_CASES: &[((i128, i128), i128)] = &[
  708. "
  709. }
  710. fn epilogue() -> &'static str {
  711. "
  712. ];
  713. #[test]
  714. fn divti3() {
  715. for &((a, b), c) in TEST_CASES {
  716. let c_ = __divti3(a, b);
  717. assert_eq!(((a, b), c), ((a, b), c_));
  718. }
  719. }
  720. "
  721. }
  722. }
  723. #[derive(Eq, Hash, PartialEq)]
  724. pub struct Moddi3 {
  725. a: i64,
  726. b: i64,
  727. c: i64,
  728. }
  729. impl TestCase for Moddi3 {
  730. fn name() -> &'static str {
  731. "moddi3"
  732. }
  733. fn generate<R>(rng: &mut R) -> Option<Self>
  734. where
  735. R: Rng,
  736. Self: Sized,
  737. {
  738. let a = gen_i64(rng);
  739. let b = gen_i64(rng);
  740. if b == 0 {
  741. return None;
  742. }
  743. let c = a % b;
  744. Some(Moddi3 { a, b, c })
  745. }
  746. fn to_string(&self, buffer: &mut String) {
  747. writeln!(
  748. buffer,
  749. "(({a}, {b}), {c}),",
  750. a = self.a,
  751. b = self.b,
  752. c = self.c
  753. )
  754. .unwrap();
  755. }
  756. fn prologue() -> &'static str {
  757. "
  758. use compiler_builtins::int::sdiv::__moddi3;
  759. static TEST_CASES: &[((i64, i64), i64)] = &[
  760. "
  761. }
  762. fn epilogue() -> &'static str {
  763. "
  764. ];
  765. #[test]
  766. fn moddi3() {
  767. for &((a, b), c) in TEST_CASES {
  768. let c_ = __moddi3(a, b);
  769. assert_eq!(((a, b), c), ((a, b), c_));
  770. }
  771. }
  772. "
  773. }
  774. }
  775. #[derive(Eq, Hash, PartialEq)]
  776. pub struct Modsi3 {
  777. a: i32,
  778. b: i32,
  779. c: i32,
  780. }
  781. impl TestCase for Modsi3 {
  782. fn name() -> &'static str {
  783. "modsi3"
  784. }
  785. fn generate<R>(rng: &mut R) -> Option<Self>
  786. where
  787. R: Rng,
  788. Self: Sized,
  789. {
  790. let a = gen_i32(rng);
  791. let b = gen_i32(rng);
  792. if b == 0 {
  793. return None;
  794. }
  795. let c = a % b;
  796. Some(Modsi3 { a, b, c })
  797. }
  798. fn to_string(&self, buffer: &mut String) {
  799. writeln!(
  800. buffer,
  801. "(({a}, {b}), {c}),",
  802. a = self.a,
  803. b = self.b,
  804. c = self.c
  805. )
  806. .unwrap();
  807. }
  808. fn prologue() -> &'static str {
  809. "
  810. use compiler_builtins::int::sdiv::__modsi3;
  811. static TEST_CASES: &[((i32, i32), i32)] = &[
  812. "
  813. }
  814. fn epilogue() -> &'static str {
  815. "
  816. ];
  817. #[test]
  818. fn modsi3() {
  819. for &((a, b), c) in TEST_CASES {
  820. let c_ = __modsi3(a, b);
  821. assert_eq!(((a, b), c), ((a, b), c_));
  822. }
  823. }
  824. "
  825. }
  826. }
  827. #[derive(Eq, Hash, PartialEq)]
  828. pub struct Modti3 {
  829. a: i128,
  830. b: i128,
  831. c: i128,
  832. }
  833. impl TestCase for Modti3 {
  834. fn name() -> &'static str {
  835. "modti3"
  836. }
  837. fn generate<R>(rng: &mut R) -> Option<Self>
  838. where
  839. R: Rng,
  840. Self: Sized,
  841. {
  842. let a = gen_i128(rng);
  843. let b = gen_i128(rng);
  844. if b == 0 {
  845. return None;
  846. }
  847. let c = a % b;
  848. Some(Modti3 { a, b, c })
  849. }
  850. fn to_string(&self, buffer: &mut String) {
  851. writeln!(
  852. buffer,
  853. "(({a}, {b}), {c}),",
  854. a = self.a,
  855. b = self.b,
  856. c = self.c
  857. )
  858. .unwrap();
  859. }
  860. fn prologue() -> &'static str {
  861. "
  862. use compiler_builtins::int::sdiv::__modti3;
  863. static TEST_CASES: &[((i128, i128), i128)] = &[
  864. "
  865. }
  866. fn epilogue() -> &'static str {
  867. "
  868. ];
  869. #[test]
  870. fn modti3() {
  871. for &((a, b), c) in TEST_CASES {
  872. let c_ = __modti3(a, b);
  873. assert_eq!(((a, b), c), ((a, b), c_));
  874. }
  875. }
  876. "
  877. }
  878. }
  879. #[derive(Eq, Hash, PartialEq)]
  880. struct Muldi3 {
  881. a: u64,
  882. b: u64,
  883. c: u64,
  884. }
  885. impl TestCase for Muldi3 {
  886. fn name() -> &'static str {
  887. "muldi3"
  888. }
  889. fn generate<R>(rng: &mut R) -> Option<Self>
  890. where
  891. R: Rng,
  892. Self: Sized,
  893. {
  894. let a = gen_u64(rng);
  895. let b = gen_u64(rng);
  896. let c = a.wrapping_mul(b);
  897. Some(Muldi3 { a, b, c })
  898. }
  899. fn to_string(&self, buffer: &mut String) {
  900. writeln!(
  901. buffer,
  902. "(({a}, {b}), {c}),",
  903. a = self.a,
  904. b = self.b,
  905. c = self.c
  906. )
  907. .unwrap();
  908. }
  909. fn prologue() -> &'static str {
  910. "
  911. use compiler_builtins::int::mul::__muldi3;
  912. static TEST_CASES: &[((u64, u64), u64)] = &[
  913. "
  914. }
  915. fn epilogue() -> &'static str {
  916. "
  917. ];
  918. #[test]
  919. fn muldi3() {
  920. for &((a, b), c) in TEST_CASES {
  921. let c_ = __muldi3(a, b);
  922. assert_eq!(((a, b), c), ((a, b), c_));
  923. }
  924. }
  925. "
  926. }
  927. }
  928. #[derive(Eq, Hash, PartialEq)]
  929. pub struct Mulodi4 {
  930. a: i64,
  931. b: i64,
  932. c: i64,
  933. overflow: u32,
  934. }
  935. impl TestCase for Mulodi4 {
  936. fn name() -> &'static str {
  937. "mulodi4"
  938. }
  939. fn generate<R>(rng: &mut R) -> Option<Self>
  940. where
  941. R: Rng,
  942. {
  943. let a = gen_i64(rng);
  944. let b = gen_i64(rng);
  945. let c = a.wrapping_mul(b);
  946. let overflow = if a.checked_mul(b).is_some() { 0 } else { 1 };
  947. Some(Mulodi4 { a, b, c, overflow })
  948. }
  949. fn to_string(&self, buffer: &mut String) {
  950. writeln!(
  951. buffer,
  952. "(({a}, {b}), ({c}, {overflow})),",
  953. a = self.a,
  954. b = self.b,
  955. c = self.c,
  956. overflow = self.overflow
  957. )
  958. .unwrap();
  959. }
  960. fn prologue() -> &'static str {
  961. "
  962. use compiler_builtins::int::mul::__mulodi4;
  963. static TEST_CASES: &[((i64, i64), (i64, i32))] = &[
  964. "
  965. }
  966. fn epilogue() -> &'static str {
  967. "
  968. ];
  969. #[test]
  970. fn mulodi4() {
  971. let mut overflow_ = 2;
  972. for &((a, b), (c, overflow)) in TEST_CASES {
  973. let c_ = __mulodi4(a, b, &mut overflow_);
  974. assert_eq!(((a, b), (c, overflow)), ((a, b), (c_, overflow_)));
  975. }
  976. }
  977. "
  978. }
  979. }
  980. #[derive(Eq, Hash, PartialEq)]
  981. pub struct Mulosi4 {
  982. a: i32,
  983. b: i32,
  984. c: i32,
  985. overflow: u32,
  986. }
  987. impl TestCase for Mulosi4 {
  988. fn name() -> &'static str {
  989. "mulosi4"
  990. }
  991. fn generate<R>(rng: &mut R) -> Option<Self>
  992. where
  993. R: Rng,
  994. {
  995. let a = gen_i32(rng);
  996. let b = gen_i32(rng);
  997. let c = a.wrapping_mul(b);
  998. let overflow = if a.checked_mul(b).is_some() { 0 } else { 1 };
  999. Some(Mulosi4 { a, b, c, overflow })
  1000. }
  1001. fn prologue() -> &'static str {
  1002. "
  1003. use compiler_builtins::int::mul::__mulosi4;
  1004. static TEST_CASES: &[((i32, i32), (i32, i32))] = &[
  1005. "
  1006. }
  1007. fn epilogue() -> &'static str {
  1008. "
  1009. ];
  1010. #[test]
  1011. fn mulosi4() {
  1012. let mut overflow_ = 2;
  1013. for &((a, b), (c, overflow)) in TEST_CASES {
  1014. let c_ = __mulosi4(a, b, &mut overflow_);
  1015. assert_eq!(((a, b), (c, overflow)), ((a, b), (c_, overflow_)));
  1016. }
  1017. }
  1018. "
  1019. }
  1020. fn to_string(&self, buffer: &mut String) {
  1021. writeln!(
  1022. buffer,
  1023. "(({a}, {b}), ({c}, {overflow})),",
  1024. a = self.a,
  1025. b = self.b,
  1026. c = self.c,
  1027. overflow = self.overflow
  1028. )
  1029. .unwrap();
  1030. }
  1031. }
  1032. #[derive(Eq, Hash, PartialEq)]
  1033. pub struct Muloti4 {
  1034. a: i128,
  1035. b: i128,
  1036. c: i128,
  1037. overflow: u32,
  1038. }
  1039. impl TestCase for Muloti4 {
  1040. fn name() -> &'static str {
  1041. "muloti4"
  1042. }
  1043. fn generate<R>(rng: &mut R) -> Option<Self>
  1044. where
  1045. R: Rng,
  1046. {
  1047. let a = gen_i128(rng);
  1048. let b = gen_i128(rng);
  1049. let c = a.wrapping_mul(b);
  1050. let overflow = if a.checked_mul(b).is_some() { 0 } else { 1 };
  1051. Some(Muloti4 { a, b, c, overflow })
  1052. }
  1053. fn prologue() -> &'static str {
  1054. "
  1055. use compiler_builtins::int::mul::__muloti4;
  1056. static TEST_CASES: &[((i128, i128), (i128, i32))] = &[
  1057. "
  1058. }
  1059. fn epilogue() -> &'static str {
  1060. "
  1061. ];
  1062. #[test]
  1063. fn muloti4() {
  1064. let mut overflow_ = 2;
  1065. for &((a, b), (c, overflow)) in TEST_CASES {
  1066. let c_ = __muloti4(a, b, &mut overflow_);
  1067. assert_eq!(((a, b), (c, overflow)), ((a, b), (c_, overflow_)));
  1068. }
  1069. }
  1070. "
  1071. }
  1072. fn to_string(&self, buffer: &mut String) {
  1073. writeln!(
  1074. buffer,
  1075. "(({a}, {b}), ({c}, {overflow})),",
  1076. a = self.a,
  1077. b = self.b,
  1078. c = self.c,
  1079. overflow = self.overflow
  1080. )
  1081. .unwrap();
  1082. }
  1083. }
  1084. #[derive(Eq, Hash, PartialEq)]
  1085. pub struct Multi3 {
  1086. a: i128,
  1087. b: i128,
  1088. c: i128,
  1089. }
  1090. impl TestCase for Multi3 {
  1091. fn name() -> &'static str {
  1092. "multi3"
  1093. }
  1094. fn generate<R>(rng: &mut R) -> Option<Self>
  1095. where
  1096. R: Rng,
  1097. Self: Sized,
  1098. {
  1099. let a = gen_i128(rng);
  1100. let b = gen_i128(rng);
  1101. let c = a.wrapping_mul(b);
  1102. Some(Multi3 { a, b, c })
  1103. }
  1104. fn to_string(&self, buffer: &mut String) {
  1105. writeln!(
  1106. buffer,
  1107. "(({a}, {b}), {c}),",
  1108. a = self.a,
  1109. b = self.b,
  1110. c = self.c
  1111. )
  1112. .unwrap();
  1113. }
  1114. fn prologue() -> &'static str {
  1115. "
  1116. use compiler_builtins::int::mul::__multi3;
  1117. static TEST_CASES: &[((i128, i128), i128)] = &[
  1118. "
  1119. }
  1120. fn epilogue() -> &'static str {
  1121. "
  1122. ];
  1123. #[test]
  1124. fn multi3() {
  1125. for &((a, b), c) in TEST_CASES {
  1126. let c_ = __multi3(a, b);
  1127. assert_eq!(((a, b), c), ((a, b), c_));
  1128. }
  1129. }
  1130. "
  1131. }
  1132. }
  1133. #[derive(Eq, Hash, PartialEq)]
  1134. pub struct Powidf2 {
  1135. a: u64,
  1136. b: i32,
  1137. c: u64,
  1138. }
  1139. impl TestCase for Powidf2 {
  1140. fn name() -> &'static str {
  1141. "powidf2"
  1142. }
  1143. fn generate<R>(rng: &mut R) -> Option<Self>
  1144. where
  1145. R: Rng,
  1146. Self: Sized,
  1147. {
  1148. let a = gen_f64(rng);
  1149. let b = gen_i32(rng);
  1150. let c = a.powi(b);
  1151. // TODO accept NaNs. We don't do that right now because we can't check
  1152. // for NaN-ness on the thumb targets
  1153. if a.is_nan() || c.is_nan() {
  1154. return None;
  1155. }
  1156. Some(
  1157. Powidf2 {
  1158. a: to_u64(a),
  1159. b,
  1160. c: to_u64(c),
  1161. },
  1162. )
  1163. }
  1164. fn to_string(&self, buffer: &mut String) {
  1165. writeln!(
  1166. buffer,
  1167. "(({a}, {b}), {c}),",
  1168. a = self.a,
  1169. b = self.b,
  1170. c = self.c
  1171. )
  1172. .unwrap();
  1173. }
  1174. fn prologue() -> &'static str {
  1175. r#"
  1176. #[cfg(all(target_arch = "arm",
  1177. not(any(target_env = "gnu", target_env = "musl")),
  1178. target_os = "linux",
  1179. test))]
  1180. use core::mem;
  1181. #[cfg(not(all(target_arch = "arm",
  1182. not(any(target_env = "gnu", target_env = "musl")),
  1183. target_os = "linux",
  1184. test)))]
  1185. use std::mem;
  1186. use compiler_builtins::float::pow::__powidf2;
  1187. fn mk_f64(x: u64) -> f64 {
  1188. unsafe { mem::transmute(x) }
  1189. }
  1190. fn to_u64(x: f64) -> u64 {
  1191. unsafe { mem::transmute(x) }
  1192. }
  1193. static TEST_CASES: &[((u64, i32), u64)] = &[
  1194. "#
  1195. }
  1196. fn epilogue() -> &'static str {
  1197. "
  1198. ];
  1199. #[test]
  1200. fn powidf2() {
  1201. for &((a, b), c) in TEST_CASES {
  1202. let c_ = __powidf2(mk_f64(a), b);
  1203. assert_eq!(((a, b), c), ((a, b), to_u64(c_)));
  1204. }
  1205. }
  1206. "
  1207. }
  1208. }
  1209. #[derive(Eq, Hash, PartialEq)]
  1210. pub struct Powisf2 {
  1211. a: u32,
  1212. b: i32,
  1213. c: u32,
  1214. }
  1215. impl TestCase for Powisf2 {
  1216. fn name() -> &'static str {
  1217. "powisf2"
  1218. }
  1219. fn generate<R>(rng: &mut R) -> Option<Self>
  1220. where
  1221. R: Rng,
  1222. Self: Sized,
  1223. {
  1224. let a = gen_f32(rng);
  1225. let b = gen_i32(rng);
  1226. let c = a.powi(b);
  1227. // TODO accept NaNs. We don't do that right now because we can't check
  1228. // for NaN-ness on the thumb targets
  1229. if a.is_nan() || c.is_nan() {
  1230. return None;
  1231. }
  1232. Some(
  1233. Powisf2 {
  1234. a: to_u32(a),
  1235. b,
  1236. c: to_u32(c),
  1237. },
  1238. )
  1239. }
  1240. fn to_string(&self, buffer: &mut String) {
  1241. writeln!(
  1242. buffer,
  1243. "(({a}, {b}), {c}),",
  1244. a = self.a,
  1245. b = self.b,
  1246. c = self.c
  1247. )
  1248. .unwrap();
  1249. }
  1250. fn prologue() -> &'static str {
  1251. r#"
  1252. #[cfg(all(target_arch = "arm",
  1253. not(any(target_env = "gnu", target_env = "musl")),
  1254. target_os = "linux",
  1255. test))]
  1256. use core::mem;
  1257. #[cfg(not(all(target_arch = "arm",
  1258. not(any(target_env = "gnu", target_env = "musl")),
  1259. target_os = "linux",
  1260. test)))]
  1261. use std::mem;
  1262. use compiler_builtins::float::pow::__powisf2;
  1263. fn mk_f32(x: u32) -> f32 {
  1264. unsafe { mem::transmute(x) }
  1265. }
  1266. fn to_u32(x: f32) -> u32 {
  1267. unsafe { mem::transmute(x) }
  1268. }
  1269. static TEST_CASES: &[((u32, i32), u32)] = &[
  1270. "#
  1271. }
  1272. fn epilogue() -> &'static str {
  1273. "
  1274. ];
  1275. #[test]
  1276. fn powisf2() {
  1277. for &((a, b), c) in TEST_CASES {
  1278. let c_ = __powisf2(mk_f32(a), b);
  1279. assert_eq!(((a, b), c), ((a, b), to_u32(c_)));
  1280. }
  1281. }
  1282. "
  1283. }
  1284. }
  1285. #[derive(Eq, Hash, PartialEq)]
  1286. pub struct Lshrdi3 {
  1287. a: u64,
  1288. b: u32,
  1289. c: u64,
  1290. }
  1291. impl TestCase for Lshrdi3 {
  1292. fn name() -> &'static str {
  1293. "lshrdi3"
  1294. }
  1295. fn generate<R>(rng: &mut R) -> Option<Self>
  1296. where
  1297. R: Rng,
  1298. Self: Sized,
  1299. {
  1300. let a = gen_u64(rng);
  1301. let b = (rng.gen::<u8>() % 64) as u32;
  1302. let c = a >> b;
  1303. Some(Lshrdi3 { a, b, c })
  1304. }
  1305. fn to_string(&self, buffer: &mut String) {
  1306. writeln!(
  1307. buffer,
  1308. "(({a}, {b}), {c}),",
  1309. a = self.a,
  1310. b = self.b,
  1311. c = self.c
  1312. )
  1313. .unwrap();
  1314. }
  1315. fn prologue() -> &'static str {
  1316. "
  1317. use compiler_builtins::int::shift::__lshrdi3;
  1318. static TEST_CASES: &[((u64, u32), u64)] = &[
  1319. "
  1320. }
  1321. fn epilogue() -> &'static str {
  1322. "
  1323. ];
  1324. #[test]
  1325. fn lshrdi3() {
  1326. for &((a, b), c) in TEST_CASES {
  1327. let c_ = __lshrdi3(a, b);
  1328. assert_eq!(((a, b), c), ((a, b), c_));
  1329. }
  1330. }
  1331. "
  1332. }
  1333. }
  1334. #[derive(Eq, Hash, PartialEq)]
  1335. pub struct Lshrti3 {
  1336. a: u128,
  1337. b: u32,
  1338. c: u128,
  1339. }
  1340. impl TestCase for Lshrti3 {
  1341. fn name() -> &'static str {
  1342. "lshrti3"
  1343. }
  1344. fn generate<R>(rng: &mut R) -> Option<Self>
  1345. where
  1346. R: Rng,
  1347. Self: Sized,
  1348. {
  1349. let a = gen_u128(rng);
  1350. let b = (rng.gen::<u8>() % 128) as u32;
  1351. let c = a >> b;
  1352. Some(Lshrti3 { a, b, c })
  1353. }
  1354. fn to_string(&self, buffer: &mut String) {
  1355. writeln!(
  1356. buffer,
  1357. "(({a}, {b}), {c}),",
  1358. a = self.a,
  1359. b = self.b,
  1360. c = self.c
  1361. )
  1362. .unwrap();
  1363. }
  1364. fn prologue() -> &'static str {
  1365. "
  1366. use compiler_builtins::int::shift::__lshrti3;
  1367. static TEST_CASES: &[((u128, u32), u128)] = &[
  1368. "
  1369. }
  1370. fn epilogue() -> &'static str {
  1371. "
  1372. ];
  1373. #[test]
  1374. fn lshrti3() {
  1375. for &((a, b), c) in TEST_CASES {
  1376. let c_ = __lshrti3(a, b);
  1377. assert_eq!(((a, b), c), ((a, b), c_));
  1378. }
  1379. }
  1380. "
  1381. }
  1382. }
  1383. #[derive(Eq, Hash, PartialEq)]
  1384. pub struct Subdf3 {
  1385. a: u64,
  1386. b: u64,
  1387. c: u64,
  1388. }
  1389. impl TestCase for Subdf3 {
  1390. fn name() -> &'static str {
  1391. "subdf3"
  1392. }
  1393. fn generate<R>(rng: &mut R) -> Option<Self>
  1394. where
  1395. R: Rng,
  1396. Self: Sized,
  1397. {
  1398. let a = gen_f64(rng);
  1399. let b = gen_f64(rng);
  1400. let c = a - b;
  1401. // TODO accept NaNs. We don't do that right now because we can't check
  1402. // for NaN-ness on the thumb targets (due to missing intrinsics)
  1403. if a.is_nan() || b.is_nan() || c.is_nan() {
  1404. return None;
  1405. }
  1406. Some(
  1407. Subdf3 {
  1408. a: to_u64(a),
  1409. b: to_u64(b),
  1410. c: to_u64(c),
  1411. },
  1412. )
  1413. }
  1414. fn to_string(&self, buffer: &mut String) {
  1415. writeln!(
  1416. buffer,
  1417. "(({a}, {b}), {c}),",
  1418. a = self.a,
  1419. b = self.b,
  1420. c = self.c
  1421. )
  1422. .unwrap();
  1423. }
  1424. fn prologue() -> &'static str {
  1425. r#"
  1426. #[cfg(all(target_arch = "arm",
  1427. not(any(target_env = "gnu", target_env = "musl")),
  1428. target_os = "linux",
  1429. test))]
  1430. use core::mem;
  1431. #[cfg(not(all(target_arch = "arm",
  1432. not(any(target_env = "gnu", target_env = "musl")),
  1433. target_os = "linux",
  1434. test)))]
  1435. use std::mem;
  1436. use compiler_builtins::float::sub::__subdf3;
  1437. fn mk_f64(x: u64) -> f64 {
  1438. unsafe { mem::transmute(x) }
  1439. }
  1440. fn to_u64(x: f64) -> u64 {
  1441. unsafe { mem::transmute(x) }
  1442. }
  1443. static TEST_CASES: &[((u64, u64), u64)] = &[
  1444. "#
  1445. }
  1446. fn epilogue() -> &'static str {
  1447. "
  1448. ];
  1449. #[test]
  1450. fn subdf3() {
  1451. for &((a, b), c) in TEST_CASES {
  1452. let c_ = __subdf3(mk_f64(a), mk_f64(b));
  1453. assert_eq!(((a, b), c), ((a, b), to_u64(c_)));
  1454. }
  1455. }
  1456. "
  1457. }
  1458. }
  1459. #[derive(Eq, Hash, PartialEq)]
  1460. pub struct Subsf3 {
  1461. a: u32,
  1462. b: u32,
  1463. c: u32,
  1464. }
  1465. impl TestCase for Subsf3 {
  1466. fn name() -> &'static str {
  1467. "subsf3"
  1468. }
  1469. fn generate<R>(rng: &mut R) -> Option<Self>
  1470. where
  1471. R: Rng,
  1472. Self: Sized,
  1473. {
  1474. let a = gen_f32(rng);
  1475. let b = gen_f32(rng);
  1476. let c = a - b;
  1477. // TODO accept NaNs. We don't do that right now because we can't check
  1478. // for NaN-ness on the thumb targets (due to missing intrinsics)
  1479. if a.is_nan() || b.is_nan() || c.is_nan() {
  1480. return None;
  1481. }
  1482. Some(
  1483. Subsf3 {
  1484. a: to_u32(a),
  1485. b: to_u32(b),
  1486. c: to_u32(c),
  1487. },
  1488. )
  1489. }
  1490. fn to_string(&self, buffer: &mut String) {
  1491. writeln!(
  1492. buffer,
  1493. "(({a}, {b}), {c}),",
  1494. a = self.a,
  1495. b = self.b,
  1496. c = self.c
  1497. )
  1498. .unwrap();
  1499. }
  1500. fn prologue() -> &'static str {
  1501. r#"
  1502. #[cfg(all(target_arch = "arm",
  1503. not(any(target_env = "gnu", target_env = "musl")),
  1504. target_os = "linux",
  1505. test))]
  1506. use core::mem;
  1507. #[cfg(not(all(target_arch = "arm",
  1508. not(any(target_env = "gnu", target_env = "musl")),
  1509. target_os = "linux",
  1510. test)))]
  1511. use std::mem;
  1512. use compiler_builtins::float::sub::__subsf3;
  1513. fn mk_f32(x: u32) -> f32 {
  1514. unsafe { mem::transmute(x) }
  1515. }
  1516. fn to_u32(x: f32) -> u32 {
  1517. unsafe { mem::transmute(x) }
  1518. }
  1519. static TEST_CASES: &[((u32, u32), u32)] = &[
  1520. "#
  1521. }
  1522. fn epilogue() -> &'static str {
  1523. "
  1524. ];
  1525. #[test]
  1526. fn subsf3() {
  1527. for &((a, b), c) in TEST_CASES {
  1528. let c_ = __subsf3(mk_f32(a), mk_f32(b));
  1529. assert_eq!(((a, b), c), ((a, b), to_u32(c_)));
  1530. }
  1531. }
  1532. "
  1533. }
  1534. }
  1535. #[derive(Eq, Hash, PartialEq)]
  1536. pub struct Udivdi3 {
  1537. a: u64,
  1538. b: u64,
  1539. c: u64,
  1540. }
  1541. impl TestCase for Udivdi3 {
  1542. fn name() -> &'static str {
  1543. "udivdi3"
  1544. }
  1545. fn generate<R>(rng: &mut R) -> Option<Self>
  1546. where
  1547. R: Rng,
  1548. Self: Sized,
  1549. {
  1550. let a = gen_u64(rng);
  1551. let b = gen_u64(rng);
  1552. if b == 0 {
  1553. return None;
  1554. }
  1555. let c = a / b;
  1556. Some(Udivdi3 { a, b, c })
  1557. }
  1558. fn to_string(&self, buffer: &mut String) {
  1559. writeln!(
  1560. buffer,
  1561. "(({a}, {b}), {c}),",
  1562. a = self.a,
  1563. b = self.b,
  1564. c = self.c
  1565. )
  1566. .unwrap();
  1567. }
  1568. fn prologue() -> &'static str {
  1569. "
  1570. use compiler_builtins::int::udiv::__udivdi3;
  1571. static TEST_CASES: &[((u64, u64), u64)] = &[
  1572. "
  1573. }
  1574. fn epilogue() -> &'static str {
  1575. "
  1576. ];
  1577. #[test]
  1578. fn udivdi3() {
  1579. for &((a, b), c) in TEST_CASES {
  1580. let c_ = __udivdi3(a, b);
  1581. assert_eq!(((a, b), c), ((a, b), c_));
  1582. }
  1583. }
  1584. "
  1585. }
  1586. }
  1587. #[derive(Eq, Hash, PartialEq)]
  1588. pub struct Udivmoddi4 {
  1589. a: u64,
  1590. b: u64,
  1591. c: u64,
  1592. rem: u64,
  1593. }
  1594. impl TestCase for Udivmoddi4 {
  1595. fn name() -> &'static str {
  1596. "udivmoddi4"
  1597. }
  1598. fn generate<R>(rng: &mut R) -> Option<Self>
  1599. where
  1600. R: Rng,
  1601. Self: Sized,
  1602. {
  1603. let a = gen_u64(rng);
  1604. let b = gen_u64(rng);
  1605. if b == 0 {
  1606. return None;
  1607. }
  1608. let c = a / b;
  1609. let rem = a % b;
  1610. Some(Udivmoddi4 { a, b, c, rem })
  1611. }
  1612. fn to_string(&self, buffer: &mut String) {
  1613. writeln!(
  1614. buffer,
  1615. "(({a}, {b}), ({c}, {rem})),",
  1616. a = self.a,
  1617. b = self.b,
  1618. c = self.c,
  1619. rem = self.rem
  1620. )
  1621. .unwrap();
  1622. }
  1623. fn prologue() -> &'static str {
  1624. "
  1625. use compiler_builtins::int::udiv::__udivmoddi4;
  1626. static TEST_CASES: &[((u64, u64), (u64, u64))] = &[
  1627. "
  1628. }
  1629. fn epilogue() -> &'static str {
  1630. "
  1631. ];
  1632. #[test]
  1633. fn udivmoddi4() {
  1634. for &((a, b), (c, rem)) in TEST_CASES {
  1635. let mut rem_ = 0;
  1636. let c_ = __udivmoddi4(a, b, Some(&mut rem_));
  1637. assert_eq!(((a, b), (c, rem)), ((a, b), (c_, rem_)));
  1638. }
  1639. }
  1640. "
  1641. }
  1642. }
  1643. #[derive(Eq, Hash, PartialEq)]
  1644. pub struct Udivmodsi4 {
  1645. a: u32,
  1646. b: u32,
  1647. c: u32,
  1648. rem: u32,
  1649. }
  1650. impl TestCase for Udivmodsi4 {
  1651. fn name() -> &'static str {
  1652. "udivmodsi4"
  1653. }
  1654. fn generate<R>(rng: &mut R) -> Option<Self>
  1655. where
  1656. R: Rng,
  1657. Self: Sized,
  1658. {
  1659. let a = gen_u32(rng);
  1660. let b = gen_u32(rng);
  1661. if b == 0 {
  1662. return None;
  1663. }
  1664. let c = a / b;
  1665. let rem = a % b;
  1666. Some(Udivmodsi4 { a, b, c, rem })
  1667. }
  1668. fn to_string(&self, buffer: &mut String) {
  1669. writeln!(
  1670. buffer,
  1671. "(({a}, {b}), ({c}, {rem})),",
  1672. a = self.a,
  1673. b = self.b,
  1674. c = self.c,
  1675. rem = self.rem
  1676. )
  1677. .unwrap();
  1678. }
  1679. fn prologue() -> &'static str {
  1680. "
  1681. use compiler_builtins::int::udiv::__udivmodsi4;
  1682. static TEST_CASES: &[((u32, u32), (u32, u32))] = &[
  1683. "
  1684. }
  1685. fn epilogue() -> &'static str {
  1686. "
  1687. ];
  1688. #[test]
  1689. fn udivmodsi4() {
  1690. for &((a, b), (c, rem)) in TEST_CASES {
  1691. let mut rem_ = 0;
  1692. let c_ = __udivmodsi4(a, b, Some(&mut rem_));
  1693. assert_eq!(((a, b), (c, rem)), ((a, b), (c_, rem_)));
  1694. }
  1695. }
  1696. "
  1697. }
  1698. }
  1699. #[derive(Eq, Hash, PartialEq)]
  1700. pub struct Udivmodti4 {
  1701. a: u128,
  1702. b: u128,
  1703. c: u128,
  1704. rem: u128,
  1705. }
  1706. impl TestCase for Udivmodti4 {
  1707. fn name() -> &'static str {
  1708. "udivmodti4"
  1709. }
  1710. fn generate<R>(rng: &mut R) -> Option<Self>
  1711. where
  1712. R: Rng,
  1713. Self: Sized,
  1714. {
  1715. let a = gen_u128(rng);
  1716. let b = gen_u128(rng);
  1717. if b == 0 {
  1718. return None;
  1719. }
  1720. let c = a / b;
  1721. let rem = a % b;
  1722. Some(Udivmodti4 { a, b, c, rem })
  1723. }
  1724. fn to_string(&self, buffer: &mut String) {
  1725. writeln!(
  1726. buffer,
  1727. "(({a}, {b}), ({c}, {rem})),",
  1728. a = self.a,
  1729. b = self.b,
  1730. c = self.c,
  1731. rem = self.rem
  1732. )
  1733. .unwrap();
  1734. }
  1735. fn prologue() -> &'static str {
  1736. "
  1737. use compiler_builtins::int::udiv::__udivmodti4;
  1738. static TEST_CASES: &[((u128, u128), (u128, u128))] = &[
  1739. "
  1740. }
  1741. fn epilogue() -> &'static str {
  1742. "
  1743. ];
  1744. #[test]
  1745. fn udivmodti4() {
  1746. for &((a, b), (c, rem)) in TEST_CASES {
  1747. let mut rem_ = 0;
  1748. let c_ = __udivmodti4(a, b, Some(&mut rem_));
  1749. assert_eq!(((a, b), (c, rem)), ((a, b), (c_, rem_)));
  1750. }
  1751. }
  1752. "
  1753. }
  1754. }
  1755. #[derive(Eq, Hash, PartialEq)]
  1756. pub struct Udivsi3 {
  1757. a: u32,
  1758. b: u32,
  1759. c: u32,
  1760. }
  1761. impl TestCase for Udivsi3 {
  1762. fn name() -> &'static str {
  1763. "udivsi3"
  1764. }
  1765. fn generate<R>(rng: &mut R) -> Option<Self>
  1766. where
  1767. R: Rng,
  1768. Self: Sized,
  1769. {
  1770. let a = gen_u32(rng);
  1771. let b = gen_u32(rng);
  1772. if b == 0 {
  1773. return None;
  1774. }
  1775. let c = a / b;
  1776. Some(Udivsi3 { a, b, c })
  1777. }
  1778. fn to_string(&self, buffer: &mut String) {
  1779. writeln!(
  1780. buffer,
  1781. "(({a}, {b}), {c}),",
  1782. a = self.a,
  1783. b = self.b,
  1784. c = self.c
  1785. )
  1786. .unwrap();
  1787. }
  1788. fn prologue() -> &'static str {
  1789. "
  1790. use compiler_builtins::int::udiv::__udivsi3;
  1791. static TEST_CASES: &[((u32, u32), u32)] = &[
  1792. "
  1793. }
  1794. fn epilogue() -> &'static str {
  1795. "
  1796. ];
  1797. #[test]
  1798. fn udivsi3() {
  1799. for &((a, b), c) in TEST_CASES {
  1800. let c_ = __udivsi3(a, b);
  1801. assert_eq!(((a, b), c), ((a, b), c_));
  1802. }
  1803. }
  1804. "
  1805. }
  1806. }
  1807. #[derive(Eq, Hash, PartialEq)]
  1808. pub struct Udivti3 {
  1809. a: u128,
  1810. b: u128,
  1811. c: u128,
  1812. }
  1813. impl TestCase for Udivti3 {
  1814. fn name() -> &'static str {
  1815. "udivti3"
  1816. }
  1817. fn generate<R>(rng: &mut R) -> Option<Self>
  1818. where
  1819. R: Rng,
  1820. Self: Sized,
  1821. {
  1822. let a = gen_u128(rng);
  1823. let b = gen_u128(rng);
  1824. if b == 0 {
  1825. return None;
  1826. }
  1827. let c = a / b;
  1828. Some(Udivti3 { a, b, c })
  1829. }
  1830. fn to_string(&self, buffer: &mut String) {
  1831. writeln!(
  1832. buffer,
  1833. "(({a}, {b}), {c}),",
  1834. a = self.a,
  1835. b = self.b,
  1836. c = self.c
  1837. )
  1838. .unwrap();
  1839. }
  1840. fn prologue() -> &'static str {
  1841. "
  1842. use compiler_builtins::int::udiv::__udivti3;
  1843. static TEST_CASES: &[((u128, u128), u128)] = &[
  1844. "
  1845. }
  1846. fn epilogue() -> &'static str {
  1847. "
  1848. ];
  1849. #[test]
  1850. fn udivti3() {
  1851. for &((a, b), c) in TEST_CASES {
  1852. let c_ = __udivti3(a, b);
  1853. assert_eq!(((a, b), c), ((a, b), c_));
  1854. }
  1855. }
  1856. "
  1857. }
  1858. }
  1859. #[derive(Eq, Hash, PartialEq)]
  1860. pub struct Umoddi3 {
  1861. a: u64,
  1862. b: u64,
  1863. c: u64,
  1864. }
  1865. impl TestCase for Umoddi3 {
  1866. fn name() -> &'static str {
  1867. "umoddi3"
  1868. }
  1869. fn generate<R>(rng: &mut R) -> Option<Self>
  1870. where
  1871. R: Rng,
  1872. Self: Sized,
  1873. {
  1874. let a = gen_u64(rng);
  1875. let b = gen_u64(rng);
  1876. if b == 0 {
  1877. return None;
  1878. }
  1879. let c = a % b;
  1880. Some(Umoddi3 { a, b, c })
  1881. }
  1882. fn to_string(&self, buffer: &mut String) {
  1883. writeln!(
  1884. buffer,
  1885. "(({a}, {b}), {c}),",
  1886. a = self.a,
  1887. b = self.b,
  1888. c = self.c
  1889. )
  1890. .unwrap();
  1891. }
  1892. fn prologue() -> &'static str {
  1893. "
  1894. use compiler_builtins::int::udiv::__umoddi3;
  1895. static TEST_CASES: &[((u64, u64), u64)] = &[
  1896. "
  1897. }
  1898. fn epilogue() -> &'static str {
  1899. "
  1900. ];
  1901. #[test]
  1902. fn umoddi3() {
  1903. for &((a, b), c) in TEST_CASES {
  1904. let c_ = __umoddi3(a, b);
  1905. assert_eq!(((a, b), c), ((a, b), c_));
  1906. }
  1907. }
  1908. "
  1909. }
  1910. }
  1911. #[derive(Eq, Hash, PartialEq)]
  1912. pub struct Umodsi3 {
  1913. a: u32,
  1914. b: u32,
  1915. c: u32,
  1916. }
  1917. impl TestCase for Umodsi3 {
  1918. fn name() -> &'static str {
  1919. "umodsi3"
  1920. }
  1921. fn generate<R>(rng: &mut R) -> Option<Self>
  1922. where
  1923. R: Rng,
  1924. Self: Sized,
  1925. {
  1926. let a = gen_u32(rng);
  1927. let b = gen_u32(rng);
  1928. if b == 0 {
  1929. return None;
  1930. }
  1931. let c = a % b;
  1932. Some(Umodsi3 { a, b, c })
  1933. }
  1934. fn to_string(&self, buffer: &mut String) {
  1935. writeln!(
  1936. buffer,
  1937. "(({a}, {b}), {c}),",
  1938. a = self.a,
  1939. b = self.b,
  1940. c = self.c
  1941. )
  1942. .unwrap();
  1943. }
  1944. fn prologue() -> &'static str {
  1945. "
  1946. use compiler_builtins::int::udiv::__umodsi3;
  1947. static TEST_CASES: &[((u32, u32), u32)] = &[
  1948. "
  1949. }
  1950. fn epilogue() -> &'static str {
  1951. "
  1952. ];
  1953. #[test]
  1954. fn umodsi3() {
  1955. for &((a, b), c) in TEST_CASES {
  1956. let c_ = __umodsi3(a, b);
  1957. assert_eq!(((a, b), c), ((a, b), c_));
  1958. }
  1959. }
  1960. "
  1961. }
  1962. }
  1963. #[derive(Eq, Hash, PartialEq)]
  1964. pub struct Umodti3 {
  1965. a: u128,
  1966. b: u128,
  1967. c: u128,
  1968. }
  1969. impl TestCase for Umodti3 {
  1970. fn name() -> &'static str {
  1971. "umodti3"
  1972. }
  1973. fn generate<R>(rng: &mut R) -> Option<Self>
  1974. where
  1975. R: Rng,
  1976. Self: Sized,
  1977. {
  1978. let a = gen_u128(rng);
  1979. let b = gen_u128(rng);
  1980. if b == 0 {
  1981. return None;
  1982. }
  1983. let c = a % b;
  1984. Some(Umodti3 { a, b, c })
  1985. }
  1986. fn to_string(&self, buffer: &mut String) {
  1987. writeln!(
  1988. buffer,
  1989. "(({a}, {b}), {c}),",
  1990. a = self.a,
  1991. b = self.b,
  1992. c = self.c
  1993. )
  1994. .unwrap();
  1995. }
  1996. fn prologue() -> &'static str {
  1997. "
  1998. use compiler_builtins::int::udiv::__umodti3;
  1999. static TEST_CASES: &[((u128, u128), u128)] = &[
  2000. "
  2001. }
  2002. fn epilogue() -> &'static str {
  2003. "
  2004. ];
  2005. #[test]
  2006. fn umodti3() {
  2007. for &((a, b), c) in TEST_CASES {
  2008. let c_ = __umodti3(a, b);
  2009. assert_eq!(((a, b), c), ((a, b), c_));
  2010. }
  2011. }
  2012. "
  2013. }
  2014. }
  2015. trait TestCase {
  2016. /// Name of the intrinsic to test
  2017. fn name() -> &'static str;
  2018. /// Generates a valid test case
  2019. fn generate<R>(rng: &mut R) -> Option<Self>
  2020. where
  2021. R: Rng,
  2022. Self: Sized;
  2023. /// Stringifies a test case
  2024. fn to_string(&self, buffer: &mut String);
  2025. /// Prologue of the test file
  2026. fn prologue() -> &'static str;
  2027. /// Epilogue of the test file
  2028. fn epilogue() -> &'static str;
  2029. }
  2030. const PROLOGUE: &'static str = r#"
  2031. extern crate compiler_builtins;
  2032. // test runner
  2033. #[cfg(all(target_arch = "arm",
  2034. not(any(target_env = "gnu", target_env = "musl")),
  2035. target_os = "linux",
  2036. test))]
  2037. extern crate utest_cortex_m_qemu;
  2038. // overrides `panic!`
  2039. #[cfg(all(target_arch = "arm",
  2040. not(any(target_env = "gnu", target_env = "musl")),
  2041. target_os = "linux",
  2042. test))]
  2043. #[macro_use]
  2044. extern crate utest_macros;
  2045. #[cfg(all(target_arch = "arm",
  2046. not(any(target_env = "gnu", target_env = "musl")),
  2047. target_os = "linux",
  2048. test))]
  2049. macro_rules! panic {
  2050. ($($tt:tt)*) => {
  2051. upanic!($($tt)*);
  2052. };
  2053. }
  2054. "#;
  2055. macro_rules! gen_int {
  2056. ($name:ident, $ity:ident, $hty:ident) => {
  2057. fn $name<R>(rng: &mut R) -> $ity
  2058. where
  2059. R: Rng,
  2060. {
  2061. let mut mk = || if rng.gen_weighted_bool(10) {
  2062. *rng.choose(&[::std::$hty::MAX, 0, ::std::$hty::MIN]).unwrap()
  2063. } else {
  2064. rng.gen::<$hty>()
  2065. };
  2066. unsafe { mem::transmute([mk(), mk()]) }
  2067. }
  2068. }
  2069. }
  2070. gen_int!(gen_i32, i32, i16);
  2071. gen_int!(gen_i64, i64, i32);
  2072. gen_int!(gen_i128, i128, i64);
  2073. macro_rules! gen_float {
  2074. ($name:ident,
  2075. $fty:ident,
  2076. $uty:ident,
  2077. $bits:expr,
  2078. $significand_bits:expr) => {
  2079. pub fn $name<R>(rng: &mut R) -> $fty
  2080. where
  2081. R: Rng,
  2082. {
  2083. const BITS: u8 = $bits;
  2084. const SIGNIFICAND_BITS: u8 = $significand_bits;
  2085. const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
  2086. const SIGN_MASK: $uty = (1 << (BITS - 1));
  2087. const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
  2088. fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
  2089. unsafe {
  2090. mem::transmute(((sign as $uty) << (BITS - 1)) |
  2091. ((exponent & EXPONENT_MASK) <<
  2092. SIGNIFICAND_BITS) |
  2093. (significand & SIGNIFICAND_MASK))
  2094. }
  2095. }
  2096. if rng.gen_weighted_bool(10) {
  2097. // Special values
  2098. *rng.choose(&[-0.0,
  2099. 0.0,
  2100. ::std::$fty::NAN,
  2101. ::std::$fty::INFINITY,
  2102. -::std::$fty::INFINITY])
  2103. .unwrap()
  2104. } else if rng.gen_weighted_bool(10) {
  2105. // NaN patterns
  2106. mk_f32(rng.gen(), rng.gen(), 0)
  2107. } else if rng.gen() {
  2108. // Denormalized
  2109. mk_f32(rng.gen(), 0, rng.gen())
  2110. } else {
  2111. // Random anything
  2112. mk_f32(rng.gen(), rng.gen(), rng.gen())
  2113. }
  2114. }
  2115. }
  2116. }
  2117. gen_float!(gen_f32, f32, u32, 32, 23);
  2118. gen_float!(gen_f64, f64, u64, 64, 52);
  2119. pub fn gen_u128<R>(rng: &mut R) -> u128
  2120. where
  2121. R: Rng,
  2122. {
  2123. gen_i128(rng) as u128
  2124. }
  2125. pub fn gen_u32<R>(rng: &mut R) -> u32
  2126. where
  2127. R: Rng,
  2128. {
  2129. gen_i32(rng) as u32
  2130. }
  2131. fn gen_u64<R>(rng: &mut R) -> u64
  2132. where
  2133. R: Rng,
  2134. {
  2135. gen_i64(rng) as u64
  2136. }
  2137. pub fn to_u32(x: f32) -> u32 {
  2138. unsafe { mem::transmute(x) }
  2139. }
  2140. pub fn to_u64(x: f64) -> u64 {
  2141. unsafe { mem::transmute(x) }
  2142. }
  2143. fn mk_tests<T, R>(mut n: usize, rng: &mut R) -> String
  2144. where
  2145. T: Eq + Hash + TestCase,
  2146. R: Rng,
  2147. {
  2148. let mut buffer = PROLOGUE.to_owned();
  2149. buffer.push_str(T::prologue());
  2150. let mut cases = HashSet::new();
  2151. while n != 0 {
  2152. if let Some(case) = T::generate(rng) {
  2153. if cases.contains(&case) {
  2154. continue;
  2155. }
  2156. case.to_string(&mut buffer);
  2157. n -= 1;
  2158. cases.insert(case);
  2159. }
  2160. }
  2161. buffer.push_str(T::epilogue());
  2162. buffer
  2163. }
  2164. fn mk_file<T>()
  2165. where
  2166. T: Eq + Hash + TestCase,
  2167. {
  2168. use std::io::Write;
  2169. let rng = &mut rand::thread_rng();
  2170. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  2171. let out_file = out_dir.join(format!("{}.rs", T::name()));
  2172. let contents = mk_tests::<T, _>(NTESTS, rng);
  2173. File::create(out_file)
  2174. .unwrap()
  2175. .write_all(contents.as_bytes())
  2176. .unwrap();
  2177. }
  2178. }
  2179. #[cfg(feature = "c")]
  2180. mod c {
  2181. extern crate gcc;
  2182. use std::collections::BTreeMap;
  2183. use std::env;
  2184. use std::path::Path;
  2185. struct Sources {
  2186. // SYMBOL -> PATH TO SOURCE
  2187. map: BTreeMap<&'static str, &'static str>,
  2188. }
  2189. impl Sources {
  2190. fn new() -> Sources {
  2191. Sources { map: BTreeMap::new() }
  2192. }
  2193. fn extend(&mut self, sources: &[&'static str]) {
  2194. // NOTE Some intrinsics have both a generic implementation (e.g.
  2195. // `floatdidf.c`) and an arch optimized implementation
  2196. // (`x86_64/floatdidf.c`). In those cases, we keep the arch optimized
  2197. // implementation and discard the generic implementation. If we don't
  2198. // and keep both implementations, the linker will yell at us about
  2199. // duplicate symbols!
  2200. for &src in sources {
  2201. let symbol = Path::new(src).file_stem().unwrap().to_str().unwrap();
  2202. if src.contains("/") {
  2203. // Arch-optimized implementation (preferred)
  2204. self.map.insert(symbol, src);
  2205. } else {
  2206. // Generic implementation
  2207. if !self.map.contains_key(symbol) {
  2208. self.map.insert(symbol, src);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. fn remove(&mut self, symbols: &[&str]) {
  2214. for symbol in symbols {
  2215. self.map.remove(*symbol).unwrap();
  2216. }
  2217. }
  2218. }
  2219. /// Compile intrinsics from the compiler-rt C source code
  2220. pub fn compile(llvm_target: &[&str]) {
  2221. let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
  2222. let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
  2223. let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
  2224. let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
  2225. let cfg = &mut gcc::Config::new();
  2226. if target_env == "msvc" {
  2227. // Don't pull in extra libraries on MSVC
  2228. cfg.flag("/Zl");
  2229. // Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP
  2230. cfg.define("__func__", Some("__FUNCTION__"));
  2231. } else {
  2232. // Turn off various features of gcc and such, mostly copying
  2233. // compiler-rt's build system already
  2234. cfg.flag("-fno-builtin");
  2235. cfg.flag("-fvisibility=hidden");
  2236. cfg.flag("-fomit-frame-pointer");
  2237. cfg.flag("-ffreestanding");
  2238. cfg.define("VISIBILITY_HIDDEN", None);
  2239. }
  2240. // NOTE Most of the ARM intrinsics are written in assembly. Tell gcc which arch we are going
  2241. // to target to make sure that the assembly implementations really work for the target. If
  2242. // the implementation is not valid for the arch, then gcc will error when compiling it.
  2243. if llvm_target[0].starts_with("thumb") {
  2244. cfg.flag("-mthumb");
  2245. if llvm_target.last() == Some(&"eabihf") {
  2246. cfg.flag("-mfloat-abi=hard");
  2247. }
  2248. }
  2249. if llvm_target[0] == "thumbv6m" {
  2250. cfg.flag("-march=armv6-m");
  2251. }
  2252. if llvm_target[0] == "thumbv7m" {
  2253. cfg.flag("-march=armv7-m");
  2254. }
  2255. if llvm_target[0] == "thumbv7em" {
  2256. cfg.flag("-march=armv7e-m");
  2257. }
  2258. let mut sources = Sources::new();
  2259. sources.extend(
  2260. &[
  2261. "absvdi2.c",
  2262. "absvsi2.c",
  2263. "addvdi3.c",
  2264. "addvsi3.c",
  2265. "apple_versioning.c",
  2266. "clzdi2.c",
  2267. "clzsi2.c",
  2268. "cmpdi2.c",
  2269. "comparedf2.c",
  2270. "comparesf2.c",
  2271. "ctzdi2.c",
  2272. "ctzsi2.c",
  2273. "divdc3.c",
  2274. "divdf3.c",
  2275. "divsc3.c",
  2276. "divsf3.c",
  2277. "divxc3.c",
  2278. "extendsfdf2.c",
  2279. "extendhfsf2.c",
  2280. "ffsdi2.c",
  2281. "fixdfdi.c",
  2282. "fixdfsi.c",
  2283. "fixsfdi.c",
  2284. "fixsfsi.c",
  2285. "fixunsdfdi.c",
  2286. "fixunsdfsi.c",
  2287. "fixunssfdi.c",
  2288. "fixunssfsi.c",
  2289. "fixunsxfdi.c",
  2290. "fixunsxfsi.c",
  2291. "fixxfdi.c",
  2292. "floatdidf.c",
  2293. "floatdisf.c",
  2294. "floatdixf.c",
  2295. "floatsidf.c",
  2296. "floatsisf.c",
  2297. "floatundidf.c",
  2298. "floatundisf.c",
  2299. "floatundixf.c",
  2300. "floatunsidf.c",
  2301. "floatunsisf.c",
  2302. "int_util.c",
  2303. "muldc3.c",
  2304. "muldf3.c",
  2305. "mulsc3.c",
  2306. "mulsf3.c",
  2307. "mulvdi3.c",
  2308. "mulvsi3.c",
  2309. "mulxc3.c",
  2310. "negdf2.c",
  2311. "negdi2.c",
  2312. "negsf2.c",
  2313. "negvdi2.c",
  2314. "negvsi2.c",
  2315. "paritydi2.c",
  2316. "paritysi2.c",
  2317. "popcountdi2.c",
  2318. "popcountsi2.c",
  2319. "powixf2.c",
  2320. "subvdi3.c",
  2321. "subvsi3.c",
  2322. "truncdfhf2.c",
  2323. "truncdfsf2.c",
  2324. "truncsfhf2.c",
  2325. "ucmpdi2.c",
  2326. ],
  2327. );
  2328. if target_os != "ios" {
  2329. sources.extend(
  2330. &[
  2331. "absvti2.c",
  2332. "addvti3.c",
  2333. "clzti2.c",
  2334. "cmpti2.c",
  2335. "ctzti2.c",
  2336. "ffsti2.c",
  2337. "fixdfti.c",
  2338. "fixsfti.c",
  2339. "fixunsdfti.c",
  2340. "fixunssfti.c",
  2341. "fixunsxfti.c",
  2342. "fixxfti.c",
  2343. "floattidf.c",
  2344. "floattisf.c",
  2345. "floattixf.c",
  2346. "floatuntidf.c",
  2347. "floatuntisf.c",
  2348. "floatuntixf.c",
  2349. "mulvti3.c",
  2350. "negti2.c",
  2351. "negvti2.c",
  2352. "parityti2.c",
  2353. "popcountti2.c",
  2354. "subvti3.c",
  2355. "ucmpti2.c",
  2356. ],
  2357. );
  2358. }
  2359. if target_vendor == "apple" {
  2360. sources.extend(
  2361. &[
  2362. "atomic_flag_clear.c",
  2363. "atomic_flag_clear_explicit.c",
  2364. "atomic_flag_test_and_set.c",
  2365. "atomic_flag_test_and_set_explicit.c",
  2366. "atomic_signal_fence.c",
  2367. "atomic_thread_fence.c",
  2368. ],
  2369. );
  2370. }
  2371. if target_env == "msvc" {
  2372. if target_arch == "x86_64" {
  2373. sources.extend(
  2374. &[
  2375. "x86_64/floatdidf.c",
  2376. "x86_64/floatdisf.c",
  2377. "x86_64/floatdixf.c",
  2378. ],
  2379. );
  2380. }
  2381. } else {
  2382. if target_os != "freebsd" && target_os != "netbsd" {
  2383. sources.extend(&["gcc_personality_v0.c"]);
  2384. }
  2385. if target_arch == "x86_64" {
  2386. sources.extend(
  2387. &[
  2388. "x86_64/chkstk.S",
  2389. "x86_64/chkstk2.S",
  2390. "x86_64/floatdidf.c",
  2391. "x86_64/floatdisf.c",
  2392. "x86_64/floatdixf.c",
  2393. "x86_64/floatundidf.S",
  2394. "x86_64/floatundisf.S",
  2395. "x86_64/floatundixf.S",
  2396. ],
  2397. );
  2398. }
  2399. if target_arch == "x86" {
  2400. sources.extend(
  2401. &[
  2402. "i386/ashldi3.S",
  2403. "i386/ashrdi3.S",
  2404. "i386/chkstk.S",
  2405. "i386/chkstk2.S",
  2406. "i386/divdi3.S",
  2407. "i386/floatdidf.S",
  2408. "i386/floatdisf.S",
  2409. "i386/floatdixf.S",
  2410. "i386/floatundidf.S",
  2411. "i386/floatundisf.S",
  2412. "i386/floatundixf.S",
  2413. "i386/lshrdi3.S",
  2414. "i386/moddi3.S",
  2415. "i386/muldi3.S",
  2416. "i386/udivdi3.S",
  2417. "i386/umoddi3.S",
  2418. ],
  2419. );
  2420. }
  2421. }
  2422. if target_arch == "arm" && target_os != "ios" {
  2423. sources.extend(
  2424. &[
  2425. "arm/aeabi_cdcmp.S",
  2426. "arm/aeabi_cdcmpeq_check_nan.c",
  2427. "arm/aeabi_cfcmp.S",
  2428. "arm/aeabi_cfcmpeq_check_nan.c",
  2429. "arm/aeabi_dcmp.S",
  2430. "arm/aeabi_div0.c",
  2431. "arm/aeabi_drsub.c",
  2432. "arm/aeabi_fcmp.S",
  2433. "arm/aeabi_frsub.c",
  2434. "arm/bswapdi2.S",
  2435. "arm/bswapsi2.S",
  2436. "arm/clzdi2.S",
  2437. "arm/clzsi2.S",
  2438. "arm/comparesf2.S",
  2439. "arm/divmodsi4.S",
  2440. "arm/divsi3.S",
  2441. "arm/modsi3.S",
  2442. "arm/switch16.S",
  2443. "arm/switch32.S",
  2444. "arm/switch8.S",
  2445. "arm/switchu8.S",
  2446. "arm/sync_synchronize.S",
  2447. "arm/udivmodsi4.S",
  2448. "arm/udivsi3.S",
  2449. "arm/umodsi3.S",
  2450. ],
  2451. );
  2452. }
  2453. if llvm_target[0] == "armv7" {
  2454. sources.extend(
  2455. &[
  2456. "arm/sync_fetch_and_add_4.S",
  2457. "arm/sync_fetch_and_add_8.S",
  2458. "arm/sync_fetch_and_and_4.S",
  2459. "arm/sync_fetch_and_and_8.S",
  2460. "arm/sync_fetch_and_max_4.S",
  2461. "arm/sync_fetch_and_max_8.S",
  2462. "arm/sync_fetch_and_min_4.S",
  2463. "arm/sync_fetch_and_min_8.S",
  2464. "arm/sync_fetch_and_nand_4.S",
  2465. "arm/sync_fetch_and_nand_8.S",
  2466. "arm/sync_fetch_and_or_4.S",
  2467. "arm/sync_fetch_and_or_8.S",
  2468. "arm/sync_fetch_and_sub_4.S",
  2469. "arm/sync_fetch_and_sub_8.S",
  2470. "arm/sync_fetch_and_umax_4.S",
  2471. "arm/sync_fetch_and_umax_8.S",
  2472. "arm/sync_fetch_and_umin_4.S",
  2473. "arm/sync_fetch_and_umin_8.S",
  2474. "arm/sync_fetch_and_xor_4.S",
  2475. "arm/sync_fetch_and_xor_8.S",
  2476. ],
  2477. );
  2478. }
  2479. if llvm_target.last().unwrap().ends_with("eabihf") {
  2480. if !llvm_target[0].starts_with("thumbv7em") {
  2481. sources.extend(
  2482. &[
  2483. "arm/adddf3vfp.S",
  2484. "arm/addsf3vfp.S",
  2485. "arm/divdf3vfp.S",
  2486. "arm/divsf3vfp.S",
  2487. "arm/eqdf2vfp.S",
  2488. "arm/eqsf2vfp.S",
  2489. "arm/extendsfdf2vfp.S",
  2490. "arm/fixdfsivfp.S",
  2491. "arm/fixsfsivfp.S",
  2492. "arm/fixunsdfsivfp.S",
  2493. "arm/fixunssfsivfp.S",
  2494. "arm/floatsidfvfp.S",
  2495. "arm/floatsisfvfp.S",
  2496. "arm/floatunssidfvfp.S",
  2497. "arm/floatunssisfvfp.S",
  2498. "arm/gedf2vfp.S",
  2499. "arm/gesf2vfp.S",
  2500. "arm/gtdf2vfp.S",
  2501. "arm/gtsf2vfp.S",
  2502. "arm/ledf2vfp.S",
  2503. "arm/lesf2vfp.S",
  2504. "arm/ltdf2vfp.S",
  2505. "arm/ltsf2vfp.S",
  2506. "arm/muldf3vfp.S",
  2507. "arm/mulsf3vfp.S",
  2508. "arm/nedf2vfp.S",
  2509. "arm/nesf2vfp.S",
  2510. "arm/restore_vfp_d8_d15_regs.S",
  2511. "arm/save_vfp_d8_d15_regs.S",
  2512. "arm/subdf3vfp.S",
  2513. "arm/subsf3vfp.S",
  2514. ],
  2515. );
  2516. }
  2517. sources.extend(&["arm/negdf2vfp.S", "arm/negsf2vfp.S"]);
  2518. }
  2519. if target_arch == "aarch64" {
  2520. sources.extend(
  2521. &[
  2522. "comparetf2.c",
  2523. "extenddftf2.c",
  2524. "extendsftf2.c",
  2525. "fixtfdi.c",
  2526. "fixtfsi.c",
  2527. "fixtfti.c",
  2528. "fixunstfdi.c",
  2529. "fixunstfsi.c",
  2530. "fixunstfti.c",
  2531. "floatditf.c",
  2532. "floatsitf.c",
  2533. "floatunditf.c",
  2534. "floatunsitf.c",
  2535. "multc3.c",
  2536. "trunctfdf2.c",
  2537. "trunctfsf2.c",
  2538. ],
  2539. );
  2540. }
  2541. // Remove the assembly implementations that won't compile for the target
  2542. if llvm_target[0] == "thumbv6m" {
  2543. sources.remove(
  2544. &[
  2545. "aeabi_cdcmp",
  2546. "aeabi_cfcmp",
  2547. "aeabi_dcmp",
  2548. "aeabi_fcmp",
  2549. "clzdi2",
  2550. "clzsi2",
  2551. "comparesf2",
  2552. "divmodsi4",
  2553. "divsi3",
  2554. "modsi3",
  2555. "switch16",
  2556. "switch32",
  2557. "switch8",
  2558. "switchu8",
  2559. "udivmodsi4",
  2560. "udivsi3",
  2561. "umodsi3",
  2562. ],
  2563. );
  2564. // But use some generic implementations where possible
  2565. sources.extend(&["clzdi2.c", "clzsi2.c"])
  2566. }
  2567. if llvm_target[0] == "thumbv7m" || llvm_target[0] == "thumbv7em" {
  2568. sources.remove(&["aeabi_cdcmp", "aeabi_cfcmp"]);
  2569. }
  2570. let root = if env::var_os("CARGO_FEATURE_RUSTBUILD").is_some() {
  2571. Path::new("../../libcompiler_builtins")
  2572. } else {
  2573. Path::new(".")
  2574. };
  2575. let src_dir = root.join("compiler-rt/lib/builtins");
  2576. for src in sources.map.values() {
  2577. let src = src_dir.join(src);
  2578. cfg.file(&src);
  2579. println!("cargo:rerun-if-changed={}", src.display());
  2580. }
  2581. cfg.compile("libcompiler-rt.a");
  2582. }
  2583. }