Skip to content

Commit

Permalink
Add xor
Browse files Browse the repository at this point in the history
Signed-off-by: Wojciech Zmuda <[email protected]>
  • Loading branch information
wzmuda committed Nov 17, 2024
1 parent 6cbd0df commit 3b32f5f
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler_rt/src/alu.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod and;
pub mod or;
pub mod xor;

mod test_case;
6 changes: 6 additions & 0 deletions compiler_rt/src/alu/xor.cairo
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 35 additions & 0 deletions compiler_rt/src/alu/xor/xor_bool.cairo
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
64 changes: 64 additions & 0 deletions compiler_rt/src/alu/xor/xor_i128.cairo
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
77 changes: 77 additions & 0 deletions compiler_rt/src/alu/xor/xor_i16.cairo
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading

0 comments on commit 3b32f5f

Please sign in to comment.