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 15, 2024
1 parent 0f10355 commit eac1dc7
Show file tree
Hide file tree
Showing 9 changed files with 456 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;
8 changes: 8 additions & 0 deletions compiler_rt/src/alu/xor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod xor_bool;
pub mod xor_i8;
pub mod xor_i16;
pub mod xor_i32;
pub mod xor_i64;
pub mod xor_i128;

mod xor_test_cases;
36 changes: 36 additions & 0 deletions compiler_rt/src/alu/xor/xor_bool.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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;

#[test]
fn test_i1() {
for case in crate::alu::xor::xor_test_cases::xor_i1.span() {
assert_eq!(__llvm__xor_bool_bool(*case.lhs, *case.rhs), *case.expected);
}
}

// Values larger than 1 bit should not work
#[test]
#[should_panic]
fn test_i8() {
for case in crate::alu::xor::xor_test_cases::xor_i8.span() {
assert_eq!(__llvm__xor_bool_bool(*case.lhs, *case.rhs), *case.expected);
}
}
}
19 changes: 19 additions & 0 deletions compiler_rt/src/alu/xor/xor_i128.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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;

#[test]
fn test_i128() {
for case in crate::alu::xor::xor_test_cases::xor_i128.span() {
assert_eq!(__llvm__xor_i128_i128(*case.lhs, *case.rhs), *case.expected);
}
}
}
42 changes: 42 additions & 0 deletions compiler_rt/src/alu/xor/xor_i16.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

#[test]
fn test_i16() {
for case in crate::alu::xor::xor_test_cases::xor_i16.span() {
assert_eq!(__llvm__xor_i16_i16(*case.lhs, *case.rhs), *case.expected);
}
}

// Values larger than 16 bit should not work
#[test]
#[should_panic]
fn test_i32() {
for case in crate::alu::xor::xor_test_cases::xor_i32.span() {
__llvm__xor_i16_i16(*case.lhs, *case.rhs);
}
}
}
42 changes: 42 additions & 0 deletions compiler_rt/src/alu/xor/xor_i32.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

#[test]
fn test_i32() {
for case in crate::alu::xor::xor_test_cases::xor_i32.span() {
assert_eq!(__llvm__xor_i32_i32(*case.lhs, *case.rhs), *case.expected);
}
}

// Values larger than 32 bit should not work
#[test]
#[should_panic]
fn test_i64() {
for case in crate::alu::xor::xor_test_cases::xor_i64.span() {
__llvm__xor_i32_i32(*case.lhs, *case.rhs);
}
}
}
42 changes: 42 additions & 0 deletions compiler_rt/src/alu/xor/xor_i64.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

#[test]
fn test_i64() {
for case in crate::alu::xor::xor_test_cases::xor_i64.span() {
assert_eq!(__llvm__xor_i64_i64(*case.lhs, *case.rhs), *case.expected);
}
}

// Values larger than 64 bit should not work
#[test]
#[should_panic]
fn test_i128() {
for case in crate::alu::xor::xor_test_cases::xor_i128.span() {
__llvm__xor_i64_i64(*case.lhs, *case.rhs);
}
}
}
42 changes: 42 additions & 0 deletions compiler_rt/src/alu/xor/xor_i8.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

#[test]
fn test_i8() {
for case in crate::alu::xor::xor_test_cases::xor_i8.span() {
assert_eq!(__llvm__xor_i8_i8(*case.lhs, *case.rhs), *case.expected);
}
}

// Values larger than 8 bit should not work
#[test]
#[should_panic]
fn test_i16() {
for case in crate::alu::xor::xor_test_cases::xor_i16.span() {
__llvm__xor_i8_i8(*case.lhs, *case.rhs);
}
}
}
Loading

0 comments on commit eac1dc7

Please sign in to comment.