123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443 |
- use rand::seq::SliceRandom;
- use rand::Rng;
- use std::collections::HashMap;
- use std::fmt;
- use std::fmt::Write as FmtWrite;
- use std::fs::{self, OpenOptions};
- use std::hash::{Hash, Hasher};
- use std::io::Write;
- use std::path::PathBuf;
- use std::{env, mem};
- const NTESTS: usize = 1_000;
- fn main() {
- let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
- let out_file = out_dir.join("generated.rs");
- drop(fs::remove_file(&out_file));
- let target = env::var("TARGET").unwrap();
- let target_arch_arm = target.contains("arm") || target.contains("thumb");
- let target_arch_mips = target.contains("mips");
- // TODO accept NaNs. We don't do that right now because we can't check
- // for NaN-ness on the thumb targets (due to missing intrinsics)
- // float/add.rs
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0 + b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::add::__adddf3(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- let c = a.0 + b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::add::__addsf3(a, b)",
- );
- if target_arch_arm {
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0 + b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::add::__adddf3vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- let c = a.0 + b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::add::__addsf3vfp(a, b)",
- );
- }
- // float/cmp.rs
- gen(
- |(a, b): (MyF64, MyF64)| {
- let (a, b) = (a.0, b.0);
- if a.is_nan() || b.is_nan() {
- return None;
- }
- if a.is_nan() || b.is_nan() {
- Some(-1)
- } else if a < b {
- Some(-1)
- } else if a > b {
- Some(1)
- } else {
- Some(0)
- }
- },
- "builtins::float::cmp::__gedf2(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- let (a, b) = (a.0, b.0);
- if a.is_nan() || b.is_nan() {
- return None;
- }
- if a.is_nan() || b.is_nan() {
- Some(-1)
- } else if a < b {
- Some(-1)
- } else if a > b {
- Some(1)
- } else {
- Some(0)
- }
- },
- "builtins::float::cmp::__gesf2(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- let (a, b) = (a.0, b.0);
- if a.is_nan() || b.is_nan() {
- return None;
- }
- if a.is_nan() || b.is_nan() {
- Some(1)
- } else if a < b {
- Some(-1)
- } else if a > b {
- Some(1)
- } else {
- Some(0)
- }
- },
- "builtins::float::cmp::__ledf2(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- let (a, b) = (a.0, b.0);
- if a.is_nan() || b.is_nan() {
- return None;
- }
- if a.is_nan() || b.is_nan() {
- Some(1)
- } else if a < b {
- Some(-1)
- } else if a > b {
- Some(1)
- } else {
- Some(0)
- }
- },
- "builtins::float::cmp::__lesf2(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- let c = a.0.is_nan() || b.0.is_nan();
- Some(c as i32)
- },
- "builtins::float::cmp::__unordsf2(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0.is_nan() || b.0.is_nan();
- Some(c as i32)
- },
- "builtins::float::cmp::__unorddf2(a, b)",
- );
- if target_arch_arm {
- gen(
- |(a, b): (MyF32, MyF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 <= b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_fcmple(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 >= b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_fcmpge(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 == b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_fcmpeq(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 < b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_fcmplt(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 > b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_fcmpgt(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 <= b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_dcmple(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 >= b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_dcmpge(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 == b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_dcmpeq(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 < b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_dcmplt(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- let c = (a.0 > b.0) as i32;
- Some(c)
- },
- "builtins::float::cmp::__aeabi_dcmpgt(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 >= b.0) as i32)
- },
- "builtins::float::cmp::__gesf2vfp(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 >= b.0) as i32)
- },
- "builtins::float::cmp::__gedf2vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 > b.0) as i32)
- },
- "builtins::float::cmp::__gtsf2vfp(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 > b.0) as i32)
- },
- "builtins::float::cmp::__gtdf2vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 < b.0) as i32)
- },
- "builtins::float::cmp::__ltsf2vfp(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 < b.0) as i32)
- },
- "builtins::float::cmp::__ltdf2vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 <= b.0) as i32)
- },
- "builtins::float::cmp::__lesf2vfp(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 <= b.0) as i32)
- },
- "builtins::float::cmp::__ledf2vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 != b.0) as i32)
- },
- "builtins::float::cmp::__nesf2vfp(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 != b.0) as i32)
- },
- "builtins::float::cmp::__nedf2vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 == b.0) as i32)
- },
- "builtins::float::cmp::__eqsf2vfp(a, b)",
- );
- gen(
- |(a, b): (MyF64, MyF64)| {
- if a.0.is_nan() || b.0.is_nan() {
- return None;
- }
- Some((a.0 == b.0) as i32)
- },
- "builtins::float::cmp::__eqdf2vfp(a, b)",
- );
- }
- // float/extend.rs
- gen(
- |a: MyF32| {
- if a.0.is_nan() {
- return None;
- }
- Some(f64::from(a.0))
- },
- "builtins::float::extend::__extendsfdf2(a)",
- );
- if target_arch_arm {
- gen(
- |a: LargeF32| {
- if a.0.is_nan() {
- return None;
- }
- Some(f64::from(a.0))
- },
- "builtins::float::extend::__extendsfdf2vfp(a)",
- );
- }
- // float/conv.rs
- gen(
- |a: MyF64| i64::cast(a.0),
- "builtins::float::conv::__fixdfdi(a)",
- );
- gen(
- |a: MyF64| i32::cast(a.0),
- "builtins::float::conv::__fixdfsi(a)",
- );
- gen(
- |a: MyF32| i64::cast(a.0),
- "builtins::float::conv::__fixsfdi(a)",
- );
- gen(
- |a: MyF32| i32::cast(a.0),
- "builtins::float::conv::__fixsfsi(a)",
- );
- gen(
- |a: MyF32| i128::cast(a.0),
- "builtins::float::conv::__fixsfti(a)",
- );
- gen(
- |a: MyF64| i128::cast(a.0),
- "builtins::float::conv::__fixdfti(a)",
- );
- gen(
- |a: MyF64| u64::cast(a.0),
- "builtins::float::conv::__fixunsdfdi(a)",
- );
- gen(
- |a: MyF64| u32::cast(a.0),
- "builtins::float::conv::__fixunsdfsi(a)",
- );
- gen(
- |a: MyF32| u64::cast(a.0),
- "builtins::float::conv::__fixunssfdi(a)",
- );
- gen(
- |a: MyF32| u32::cast(a.0),
- "builtins::float::conv::__fixunssfsi(a)",
- );
- gen(
- |a: MyF32| u128::cast(a.0),
- "builtins::float::conv::__fixunssfti(a)",
- );
- gen(
- |a: MyF64| u128::cast(a.0),
- "builtins::float::conv::__fixunsdfti(a)",
- );
- gen(
- |a: MyI64| Some(a.0 as f64),
- "builtins::float::conv::__floatdidf(a)",
- );
- gen(
- |a: MyI32| Some(a.0 as f64),
- "builtins::float::conv::__floatsidf(a)",
- );
- gen(
- |a: MyI32| Some(a.0 as f32),
- "builtins::float::conv::__floatsisf(a)",
- );
- gen(
- |a: MyU64| Some(a.0 as f64),
- "builtins::float::conv::__floatundidf(a)",
- );
- gen(
- |a: MyU32| Some(a.0 as f64),
- "builtins::float::conv::__floatunsidf(a)",
- );
- gen(
- |a: MyU32| Some(a.0 as f32),
- "builtins::float::conv::__floatunsisf(a)",
- );
- gen(
- |a: MyU128| Some(a.0 as f32),
- "builtins::float::conv::__floatuntisf(a)",
- );
- if !target_arch_mips {
- gen(
- |a: MyI128| Some(a.0 as f32),
- "builtins::float::conv::__floattisf(a)",
- );
- gen(
- |a: MyI128| Some(a.0 as f64),
- "builtins::float::conv::__floattidf(a)",
- );
- gen(
- |a: MyU128| Some(a.0 as f64),
- "builtins::float::conv::__floatuntidf(a)",
- );
- }
- // float/pow.rs
- gen(
- |(a, b): (MyF64, MyI32)| {
- let c = a.0.powi(b.0);
- if a.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::pow::__powidf2(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyI32)| {
- let c = a.0.powi(b.0);
- if a.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::pow::__powisf2(a, b)",
- );
- // float/sub.rs
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0 - b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::sub::__subdf3(a, b)",
- );
- gen(
- |(a, b): (MyF32, MyF32)| {
- let c = a.0 - b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::sub::__subsf3(a, b)",
- );
- if target_arch_arm {
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0 - b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::sub::__subdf3vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- let c = a.0 - b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::sub::__subsf3vfp(a, b)",
- );
- }
- // float/mul.rs
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0 * b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::mul::__muldf3(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- let c = a.0 * b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::mul::__mulsf3(a, b)",
- );
- if target_arch_arm {
- gen(
- |(a, b): (MyF64, MyF64)| {
- let c = a.0 * b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::mul::__muldf3vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- let c = a.0 * b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::mul::__mulsf3vfp(a, b)",
- );
- }
- // float/div.rs
- gen(
- |(a, b): (MyF64, MyF64)| {
- if b.0 == 0.0 {
- return None;
- }
- let c = a.0 / b.0;
- if a.0.is_nan()
- || b.0.is_nan()
- || c.is_nan()
- || c.abs() <= f64::from_bits(4503599627370495u64)
- {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::div::__divdf3(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if b.0 == 0.0 {
- return None;
- }
- let c = a.0 / b.0;
- if a.0.is_nan() || b.0.is_nan() || c.is_nan() || c.abs() <= f32::from_bits(16777215u32)
- {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::div::__divsf3(a, b)",
- );
- if target_arch_arm {
- gen(
- |(a, b): (MyF64, MyF64)| {
- if b.0 == 0.0 {
- return None;
- }
- let c = a.0 / b.0;
- if a.0.is_nan()
- || b.0.is_nan()
- || c.is_nan()
- || c.abs() <= f64::from_bits(4503599627370495u64)
- {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::div::__divdf3vfp(a, b)",
- );
- gen(
- |(a, b): (LargeF32, LargeF32)| {
- if b.0 == 0.0 {
- return None;
- }
- let c = a.0 / b.0;
- if a.0.is_nan()
- || b.0.is_nan()
- || c.is_nan()
- || c.abs() <= f32::from_bits(16777215u32)
- {
- None
- } else {
- Some(c)
- }
- },
- "builtins::float::div::__divsf3vfp(a, b)",
- );
- }
- // int/addsub.rs
- gen(
- |(a, b): (MyU128, MyU128)| Some(a.0.wrapping_add(b.0)),
- "builtins::int::addsub::__rust_u128_add(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| Some(a.0.wrapping_add(b.0)),
- "builtins::int::addsub::__rust_i128_add(a, b)",
- );
- gen(
- |(a, b): (MyU128, MyU128)| Some(a.0.overflowing_add(b.0)),
- "builtins::int::addsub::__rust_u128_addo(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| Some(a.0.overflowing_add(b.0)),
- "builtins::int::addsub::__rust_i128_addo(a, b)",
- );
- gen(
- |(a, b): (MyU128, MyU128)| Some(a.0.wrapping_sub(b.0)),
- "builtins::int::addsub::__rust_u128_sub(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| Some(a.0.wrapping_sub(b.0)),
- "builtins::int::addsub::__rust_i128_sub(a, b)",
- );
- gen(
- |(a, b): (MyU128, MyU128)| Some(a.0.overflowing_sub(b.0)),
- "builtins::int::addsub::__rust_u128_subo(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| Some(a.0.overflowing_sub(b.0)),
- "builtins::int::addsub::__rust_i128_subo(a, b)",
- );
- // int/mul.rs
- gen(
- |(a, b): (MyU64, MyU64)| Some(a.0.wrapping_mul(b.0)),
- "builtins::int::mul::__muldi3(a, b)",
- );
- gen(
- |(a, b): (MyI64, MyI64)| Some(a.0.overflowing_mul(b.0)),
- "{
- let mut o = 2;
- let c = builtins::int::mul::__mulodi4(a, b, &mut o);
- (c, match o { 0 => false, 1 => true, _ => panic!() })
- }",
- );
- gen(
- |(a, b): (MyI32, MyI32)| Some(a.0.overflowing_mul(b.0)),
- "{
- let mut o = 2;
- let c = builtins::int::mul::__mulosi4(a, b, &mut o);
- (c, match o { 0 => false, 1 => true, _ => panic!() })
- }",
- );
- gen(
- |(a, b): (MyI128, MyI128)| Some(a.0.wrapping_mul(b.0)),
- "builtins::int::mul::__multi3(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| Some(a.0.overflowing_mul(b.0)),
- "{
- let mut o = 2;
- let c = builtins::int::mul::__muloti4(a, b, &mut o);
- (c, match o { 0 => false, 1 => true, _ => panic!() })
- }",
- );
- // int/sdiv.rs
- gen(
- |(a, b): (MyI64, MyI64)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 / b.0)
- }
- },
- "builtins::int::sdiv::__divdi3(a, b)",
- );
- gen(
- |(a, b): (MyI64, MyI64)| {
- if b.0 == 0 {
- None
- } else {
- Some((a.0 / b.0, a.0 % b.0))
- }
- },
- "{
- let mut r = 0;
- (builtins::int::sdiv::__divmoddi4(a, b, &mut r), r)
- }",
- );
- gen(
- |(a, b): (MyI32, MyI32)| {
- if b.0 == 0 {
- None
- } else {
- Some((a.0 / b.0, a.0 % b.0))
- }
- },
- "{
- let mut r = 0;
- (builtins::int::sdiv::__divmodsi4(a, b, &mut r), r)
- }",
- );
- gen(
- |(a, b): (MyI128, MyI128)| {
- if b.0 == 0 {
- None
- } else {
- Some((a.0 / b.0, a.0 % b.0))
- }
- },
- "{
- let mut r = 0;
- (builtins::int::sdiv::__divmodti4(a, b, &mut r), r)
- }",
- );
- gen(
- |(a, b): (MyI32, MyI32)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 / b.0)
- }
- },
- "builtins::int::sdiv::__divsi3(a, b)",
- );
- gen(
- |(a, b): (MyI32, MyI32)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 % b.0)
- }
- },
- "builtins::int::sdiv::__modsi3(a, b)",
- );
- gen(
- |(a, b): (MyI64, MyI64)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 % b.0)
- }
- },
- "builtins::int::sdiv::__moddi3(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 / b.0)
- }
- },
- "builtins::int::sdiv::__divti3(a, b)",
- );
- gen(
- |(a, b): (MyI128, MyI128)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 % b.0)
- }
- },
- "builtins::int::sdiv::__modti3(a, b)",
- );
- // int/shift.rs
- gen(
- |(a, b): (MyU32, MyU32)| Some(a.0 << (b.0 % 32)),
- "builtins::int::shift::__ashlsi3(a, b % 32)",
- );
- gen(
- |(a, b): (MyU64, MyU32)| Some(a.0 << (b.0 % 64)),
- "builtins::int::shift::__ashldi3(a, b % 64)",
- );
- gen(
- |(a, b): (MyU128, MyU32)| Some(a.0 << (b.0 % 128)),
- "builtins::int::shift::__ashlti3(a, b % 128)",
- );
- gen(
- |(a, b): (MyI32, MyU32)| Some(a.0 >> (b.0 % 32)),
- "builtins::int::shift::__ashrsi3(a, b % 32)",
- );
- gen(
- |(a, b): (MyI64, MyU32)| Some(a.0 >> (b.0 % 64)),
- "builtins::int::shift::__ashrdi3(a, b % 64)",
- );
- gen(
- |(a, b): (MyI128, MyU32)| Some(a.0 >> (b.0 % 128)),
- "builtins::int::shift::__ashrti3(a, b % 128)",
- );
- gen(
- |(a, b): (MyU32, MyU32)| Some(a.0 >> (b.0 % 32)),
- "builtins::int::shift::__lshrsi3(a, b % 32)",
- );
- gen(
- |(a, b): (MyU64, MyU32)| Some(a.0 >> (b.0 % 64)),
- "builtins::int::shift::__lshrdi3(a, b % 64)",
- );
- gen(
- |(a, b): (MyU128, MyU32)| Some(a.0 >> (b.0 % 128)),
- "builtins::int::shift::__lshrti3(a, b % 128)",
- );
- // int/udiv.rs
- gen(
- |(a, b): (MyU64, MyU64)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 / b.0)
- }
- },
- "builtins::int::udiv::__udivdi3(a, b)",
- );
- gen(
- |(a, b): (MyU64, MyU64)| {
- if b.0 == 0 {
- None
- } else {
- Some((a.0 / b.0, a.0 % b.0))
- }
- },
- "{
- let mut r = 0;
- (builtins::int::udiv::__udivmoddi4(a, b, Some(&mut r)), r)
- }",
- );
- gen(
- |(a, b): (MyU32, MyU32)| {
- if b.0 == 0 {
- None
- } else {
- Some((a.0 / b.0, a.0 % b.0))
- }
- },
- "{
- let mut r = 0;
- (builtins::int::udiv::__udivmodsi4(a, b, Some(&mut r)), r)
- }",
- );
- gen(
- |(a, b): (MyU32, MyU32)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 / b.0)
- }
- },
- "builtins::int::udiv::__udivsi3(a, b)",
- );
- gen(
- |(a, b): (MyU32, MyU32)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 % b.0)
- }
- },
- "builtins::int::udiv::__umodsi3(a, b)",
- );
- gen(
- |(a, b): (MyU64, MyU64)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 % b.0)
- }
- },
- "builtins::int::udiv::__umoddi3(a, b)",
- );
- gen(
- |(a, b): (MyU128, MyU128)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 / b.0)
- }
- },
- "builtins::int::udiv::__udivti3(a, b)",
- );
- gen(
- |(a, b): (MyU128, MyU128)| {
- if b.0 == 0 {
- None
- } else {
- Some(a.0 % b.0)
- }
- },
- "builtins::int::udiv::__umodti3(a, b)",
- );
- gen(
- |(a, b): (MyU128, MyU128)| {
- if b.0 == 0 {
- None
- } else {
- Some((a.0 / b.0, a.0 % b.0))
- }
- },
- "{
- let mut r = 0;
- (builtins::int::udiv::__udivmodti4(a, b, Some(&mut r)), r)
- }",
- );
- }
- macro_rules! gen_float {
- ($name:ident,
- $fty:ident,
- $uty:ident,
- $bits:expr,
- $significand_bits:expr) => {
- pub fn $name<R>(rng: &mut R) -> $fty
- where
- R: Rng + ?Sized,
- {
- const BITS: u8 = $bits;
- const SIGNIFICAND_BITS: u8 = $significand_bits;
- const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
- const SIGN_MASK: $uty = (1 << (BITS - 1));
- const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
- fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
- unsafe {
- mem::transmute(
- ((sign as $uty) << (BITS - 1))
- | ((exponent & EXPONENT_MASK) << SIGNIFICAND_BITS)
- | (significand & SIGNIFICAND_MASK),
- )
- }
- }
- if rng.gen_range(0, 10) == 1 {
- // Special values
- *[
- -0.0,
- 0.0,
- ::std::$fty::MIN,
- ::std::$fty::MIN_POSITIVE,
- ::std::$fty::MAX,
- ::std::$fty::NAN,
- ::std::$fty::INFINITY,
- -::std::$fty::INFINITY,
- ]
- .choose(rng)
- .unwrap()
- } else if rng.gen_range(0, 10) == 1 {
- // NaN patterns
- mk_f32(rng.gen(), rng.gen(), 0)
- } else if rng.gen() {
- // Denormalized
- mk_f32(rng.gen(), 0, rng.gen())
- } else {
- // Random anything
- mk_f32(rng.gen(), rng.gen(), rng.gen())
- }
- }
- };
- }
- gen_float!(gen_f32, f32, u32, 32, 23);
- gen_float!(gen_f64, f64, u64, 64, 52);
- macro_rules! gen_large_float {
- ($name:ident,
- $fty:ident,
- $uty:ident,
- $bits:expr,
- $significand_bits:expr) => {
- pub fn $name<R>(rng: &mut R) -> $fty
- where
- R: Rng + ?Sized,
- {
- const BITS: u8 = $bits;
- const SIGNIFICAND_BITS: u8 = $significand_bits;
- const SIGNIFICAND_MASK: $uty = (1 << SIGNIFICAND_BITS) - 1;
- const SIGN_MASK: $uty = (1 << (BITS - 1));
- const EXPONENT_MASK: $uty = !(SIGN_MASK | SIGNIFICAND_MASK);
- fn mk_f32(sign: bool, exponent: $uty, significand: $uty) -> $fty {
- unsafe {
- mem::transmute(
- ((sign as $uty) << (BITS - 1))
- | ((exponent & EXPONENT_MASK) << SIGNIFICAND_BITS)
- | (significand & SIGNIFICAND_MASK),
- )
- }
- }
- if rng.gen_range(0, 10) == 1 {
- // Special values
- *[
- -0.0,
- 0.0,
- ::std::$fty::MIN,
- ::std::$fty::MIN_POSITIVE,
- ::std::$fty::MAX,
- ::std::$fty::NAN,
- ::std::$fty::INFINITY,
- -::std::$fty::INFINITY,
- ]
- .choose(rng)
- .unwrap()
- } else if rng.gen_range(0, 10) == 1 {
- // NaN patterns
- mk_f32(rng.gen(), rng.gen(), 0)
- } else if rng.gen() {
- // Denormalized
- mk_f32(rng.gen(), 0, rng.gen())
- } else {
- // Random anything
- rng.gen::<$fty>()
- }
- }
- };
- }
- gen_large_float!(gen_large_f32, f32, u32, 32, 23);
- gen_large_float!(gen_large_f64, f64, u64, 64, 52);
- trait TestInput: Hash + Eq + fmt::Debug {
- fn ty_name() -> String;
- fn generate_lets(container: &str, cnt: &mut u8) -> String;
- fn generate_static(&self, dst: &mut String);
- }
- trait TestOutput {
- fn ty_name() -> String;
- fn generate_static(&self, dst: &mut String);
- fn generate_expr(container: &str) -> String;
- }
- fn gen<F, A, R>(mut generate: F, test: &str)
- where
- F: FnMut(A) -> Option<R>,
- A: TestInput + Copy,
- R: TestOutput,
- rand::distributions::Standard: rand::distributions::Distribution<A>,
- {
- let rng = &mut rand::thread_rng();
- let testname = test.split("::").last().unwrap().split("(").next().unwrap();
- let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
- let out_file = out_dir.join("generated.rs");
- let mut testcases = HashMap::new();
- let mut n = NTESTS;
- while n > 0 {
- let input: A = rng.gen();
- if testcases.contains_key(&input) {
- continue;
- }
- let output = match generate(input) {
- Some(o) => o,
- None => continue,
- };
- testcases.insert(input, output);
- n -= 1;
- }
- let mut contents = String::new();
- contents.push_str(&format!("mod {} {{\nuse super::*;\n", testname));
- contents.push_str("#[test]\n");
- contents.push_str("fn test() {\n");
- contents.push_str(&format!(
- "static TESTS: [({}, {}); {}] = [\n",
- A::ty_name(),
- R::ty_name(),
- NTESTS
- ));
- for (input, output) in testcases {
- contents.push_str(" (");
- input.generate_static(&mut contents);
- contents.push_str(", ");
- output.generate_static(&mut contents);
- contents.push_str("),\n");
- }
- contents.push_str("];\n");
- contents.push_str(&format!(
- r#"
- for &(inputs, output) in TESTS.iter() {{
- {}
- assert_eq!({}, {}, "inputs {{:?}}", inputs)
- }}
- "#,
- A::generate_lets("inputs", &mut 0),
- R::generate_expr("output"),
- test,
- ));
- contents.push_str("\n}\n");
- contents.push_str("\n}\n");
- OpenOptions::new()
- .write(true)
- .append(true)
- .create(true)
- .open(out_file)
- .unwrap()
- .write_all(contents.as_bytes())
- .unwrap();
- }
- macro_rules! my_float {
- ($(struct $name:ident($inner:ident) = $gen:ident;)*) => ($(
- #[derive(Debug, Clone, Copy)]
- struct $name($inner);
- impl TestInput for $name {
- fn ty_name() -> String {
- format!("u{}", &stringify!($inner)[1..])
- }
- fn generate_lets(container: &str, cnt: &mut u8) -> String {
- let me = *cnt;
- *cnt += 1;
- format!("let {} = {}::from_bits({});\n",
- (b'a' + me) as char,
- stringify!($inner),
- container)
- }
- fn generate_static(&self, dst: &mut String) {
- write!(dst, "{}", self.0.to_bits()).unwrap();
- }
- }
- impl rand::distributions::Distribution<$name> for rand::distributions::Standard {
- fn sample<R: rand::Rng + ?Sized >(&self, r: &mut R) -> $name {
- $name($gen(r))
- }
- }
- impl Hash for $name {
- fn hash<H: Hasher>(&self, h: &mut H) {
- self.0.to_bits().hash(h)
- }
- }
- impl PartialEq for $name {
- fn eq(&self, other: &$name) -> bool {
- self.0.to_bits() == other.0.to_bits()
- }
- }
- impl Eq for $name {}
- )*)
- }
- my_float! {
- struct MyF64(f64) = gen_f64;
- struct LargeF64(f64) = gen_large_f64;
- struct MyF32(f32) = gen_f32;
- struct LargeF32(f32) = gen_large_f32;
- }
- macro_rules! my_integer {
- ($(struct $name:ident($inner:ident);)*) => ($(
- #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
- struct $name($inner);
- impl TestInput for $name {
- fn ty_name() -> String {
- stringify!($inner).to_string()
- }
- fn generate_lets(container: &str, cnt: &mut u8) -> String {
- let me = *cnt;
- *cnt += 1;
- format!("let {} = {};\n",
- (b'a' + me) as char,
- container)
- }
- fn generate_static(&self, dst: &mut String) {
- write!(dst, "{}", self.0).unwrap();
- }
- }
- impl rand::distributions::Distribution<$name> for rand::distributions::Standard {
- fn sample<R: rand::Rng + ?Sized >(&self, r: &mut R) -> $name {
- let bits = (0 as $inner).count_zeros();
- let mut mk = || {
- if r.gen_range(0, 10) == 1 {
- *[
- ::std::$inner::MAX >> (bits / 2),
- 0,
- ::std::$inner::MIN >> (bits / 2),
- ].choose(r).unwrap()
- } else {
- r.gen::<$inner>()
- }
- };
- let a = mk();
- let b = mk();
- $name((a << (bits / 2)) | (b & (!0 << (bits / 2))))
- }
- }
- )*)
- }
- my_integer! {
- struct MyI32(i32);
- struct MyI64(i64);
- struct MyI128(i128);
- struct MyU16(u16);
- struct MyU32(u32);
- struct MyU64(u64);
- struct MyU128(u128);
- }
- impl<A, B> TestInput for (A, B)
- where
- A: TestInput,
- B: TestInput,
- {
- fn ty_name() -> String {
- format!("({}, {})", A::ty_name(), B::ty_name())
- }
- fn generate_lets(container: &str, cnt: &mut u8) -> String {
- format!(
- "{}{}",
- A::generate_lets(&format!("{}.0", container), cnt),
- B::generate_lets(&format!("{}.1", container), cnt)
- )
- }
- fn generate_static(&self, dst: &mut String) {
- dst.push_str("(");
- self.0.generate_static(dst);
- dst.push_str(", ");
- self.1.generate_static(dst);
- dst.push_str(")");
- }
- }
- impl TestOutput for f64 {
- fn ty_name() -> String {
- "u64".to_string()
- }
- fn generate_static(&self, dst: &mut String) {
- write!(dst, "{}", self.to_bits()).unwrap();
- }
- fn generate_expr(container: &str) -> String {
- format!("f64::from_bits({})", container)
- }
- }
- impl TestOutput for f32 {
- fn ty_name() -> String {
- "u32".to_string()
- }
- fn generate_static(&self, dst: &mut String) {
- write!(dst, "{}", self.to_bits()).unwrap();
- }
- fn generate_expr(container: &str) -> String {
- format!("f32::from_bits({})", container)
- }
- }
- macro_rules! plain_test_output {
- ($($i:tt)*) => ($(
- impl TestOutput for $i {
- fn ty_name() -> String {
- stringify!($i).to_string()
- }
- fn generate_static(&self, dst: &mut String) {
- write!(dst, "{}", self).unwrap();
- }
- fn generate_expr(container: &str) -> String {
- container.to_string()
- }
- }
- )*)
- }
- plain_test_output!(i32 i64 i128 u32 u64 u128 bool);
- impl<A, B> TestOutput for (A, B)
- where
- A: TestOutput,
- B: TestOutput,
- {
- fn ty_name() -> String {
- format!("({}, {})", A::ty_name(), B::ty_name())
- }
- fn generate_static(&self, dst: &mut String) {
- dst.push_str("(");
- self.0.generate_static(dst);
- dst.push_str(", ");
- self.1.generate_static(dst);
- dst.push_str(")");
- }
- fn generate_expr(container: &str) -> String {
- container.to_string()
- }
- }
- trait FromFloat<T>: Sized {
- fn cast(src: T) -> Option<Self>;
- }
- macro_rules! from_float {
- ($($src:ident => $($dst:ident),+);+;) => {
- $(
- $(
- impl FromFloat<$src> for $dst {
- fn cast(src: $src) -> Option<$dst> {
- use std::{$dst, $src};
- if src.is_nan() ||
- src.is_infinite() ||
- src < std::$dst::MIN as $src ||
- src > std::$dst::MAX as $src
- {
- None
- } else {
- Some(src as $dst)
- }
- }
- }
- )+
- )+
- }
- }
- from_float! {
- f32 => i32, i64, i128, u32, u64, u128;
- f64 => i32, i64, i128, u32, u64, u128;
- }
|