From 3b32f5f782cb83d6a0efe30d0aa529e5a93d65c7 Mon Sep 17 00:00:00 2001 From: Wojciech Zmuda Date: Fri, 15 Nov 2024 23:30:24 +0100 Subject: [PATCH] Add xor Signed-off-by: Wojciech Zmuda --- compiler_rt/src/alu.cairo | 1 + compiler_rt/src/alu/xor.cairo | 6 ++ compiler_rt/src/alu/xor/xor_bool.cairo | 35 ++++++++++++ compiler_rt/src/alu/xor/xor_i128.cairo | 64 +++++++++++++++++++++ compiler_rt/src/alu/xor/xor_i16.cairo | 77 ++++++++++++++++++++++++++ compiler_rt/src/alu/xor/xor_i32.cairo | 77 ++++++++++++++++++++++++++ compiler_rt/src/alu/xor/xor_i64.cairo | 77 ++++++++++++++++++++++++++ compiler_rt/src/alu/xor/xor_i8.cairo | 77 ++++++++++++++++++++++++++ 8 files changed, 414 insertions(+) create mode 100644 compiler_rt/src/alu/xor.cairo create mode 100644 compiler_rt/src/alu/xor/xor_bool.cairo create mode 100644 compiler_rt/src/alu/xor/xor_i128.cairo create mode 100644 compiler_rt/src/alu/xor/xor_i16.cairo create mode 100644 compiler_rt/src/alu/xor/xor_i32.cairo create mode 100644 compiler_rt/src/alu/xor/xor_i64.cairo create mode 100644 compiler_rt/src/alu/xor/xor_i8.cairo diff --git a/compiler_rt/src/alu.cairo b/compiler_rt/src/alu.cairo index 388fe64..5891fa8 100644 --- a/compiler_rt/src/alu.cairo +++ b/compiler_rt/src/alu.cairo @@ -1,4 +1,5 @@ pub mod and; pub mod or; +pub mod xor; mod test_case; diff --git a/compiler_rt/src/alu/xor.cairo b/compiler_rt/src/alu/xor.cairo new file mode 100644 index 0000000..84b27f7 --- /dev/null +++ b/compiler_rt/src/alu/xor.cairo @@ -0,0 +1,6 @@ +pub mod xor_bool; +pub mod xor_i8; +pub mod xor_i16; +pub mod xor_i32; +pub mod xor_i64; +pub mod xor_i128; diff --git a/compiler_rt/src/alu/xor/xor_bool.cairo b/compiler_rt/src/alu/xor/xor_bool.cairo new file mode 100644 index 0000000..7bdb455 --- /dev/null +++ b/compiler_rt/src/alu/xor/xor_bool.cairo @@ -0,0 +1,35 @@ +use core::integer::Bitwise; +extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic; + +pub fn __llvm__xor_bool_bool(lhs: u128, rhs: u128) -> u128 { + if lhs > 1 { + panic!("lhs = {:?} does not fit in bool", lhs) + } + + if rhs > 1 { + panic!("rhs = {:?} does not fit in bool", rhs) + }; + + let (_, xor_result, _) = bitwise(lhs, rhs); + xor_result +} + +#[cfg(test)] +mod tests { + use super::__llvm__xor_bool_bool; + use crate::alu::test_case::TestCase; + + pub const test_cases: [TestCase; 4] = [ + TestCase{lhs: 1, rhs: 1, expected: 0}, + TestCase{lhs: 1, rhs: 0, expected: 1}, + TestCase{lhs: 0, rhs: 1, expected: 1}, + TestCase{lhs: 0, rhs: 0, expected: 0}, + ]; + + #[test] + fn test_i1() { + for case in test_cases.span() { + assert_eq!(__llvm__xor_bool_bool(*case.lhs, *case.rhs), *case.expected); + } + } +} diff --git a/compiler_rt/src/alu/xor/xor_i128.cairo b/compiler_rt/src/alu/xor/xor_i128.cairo new file mode 100644 index 0000000..58fe46e --- /dev/null +++ b/compiler_rt/src/alu/xor/xor_i128.cairo @@ -0,0 +1,64 @@ +use core::integer::Bitwise; +extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic; + +pub fn __llvm__xor_i128_i128(lhs: u128, rhs: u128) -> u128 { + let (_, xor_result, _) = bitwise(lhs, rhs); + xor_result +} + +#[cfg(test)] +mod tests { + use super::__llvm__xor_i128_i128; + use crate::alu::test_case::TestCase; + + pub const test_cases: [TestCase; 40] = [ + TestCase{lhs: 155774063519755376060812267429406545289, rhs: 75990042375397402910552784616117410495, expected: 101165902436522780018230107004991901494}, + TestCase{lhs: 14933887807017699604311170175536231465, rhs: 82337626577251459053166483173715936986, expected: 72845651887494455410741796739175903987}, + TestCase{lhs: 159493548208643057672438558652772554881, rhs: 1887606846069570429303823004721348459, expected: 157632235434443646125498671307874330602}, + TestCase{lhs: 49514951070079698630932973157558611198, rhs: 92680826215846764186736404712703051694, expected: 128901813660278483693050212496238422864}, + TestCase{lhs: 117276773349420098306033902726002613216, rhs: 25353773237243727604371812229055813430, expected: 99901633555103033000540287688115293398}, + TestCase{lhs: 70935892499834416475898259716052468580, rhs: 64195126033705355260636445417495751687, expected: 6761863036727532204561390883407638371}, + TestCase{lhs: 68551842448656551712184025360079908519, rhs: 74330481461744918582637022962249344870, expected: 5945776457074190144122289800039516609}, + TestCase{lhs: 39291764956839536061177623957638218003, rhs: 63875187171223396297406532356567244896, expected: 60493996369916002578855337146999153011}, + TestCase{lhs: 77120114119622168013775915984204676035, rhs: 93640030235788192199882580427896596415, expected: 165440263531778934411877006852096793724}, + TestCase{lhs: 157813189006723193701611310530895540023, rhs: 52035740992222368174896195186550061478, expected: 108477768444492996599012072985043195537}, + TestCase{lhs: 0b10110010010100011001110011010001011011100100001010110001100011111000101000010000001001110110110011110010110010011111000010011110, rhs: 91842199782075806295450258077255881205, expected: 0b11110111010010011011011011011100110101101110000110110011001011100101010001110100110100001100000110101001110110111010000101101011}, + TestCase{lhs: 0b11010001111001010011010000100011001100001010100011010011110111010100101111101011110010111101100111010101111110100001100101011101, rhs: 107847225891471846954721747246280348380, expected: 0b10000000110001111010101011100000110100000010010010111110011011101100000011000100101110110101111110000011011010100000101110000001}, + TestCase{lhs: 0b10011110100001011110111100010111100011001011000101111011110011001111000011000010010110000001000111011100001101111011110101001100, rhs: 84562198080835222497499629700895607970, expected: 0b10100001000110111111100101010101100011101001011011000110010101001101110110111100101001010111111111011000001101011011000111101110}, + TestCase{lhs: 0b10101110110001000001111000110011001010000111011101111111101100000000100001010100111001111100111111010110011111000010001010111100, rhs: 31686822745774416475494292193182512721, expected: 0b10111001000100101011011011001001111001000111001110011011110100110101011100011000100001010101001110010000000011000101010011101101}, + TestCase{lhs: 0b11100110111010011100000000100010101011111101011001001110000101110010111010110111011011100101111101101100010111000011000101100011, rhs: 43395049877318420617058950582897019678, expected: 0b11000110010011000101010100000111111011110000111100111110100111011100100001010110000101101111100110110001001110010011101001111101}, + TestCase{lhs: 0b10011110000001011011100011101111010101110101000101110100111001000001010110011001011011011000001000110001110000000100111011001110, rhs: 98863481168425818938123590967208185746, expected: 0b11010100011001011101000100011100101000000100011001011011100001110000100010000110100110011100101111011011010011000100000101011100}, + TestCase{lhs: 0b10011010010101100010001011001100100101010010001010100000000101100101011111011101010101010011110100001111011010100110000101100101, rhs: 111639250910634910250713301385892364579, expected: 0b11001001101010101101001011000100000101000110011010000011010101111010000111110001011001110111000010000101001110101110000001000110}, + TestCase{lhs: 0b10001010101011101000101001011001110010011100111101100100011110001110111000011110010011010001001001001000101010111100000000001101, rhs: 156772358052761543334989949130592695006, expected: 0b11111111010111111100100001011101010100011101011010111011101011101011111001100010000100111100011101011101001011001111001011010011}, + TestCase{lhs: 0b11001010001000110000111101011000110100110111111100010110110110101011111110110100100100110101111101001011110110110101110000110011, rhs: 145656048432106057939332725548469578837, expected: 0b10100111101101110101101011111011101010100110010111100010010100011111001111001100101110111011001000011000011111101011000001100110}, + TestCase{lhs: 0b10001000111001010001001010111110010100011110111001010011111101110110000110100111000100011111000101011000000101001110111011100010, rhs: 168827684723497031425967685922979383176, expected: 0b11110111111001100001010100111101011110011011110111100110111111100101001001001011011000101000100110101000101011011110100101101010}, + TestCase{lhs: 27263947700682630194927543847938106802, rhs: 0b10010101101100110110110101101000100010010111100011101101011100101000101111011110111001010110001110011101100010110001011111111110, expected: 0b10000001001100011011010100001111111000011100100010110011001000010101011101101100100000010000010001111110111100000011001001001100}, + TestCase{lhs: 163777871281112871704542276480284449327, rhs: 0b11110110101101001110001000101010101000001011110001110100000001111110001000011011010001011000000000111101100010111111100100011010, expected: 0b10001101100000101001101001010110000101001001000110101001010100010000111011000001111101101011010110110011010011001010001100110101}, + TestCase{lhs: 162131929702578367007716566966446504619, rhs: 0b11111010110011011100111010011010111101100011110011001010001110110011110011111100000100100000000100111011111010010101101010001101, expected: 0b10000011001101001011011111010111101111110110101111111000001000101000000111111100110001100100100010001010000001111110000000100110}, + TestCase{lhs: 84439894114367077991733645831925910029, rhs: 0b11111011011010011110110100111010110101110100110110100000100001110100001010100111011000111001101001110100110010101100101100000010, expected: 0b11000100111011110110010100001110001110111001011101010100001100110110111101100001011000111010100010100000110101010000000100001111}, + TestCase{lhs: 93449127780662419034630366468130774062, rhs: 0b11010011110101101111010111100101000111111110010111101011001110010111001111001100011011100000011111100101001101100100110101001100, expected: 0b10010101100110110101000001011101100110000011111110010101111010110001000010011011001110000100100011110001010111101101000101100010}, + TestCase{lhs: 69777058703637134683027673239839594456, rhs: 0b10001111010100011000101000111011100101100100001010001011001001111101111101011101100010001010010100111001100001011111001101010111, expected: 0b10111011001011110001100010100000100000101010110110101001111110000001010100000110011010010111011010101011011111011010010010001111}, + TestCase{lhs: 64314754814512571971782397396552784769, rhs: 0b11110000000011010101111000100001011101000000011011011111000101001011110101000110100111111111110000111111011100010001001110000111, expected: 0b11000000011011111100110000011010010011111001010010010100100111101101111101001000011000101101000100101010100101010000100000000110}, + TestCase{lhs: 50679233431278652369851435999178540580, rhs: 0b11010100111100110101011011000010100010011100010010100100001000101100111001100011001101100100011100010110110101110011101110110000, expected: 0b11110010110100110010000111011101110101111111111001010000011010100011010101100000011011101010011110111010011111110111110110010100}, + TestCase{lhs: 121138864359763502618917941272335849809, rhs: 0b11010100100110100010010110101101011000101100100010010100011001100111101110010111111010011100000000001101110000110000001111011110, expected: 0b10001111101110000101101010001110010000010011011111011010111011100101110010011010001111010110001000010011101001001001101010001111}, + TestCase{lhs: 105101168334401616360641982332387234679, rhs: 0b11100100110011101010010000101101000011010111111010111101000111011100001001011110111000010010001110110100001011111001010110001111, expected: 0b10101011110111110001101110000001101000010011011110001001010010001111010001101101100100110001010100010100100010110000011011111000}, + TestCase{lhs: 0b10010001101011011010101000101001000110011001101001000111000011101010101001010011010111111110011110011111000001100011000100000100, rhs: 0b10001100100101000001101111101101011101000011101110011110000110000110101100110100011111001001011100111000101110100101001110011001, expected: 38847178347848272931287716698416505501}, + TestCase{lhs: 0b10101101100000110110001001100000011110100011111100101111111101100000110100101110100011001100100000010001000101111100101111111010, rhs: 0b10111001100011111111110111001111010010100101111100000000011110010011101110011000110001111100001110110010101000110000110011110100, expected: 26650106261027717944634079795228231438}, + TestCase{lhs: 0b10000111111010010010110000110111010111111001100010110011011100100111001101001010001001100011001110110110110100001010101110000101, rhs: 0b11110010111100111001111000110100001001011000000011100110111011100101001000110110001111000000011110011001100011100001000100001111, expected: 155658285769537589887346495563669355146}, + TestCase{lhs: 0b11000110011100000111100110100011011011011110101000101011100001010001001010100001011111001001010101001000011000101101110110010001, rhs: 0b10100110001011110010101000111101100010100001000001101101101101100001011000100101000000110010100000101000001011011101001111111010, expected: 128100851826753112996284044667861798507}, + TestCase{lhs: 0b11111100001001010001101110101001101111000110010101110000010010001011010010100100111100010000000110001110011111011000001110011110, rhs: 0b11011010011100101101111000100100011011010100010101011100110101001011100101101110111001101110010100011010001001111010001101010011, expected: 50966400537103430406071681967942410445}, + TestCase{lhs: 0b10111110100100100011100111011000101111010010101100110110110010011011010010001010110010011110100110101010111010111000010100000110, rhs: 0b10001110000110110100100101100100001110001101111011010111000101100001001100111110011100110101001010101101011101011110110100010100, expected: 64516575033524154743485907977159731218}, + TestCase{lhs: 0b11001011001000011111001011110111110100100101111100000010101111101001110010110110110000001010001011100100001001110000101010011000, rhs: 0b11111001111101111101001110000111001101100000101010001110001001000001101101011000000110000011010100000110010100011100000110101001, expected: 67573229580709373776260792601814682417}, + TestCase{lhs: 0b11011000111001111011000101001110001101010100100001110110000011110100010001011011101101101011110010110000001000001000001010110001, rhs: 0b10100111010011101100101011100001101001000001001111000101111100001110100111110101111001111111011001111010001111001100100010010111, expected: 169691962280072579573441543395931343398}, + TestCase{lhs: 0b11100001100100111101001111000100101111110100000111100010101000110110100100111001111101001011011000111100001010001000101000010100, rhs: 0b10101001110111011110100010001001100111101010110010000110111001000111101111011101001101111110101111011001011010011100010110110110, expected: 96110617624714782742090093020215070626}, + TestCase{lhs: 0b11101011010110110001010000111101110010100111001000000000100110011110110100011110101011001000101000010101100111110100110011111011, rhs: 0b11101011101010100110001000001101100000111100000001000110110101111101110010000101111001011111101101101000111011011100111101110111, expected: 1253740692999851561198043996653519756}, + ]; + + + #[test] + fn test_i128() { + for case in test_cases.span() { + assert_eq!(__llvm__xor_i128_i128(*case.lhs, *case.rhs), *case.expected); + } + } +} diff --git a/compiler_rt/src/alu/xor/xor_i16.cairo b/compiler_rt/src/alu/xor/xor_i16.cairo new file mode 100644 index 0000000..f7f5d14 --- /dev/null +++ b/compiler_rt/src/alu/xor/xor_i16.cairo @@ -0,0 +1,77 @@ +use core::integer::Bitwise; +extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic; + +pub fn __llvm__xor_i16_i16(lhs: u128, rhs: u128) -> u128 { + let _: u16 = match lhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("lhs = {:?} does not fit in u16", lhs) + }, + }; + + let _: u16 = match rhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("rhs = {:?} does not fit in u16", rhs) + }, + }; + + let (_, xor_result, _) = bitwise(lhs, rhs); + xor_result +} + +#[cfg(test)] +mod tests { + use super::__llvm__xor_i16_i16; + use crate::alu::test_case::TestCase; + + pub const test_cases: [TestCase; 40] = [ + TestCase{lhs: 1344, rhs: 25247, expected: 26591}, + TestCase{lhs: 8026, rhs: 28907, expected: 28593}, + TestCase{lhs: 18654, rhs: 7349, expected: 21611}, + TestCase{lhs: 22872, rhs: 6217, expected: 16657}, + TestCase{lhs: 31955, rhs: 6116, expected: 27447}, + TestCase{lhs: 23392, rhs: 17091, expected: 6563}, + TestCase{lhs: 28555, rhs: 2425, expected: 26354}, + TestCase{lhs: 1230, rhs: 16000, expected: 14926}, + TestCase{lhs: 23122, rhs: 16784, expected: 7106}, + TestCase{lhs: 11129, rhs: 10803, expected: 330}, + TestCase{lhs: 0b1000001011110111, rhs: 19954, expected: 0b1100111100000101}, + TestCase{lhs: 0b1111101100011111, rhs: 6022, expected: 0b1110110010011001}, + TestCase{lhs: 0b1010001111110101, rhs: 1516, expected: 0b1010011000011001}, + TestCase{lhs: 0b1000101000100001, rhs: 18709, expected: 0b1100001100110100}, + TestCase{lhs: 0b1001010110011000, rhs: 7156, expected: 0b1000111001101100}, + TestCase{lhs: 0b1011100001111100, rhs: 22820, expected: 0b1110000101011000}, + TestCase{lhs: 0b1100110011001010, rhs: 27054, expected: 0b1010010101100100}, + TestCase{lhs: 0b1000111000111010, rhs: 29856, expected: 0b1111101010011010}, + TestCase{lhs: 0b1111010100011010, rhs: 20274, expected: 0b1011101000101000}, + TestCase{lhs: 0b1010110111000100, rhs: 8669, expected: 0b1000110000011001}, + TestCase{lhs: 25645, rhs: 0b1000111001111001, expected: 0b1110101001010100}, + TestCase{lhs: 17164, rhs: 0b1111110001001000, expected: 0b1011111101000100}, + TestCase{lhs: 3102, rhs: 0b1100001100001010, expected: 0b1100111100010100}, + TestCase{lhs: 20636, rhs: 0b1011010100110001, expected: 0b1110010110101101}, + TestCase{lhs: 26786, rhs: 0b1110111011111010, expected: 0b1000011001011000}, + TestCase{lhs: 3844, rhs: 0b1100001000000011, expected: 0b1100110100000111}, + TestCase{lhs: 32324, rhs: 0b1111001011010110, expected: 0b1000110010010010}, + TestCase{lhs: 10944, rhs: 0b1100110011111011, expected: 0b1110011000111011}, + TestCase{lhs: 28746, rhs: 0b1111010000001101, expected: 0b1000010001000111}, + TestCase{lhs: 22552, rhs: 0b1101110010111010, expected: 0b1000010010100010}, + TestCase{lhs: 0b1110001010100010, rhs: 0b1011100100111100, expected: 23454}, + TestCase{lhs: 0b1101110001100100, rhs: 0b1100000110101110, expected: 7626}, + TestCase{lhs: 0b1110010001111011, rhs: 0b1110010010110000, expected: 203}, + TestCase{lhs: 0b1110000111001110, rhs: 0b1001010111011110, expected: 29712}, + TestCase{lhs: 0b1110100011100110, rhs: 0b1101010001110110, expected: 15504}, + TestCase{lhs: 0b1001001111010110, rhs: 0b1100111111110011, expected: 23589}, + TestCase{lhs: 0b1000110000010010, rhs: 0b1001000101100110, expected: 7540}, + TestCase{lhs: 0b1100100100000100, rhs: 0b1001000001011011, expected: 22879}, + TestCase{lhs: 0b1101100100101001, rhs: 0b1110001101111001, expected: 14928}, + TestCase{lhs: 0b1011000011111111, rhs: 0b1101000111011000, expected: 24871}, + ]; + + #[test] + fn test_i16() { + for case in test_cases.span() { + assert_eq!(__llvm__xor_i16_i16(*case.lhs, *case.rhs), *case.expected); + } + } +} diff --git a/compiler_rt/src/alu/xor/xor_i32.cairo b/compiler_rt/src/alu/xor/xor_i32.cairo new file mode 100644 index 0000000..fca859b --- /dev/null +++ b/compiler_rt/src/alu/xor/xor_i32.cairo @@ -0,0 +1,77 @@ +use core::integer::Bitwise; +extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic; + +pub fn __llvm__xor_i32_i32(lhs: u128, rhs: u128) -> u128 { + let _: u32 = match lhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("lhs = {:?} does not fit in u32", lhs) + }, + }; + + let _: u32 = match rhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("rhs = {:?} does not fit in u32", rhs) + }, + }; + + let (_, xor_result, _) = bitwise(lhs, rhs); + xor_result +} + +#[cfg(test)] +mod tests { + use super::__llvm__xor_i32_i32; + use crate::alu::test_case::TestCase; + + pub const test_cases: [TestCase; 40] = [ + TestCase{lhs: 1576375040, rhs: 369499779, expected: 1274252675}, + TestCase{lhs: 1631719062, rhs: 619524455, expected: 1169113073}, + TestCase{lhs: 2127363942, rhs: 1883578238, expected: 243916824}, + TestCase{lhs: 952404303, rhs: 34230786, expected: 986635085}, + TestCase{lhs: 1676227008, rhs: 1789471527, expected: 155196135}, + TestCase{lhs: 1356367863, rhs: 424323808, expected: 1234316567}, + TestCase{lhs: 726685925, rhs: 1955167165, expected: 1608065880}, + TestCase{lhs: 965599781, rhs: 1391876976, expected: 1803270485}, + TestCase{lhs: 731841051, rhs: 1217993909, expected: 1661345454}, + TestCase{lhs: 861250410, rhs: 649416040, expected: 367062530}, + TestCase{lhs: 0b11000011010101110111001111110011, rhs: 1957044873, expected: 0b10110111111100010101000101111010}, + TestCase{lhs: 0b11011010100101110100100101000010, rhs: 2094097053, expected: 0b10100110010001100010101111011111}, + TestCase{lhs: 0b11111110000011110100000010111110, rhs: 417715679, expected: 0b11100110111010101001010101100001}, + TestCase{lhs: 0b11001100011001100110011010011001, rhs: 481827812, expected: 0b11010000110111100111110101111101}, + TestCase{lhs: 0b10011000011101101101111100001001, rhs: 997162328, expected: 0b10100011000110011010001001010001}, + TestCase{lhs: 0b11101001110110100101001111111001, rhs: 254957999, expected: 0b11100110111010000000101001010110}, + TestCase{lhs: 0b10100001101001100010011110100101, rhs: 1679841804, expected: 0b11000101100001100111110110101001}, + TestCase{lhs: 0b10011100110001110111100010010101, rhs: 637741264, expected: 0b10111010110001000101000001000101}, + TestCase{lhs: 0b10000010110011010001110001100010, rhs: 1424800990, expected: 0b11010110001000011010000010111100}, + TestCase{lhs: 0b10011101011000110101110101010000, rhs: 313762767, expected: 0b10001111110100001111111010011111}, + TestCase{lhs: 1435700735, rhs: 0b10100010011011001110011100000111, expected: 0b11110111111111111110101011111000}, + TestCase{lhs: 947202709, rhs: 0b10001001010011101000101101000000, expected: 0b10110001001110111010000111010101}, + TestCase{lhs: 731038624, rhs: 0b10101011010011011100100010110100, expected: 0b10000000110111110000101100010100}, + TestCase{lhs: 169745211, rhs: 0b11011011000111010001010110001000, expected: 0b11010001000000110000111010110011}, + TestCase{lhs: 2105373088, rhs: 0b11010011100000001101000110101011, expected: 0b10101110111111011010000000001011}, + TestCase{lhs: 1051590284, rhs: 0b11111011000010010101010001111101, expected: 0b11000101101001001010101011110001}, + TestCase{lhs: 881093411, rhs: 0b10101010100110001011000001111100, expected: 0b10011110000111001101101101011111}, + TestCase{lhs: 822547370, rhs: 0b11110110111100101101111001001110, expected: 0b11000111111101011100110111100100}, + TestCase{lhs: 1245489806, rhs: 0b11110100010010110101110101000001, expected: 0b10111110011101111111011111001111}, + TestCase{lhs: 947114356, rhs: 0b10100011001111111011111011100111, expected: 0b10011011010011000110111110010011}, + TestCase{lhs: 0b11100100001110110100100101110100, rhs: 0b11101111111101010101010101111000, expected: 198056972}, + TestCase{lhs: 0b11110000100011010100100111011001, rhs: 0b10000110000001101001101101111111, expected: 1988874918}, + TestCase{lhs: 0b10011010111111001101101000101000, rhs: 0b11010011011000010110110011100001, expected: 1235072713}, + TestCase{lhs: 0b10110010010011110101011111000101, rhs: 0b10110011001001100110100110111000, expected: 23674493}, + TestCase{lhs: 0b10100101111100011010101001100000, rhs: 0b11101000100110110010100000100111, expected: 1298825799}, + TestCase{lhs: 0b10101011011011101001000011111111, rhs: 0b11100101100101110001100001100011, expected: 1324976284}, + TestCase{lhs: 0b11110000111110011111011011010101, rhs: 0b11010011100010110010110010100111, expected: 594729586}, + TestCase{lhs: 0b11101011111110000001101001100010, rhs: 0b10100100100100001011111010101111, expected: 1332257997}, + TestCase{lhs: 0b10010010111111101010001111111101, rhs: 0b10000000000111110110011111010011, expected: 316785710}, + TestCase{lhs: 0b11011011000110000011001001111011, rhs: 0b11010101010011000111110100011011, expected: 240406368}, +]; + + #[test] + fn test_i32() { + for case in test_cases.span() { + assert_eq!(__llvm__xor_i32_i32(*case.lhs, *case.rhs), *case.expected); + } + } +} diff --git a/compiler_rt/src/alu/xor/xor_i64.cairo b/compiler_rt/src/alu/xor/xor_i64.cairo new file mode 100644 index 0000000..5b87272 --- /dev/null +++ b/compiler_rt/src/alu/xor/xor_i64.cairo @@ -0,0 +1,77 @@ +use core::integer::Bitwise; +extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic; + +pub fn __llvm__xor_i64_i64(lhs: u128, rhs: u128) -> u128 { + let _: u64 = match lhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("lhs = {:?} does not fit in u64", lhs) + }, + }; + + let _: u64 = match rhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("rhs = {:?} does not fit in u64", rhs) + }, + }; + + let (_, xor_result, _) = bitwise(lhs, rhs); + xor_result +} + +#[cfg(test)] +mod tests { + use super::__llvm__xor_i64_i64; + use crate::alu::test_case::TestCase; + + pub const test_cases: [TestCase; 40] = [ + TestCase{lhs: 7496132195150255200, rhs: 2997805621602357276, expected: 4728151613721265276}, + TestCase{lhs: 2169360689881416478, rhs: 1301424196279596058, expected: 870470595215881988}, + TestCase{lhs: 8959487128816310470, rhs: 4222671955298961925, expected: 5102455053333992131}, + TestCase{lhs: 3409616542105149260, rhs: 278199016231975820, expected: 3210284395523450048}, + TestCase{lhs: 5252501444729536612, rhs: 3630054505735875419, expected: 8828226710079400767}, + TestCase{lhs: 1647287379396373735, rhs: 7005668748918126630, expected: 8639440896983979201}, + TestCase{lhs: 3830615588852656426, rhs: 5273670383927984752, expected: 8937045812108880730}, + TestCase{lhs: 5172602648638716433, rhs: 29783153385477265, expected: 5161416727318228608}, + TestCase{lhs: 9078749897041463876, rhs: 9134211415009803601, expected: 233358205214750485}, + TestCase{lhs: 8982148582506849615, rhs: 6851179424051344006, expected: 2572478877323679689}, + TestCase{lhs: 0b1111011000000100000011111101011000100101000000110111111010011101, rhs: 7743452082548318699, expected: 0b1001110101110010010010110111001001011110100011100101111101110110}, + TestCase{lhs: 0b1011011110010101010101100011000101010111101010001000011110000000, rhs: 7873712531048876183, expected: 0b1101101011010000010111011110010111011011000111010100111100010111}, + TestCase{lhs: 0b1110001010110101010100000001011111001110111111101000011111110111, rhs: 9117319750141559513, expected: 0b1001110000110010011010100001011000011100001010011100010100101110}, + TestCase{lhs: 0b1100100010110010010001011001000100001000100101011000001110110111, rhs: 4541646638551313536, expected: 0b1111011110110101011011100001111100010010101001110010111100110111}, + TestCase{lhs: 0b1000000000010010001110110011000001100100110010011111111111001111, rhs: 5832508334127524531, expected: 0b1101000011100011000000000101110111101001111001001101010101111100}, + TestCase{lhs: 0b1110001000101110001101111110100100000100100001011101011101100000, rhs: 3397550084275216810, expected: 0b1100110100001000101100100010010111110011010101110111011011001010}, + TestCase{lhs: 0b1000100000010111010111101101000110101000011101100100100001001111, rhs: 2726868358688059627, expected: 0b1010110111000000100101101000001010110100101011001111100010100100}, + TestCase{lhs: 0b1100110000100101000110100101000100100111011110011110110010010010, rhs: 4875423593397623001, expected: 0b1000111110001101111000011011110001110001011111111111010001001011}, + TestCase{lhs: 0b1010011101101101000000001010110101010110011000111011111001000011, rhs: 5221773112137725361, expected: 0b1110111100011010011101100101011111100000110001000000101111110010}, + TestCase{lhs: 0b1011011011110111010111001100000111111001111001100110111110100110, rhs: 1226960165449984601, expected: 0b1010011111110000010101010000010011110110110101010000010111111111}, + TestCase{lhs: 719180185365001007, rhs: 0b1000001010011110001000011101101000001011000110111110101101000011, expected: 0b1000101101100101001010110100101101111101001101011101100001101100}, + TestCase{lhs: 4768539711644468546, rhs: 0b1111100000100101101110111000001110100010010011001111100100001010, expected: 0b1011101000001000111110100001100111111011101001010111110001001000}, + TestCase{lhs: 7527158369822354417, rhs: 0b1011000100000000100110111100101101011010000110101111001001101111, expected: 0b1101100101110101010011010110001101111011010001000101100110011110}, + TestCase{lhs: 4588599539643079886, rhs: 0b1100111001100000111110011101000100110000000011111110000111000110, expected: 0b1111000111001101000000110010100101111000111111011011110100001000}, + TestCase{lhs: 3301384231003757141, rhs: 0b1010001100101000000101101110001001000100010000100100111011100110, expected: 0b1000111011111000110010011001010100110000010011000101010010110011}, + TestCase{lhs: 2350264863387015206, rhs: 0b1101101011111110010000100001110110110110110000010101111110000100, expected: 0b1111101001100011100100110110110101010000100000101000011110100010}, + TestCase{lhs: 8049059441214428264, rhs: 0b1001110010100100011110101010000111100011011010101101110011111000, expected: 0b1111001100010000011110100100101101100010111111011100100010010000}, + TestCase{lhs: 1438453154128719080, rhs: 0b1001011000101111100110111000111101000101110101001001010011110110, expected: 0b1000010111011001111100100000101011111000001101010111100000011110}, + TestCase{lhs: 4877978639484843708, rhs: 0b1000101110100111110011011101001011111000011111000001011001011000, expected: 0b1100100000010101110000100110100010100100000100111110000011100100}, + TestCase{lhs: 6905957971486304477, rhs: 0b1011100100001011001100000101111010111010001010011110001111001000, expected: 0b1110011011011101110101000111000100110110000111100000101100010101}, + TestCase{lhs: 0b1001001110111101010111110010000011101101010011011010111001101100, rhs: 0b1001100011010011101000110110110001001101101011001111000010010100, expected: 823873187902217976}, + TestCase{lhs: 0b1110010100011001101000101010001110000100000011011010001011010111, rhs: 0b1100100100110100010110000110111010000000001110000001001100100011, expected: 3183476270066676212}, + TestCase{lhs: 0b1101010010001011110100100101010110111011111010111100001010101010, rhs: 0b1101011100001101001100001100000110001010110101100000111110010100, expected: 254139555102182718}, + TestCase{lhs: 0b1000011011110111111011000001101101011111110110010000111101001110, rhs: 0b1010000110011100110011111011010110000001000001100001010111001011, expected: 2840403223957674629}, + TestCase{lhs: 0b1000110001010001100101111111110111010111011000011001110011010100, rhs: 0b1000001111101010110101111101101100110011010111110000011110010000, expected: 1133570266996054852}, + TestCase{lhs: 0b1100100100100000101100010100010010011000011000111111110101100101, rhs: 0b1001010010111000101000001001001000001110011011001111111010001111, expected: 6744160055325557738}, + TestCase{lhs: 0b1000110001101110001101111001010100101101110011000110000010011001, rhs: 0b1000000011110100110111100011001000110000000011000100011011100000, expected: 908295178836518521}, + TestCase{lhs: 0b1110111100011001111101010000010101001101101110001101101111010101, rhs: 0b1100000101001001001100100110111110001110010111011111001100101101, expected: 3337386585248573688}, + TestCase{lhs: 0b1110110000010111100101001101110010100110010001100111100000001101, rhs: 0b1000100101000100000101011111011010100110100000110000101000010101, expected: 7301321438299255320}, + TestCase{lhs: 0b1000010111000011110100010001110000101011111111001011010001011001, rhs: 0b1001110111010101111000110011011011011001110110001010001101110110, expected: 1735629866430371631}, + ]; + + #[test] + fn test_i64() { + for case in test_cases.span() { + assert_eq!(__llvm__xor_i64_i64(*case.lhs, *case.rhs), *case.expected); + } + } +} diff --git a/compiler_rt/src/alu/xor/xor_i8.cairo b/compiler_rt/src/alu/xor/xor_i8.cairo new file mode 100644 index 0000000..09a2469 --- /dev/null +++ b/compiler_rt/src/alu/xor/xor_i8.cairo @@ -0,0 +1,77 @@ +use core::integer::Bitwise; +extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic; + +pub fn __llvm__xor_i8_i8(lhs: u128, rhs: u128) -> u128 { + let _: u8 = match lhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("lhs = {:?} does not fit in u8", lhs) + }, + }; + + let _: u8 = match rhs.try_into() { + Option::Some(value) => value, + Option::None => { + panic!("rhs = {:?} does not fit in u8", rhs) + }, + }; + + let (_, xor_result, _) = bitwise(lhs, rhs); + xor_result +} + +#[cfg(test)] +mod tests { + use super::__llvm__xor_i8_i8; + use crate::alu::test_case::TestCase; + + pub const test_cases: [TestCase; 40] = [ + TestCase{lhs: 120, rhs: 72, expected: 48}, + TestCase{lhs: 32, rhs: 49, expected: 17}, + TestCase{lhs: 44, rhs: 46, expected: 2}, + TestCase{lhs: 84, rhs: 42, expected: 126}, + TestCase{lhs: 113, rhs: 9, expected: 120}, + TestCase{lhs: 116, rhs: 95, expected: 43}, + TestCase{lhs: 55, rhs: 94, expected: 105}, + TestCase{lhs: 82, rhs: 33, expected: 115}, + TestCase{lhs: 13, rhs: 85, expected: 88}, + TestCase{lhs: 70, rhs: 15, expected: 73}, + TestCase{lhs: 0b11111111, rhs: 42, expected: 0b11010101}, + TestCase{lhs: 0b10100111, rhs: 118, expected: 0b11010001}, + TestCase{lhs: 0b10000110, rhs: 67, expected: 0b11000101}, + TestCase{lhs: 0b10011110, rhs: 95, expected: 0b11000001}, + TestCase{lhs: 0b11110101, rhs: 35, expected: 0b11010110}, + TestCase{lhs: 0b10111010, rhs: 114, expected: 0b11001000}, + TestCase{lhs: 0b11101010, rhs: 102, expected: 0b10001100}, + TestCase{lhs: 0b10000111, rhs: 74, expected: 0b11001101}, + TestCase{lhs: 0b11100010, rhs: 97, expected: 0b10000011}, + TestCase{lhs: 0b11111111, rhs: 58, expected: 0b11000101}, + TestCase{lhs: 86, rhs: 0b11110111, expected: 0b10100001}, + TestCase{lhs: 64, rhs: 0b10101110, expected: 0b11101110}, + TestCase{lhs: 11, rhs: 0b10110011, expected: 0b10111000}, + TestCase{lhs: 79, rhs: 0b10000000, expected: 0b11001111}, + TestCase{lhs: 90, rhs: 0b11101110, expected: 0b10110100}, + TestCase{lhs: 5, rhs: 0b11101101, expected: 0b11101000}, + TestCase{lhs: 19, rhs: 0b11101101, expected: 0b11111110}, + TestCase{lhs: 50, rhs: 0b10010111, expected: 0b10100101}, + TestCase{lhs: 29, rhs: 0b11001000, expected: 0b11010101}, + TestCase{lhs: 59, rhs: 0b11100111, expected: 0b11011100}, + TestCase{lhs: 0b11001111, rhs: 0b10001101, expected: 66}, + TestCase{lhs: 0b10101000, rhs: 0b10111111, expected: 23}, + TestCase{lhs: 0b11111101, rhs: 0b10110101, expected: 72}, + TestCase{lhs: 0b11110110, rhs: 0b11111101, expected: 11}, + TestCase{lhs: 0b11110010, rhs: 0b11101100, expected: 30}, + TestCase{lhs: 0b11000001, rhs: 0b10010101, expected: 84}, + TestCase{lhs: 0b10000000, rhs: 0b10011100, expected: 28}, + TestCase{lhs: 0b11001000, rhs: 0b11101110, expected: 38}, + TestCase{lhs: 0b10111101, rhs: 0b10001000, expected: 53}, + TestCase{lhs: 0b11011011, rhs: 0b10100110, expected: 125}, + ]; + + #[test] + fn test_i8() { + for case in test_cases.span() { + assert_eq!(__llvm__xor_i8_i8(*case.lhs, *case.rhs), *case.expected); + } + } +}