Skip to content

Commit

Permalink
change implementation for i8 i16 i32 i64 too
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoGiachetta committed Oct 8, 2024
1 parent 48189f8 commit cee6da7
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/libfuncs/sint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ mod test {
2_i128.into()
}
);
dbg!(Felt::from(-396372399979_i128));
run_program_assert_output(&program, "run_test", &[], Felt::from(2_i128).into());
}

Expand Down
37 changes: 34 additions & 3 deletions src/libfuncs/sint16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::{
ods::math,
arith::{self, CmpiPredicate},
cf, llvm,
},
Expand Down Expand Up @@ -274,11 +275,29 @@ pub fn build_to_felt252<'ctx, 'this>(
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;
let prime =
entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?;

let value: Value = entry.argument(0)?.into();
let value_type = value.r#type();

let is_negative = entry.append_op_result(arith::cmpi(
context,
arith::CmpiPredicate::Slt,
value,
entry.const_int_from_type(context, location, 0, value_type)?,
location,
))?;

let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?;
let value_abs = entry.append_op_result(math::absi(context, value, location).into())?;

entry.append_operation(helper.br(0, &[result], location));
let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?;

let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?;

let final_result = entry.append_op_result(arith::select(is_negative, prime_minus_result, result, location))?;

entry.append_operation(helper.br(0, &[final_result], location));

Ok(())
}
Expand Down Expand Up @@ -504,7 +523,7 @@ mod test {
}

#[test]
fn i16_to_felt252() {
fn pos_i16_to_felt252() {
let program = load_cairo!(
use traits::Into;

Expand All @@ -516,6 +535,18 @@ mod test {
run_program_assert_output(&program, "run_test", &[], Felt::from(2).into());
}

#[test]
fn neg_i16_to_felt252() {
let program = load_cairo!(
use traits::Into;

fn run_test() -> felt252 {
-3963_i16.into()
}
);
run_program_assert_output(&program, "run_test", &[], Felt::from(-3963).into());
}

#[test]
fn i16_from_felt252() {
let program = load_cairo!(
Expand Down
37 changes: 34 additions & 3 deletions src/libfuncs/sint32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::{
ods::math,
arith::{self, CmpiPredicate},
cf, llvm,
},
Expand Down Expand Up @@ -273,11 +274,29 @@ pub fn build_to_felt252<'ctx, 'this>(
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;
let prime =
entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?;

let value: Value = entry.argument(0)?.into();
let value_type = value.r#type();

let is_negative = entry.append_op_result(arith::cmpi(
context,
arith::CmpiPredicate::Slt,
value,
entry.const_int_from_type(context, location, 0, value_type)?,
location,
))?;

let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?;
let value_abs = entry.append_op_result(math::absi(context, value, location).into())?;

entry.append_operation(helper.br(0, &[result], location));
let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?;

let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?;

let final_result = entry.append_op_result(arith::select(is_negative, prime_minus_result, result, location))?;

entry.append_operation(helper.br(0, &[final_result], location));

Ok(())
}
Expand Down Expand Up @@ -504,7 +523,7 @@ mod test {
}

#[test]
fn i32_to_felt252() {
fn pos_i32_to_felt252() {
let program = load_cairo!(
use traits::Into;

Expand All @@ -516,6 +535,18 @@ mod test {
run_program_assert_output(&program, "run_test", &[], Felt::from(2).into());
}

#[test]
fn neg_i32_to_felt252() {
let program = load_cairo!(
use traits::Into;

fn run_test() -> felt252 {
-396372_i32.into()
}
);
run_program_assert_output(&program, "run_test", &[], Felt::from(-396372).into());
}

#[test]
fn i32_from_felt252() {
let program = load_cairo!(
Expand Down
37 changes: 34 additions & 3 deletions src/libfuncs/sint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::{
ods::math,
arith::{self, CmpiPredicate},
cf, llvm,
},
Expand Down Expand Up @@ -274,11 +275,29 @@ pub fn build_to_felt252<'ctx, 'this>(
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;
let prime =
entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?;

let value: Value = entry.argument(0)?.into();
let value_type = value.r#type();

let is_negative = entry.append_op_result(arith::cmpi(
context,
arith::CmpiPredicate::Slt,
value,
entry.const_int_from_type(context, location, 0, value_type)?,
location,
))?;

let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?;
let value_abs = entry.append_op_result(math::absi(context, value, location).into())?;

entry.append_operation(helper.br(0, &[result], location));
let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?;

let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?;

let final_result = entry.append_op_result(arith::select(is_negative, prime_minus_result, result, location))?;

entry.append_operation(helper.br(0, &[final_result], location));

Ok(())
}
Expand Down Expand Up @@ -504,7 +523,7 @@ mod test {
}

#[test]
fn i64_to_felt252() {
fn pos_i64_to_felt252() {
let program = load_cairo!(
use traits::Into;

Expand All @@ -516,6 +535,18 @@ mod test {
run_program_assert_output(&program, "run_test", &[], Felt::from(2).into());
}

#[test]
fn neg_i64_to_felt252() {
let program = load_cairo!(
use traits::Into;

fn run_test() -> felt252 {
-396372_i64.into()
}
);
run_program_assert_output(&program, "run_test", &[], Felt::from(-396372).into());
}

#[test]
fn i64_from_felt252() {
let program = load_cairo!(
Expand Down
37 changes: 34 additions & 3 deletions src/libfuncs/sint8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::{
ods::math,
arith::{self, CmpiPredicate},
cf, llvm,
},
Expand Down Expand Up @@ -275,11 +276,29 @@ pub fn build_to_felt252<'ctx, 'this>(
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;
let prime =
entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?;

let value: Value = entry.argument(0)?.into();
let value_type = value.r#type();

let is_negative = entry.append_op_result(arith::cmpi(
context,
arith::CmpiPredicate::Slt,
value,
entry.const_int_from_type(context, location, 0, value_type)?,
location,
))?;

let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?;
let value_abs = entry.append_op_result(math::absi(context, value, location).into())?;

entry.append_operation(helper.br(0, &[result], location));
let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?;

let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?;

let final_result = entry.append_op_result(arith::select(is_negative, prime_minus_result, result, location))?;

entry.append_operation(helper.br(0, &[final_result], location));

Ok(())
}
Expand Down Expand Up @@ -506,7 +525,7 @@ mod test {
}

#[test]
fn i8_to_felt252() {
fn pos_i8_to_felt252() {
let program = load_cairo!(
use traits::Into;

Expand All @@ -518,6 +537,18 @@ mod test {
run_program_assert_output(&program, "run_test", &[], Felt::from(2).into());
}

#[test]
fn neg_i8_to_felt252() {
let program = load_cairo!(
use traits::Into;

fn run_test() -> felt252 {
-255_i128.into()
}
);
run_program_assert_output(&program, "run_test", &[], Felt::from(-255).into());
}

#[test]
fn i8_from_felt252() {
let program = load_cairo!(
Expand Down

0 comments on commit cee6da7

Please sign in to comment.