Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(old ssa): fix to_be_bits #1765

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/test_data/to_bits/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.7.0"

[dependencies]
24 changes: 24 additions & 0 deletions crates/nargo_cli/tests/test_data/to_bits/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use dep::std;

fn main() {
let field = 1000;
let be_bits = field.to_be_bits(16);
let le_bits = field.to_le_bits(16);

for i in 0..16 {
let x = be_bits[i];
let y = le_bits[15-i];
assert(x == y);
}

let x = 3;
let be_bits_x = x.to_be_bits(4);
let le_bits_x = x.to_le_bits(4);

for i in 0..4 {
let be_bit = be_bits_x[i];
let le_bit = le_bits_x[3-i];
assert(be_bit == le_bit);
}

}
6 changes: 5 additions & 1 deletion crates/noirc_evaluator/src/ssa/optimizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ fn evaluate_intrinsic(
block_id: BlockId,
) -> Result<Vec<NodeId>, RuntimeErrorKind> {
match op {
builtin::Opcode::ToBits(_) => {
builtin::Opcode::ToBits(endian) => {
let bit_count = args[1].to_u128() as u32;
let mut result = Vec::new();
let mut bits = args[0].bits();
bits.reverse();
bits.resize(bit_count as usize, false);
jfecher marked this conversation as resolved.
Show resolved Hide resolved
if endian == builtin::Endian::Big {
bits.reverse();
}

if let ObjectType::ArrayPointer(a) = res_type {
for i in 0..bit_count {
Expand Down