Skip to content

Commit

Permalink
fix(old ssa): fix to_be_bits (#1765)
Browse files Browse the repository at this point in the history
* fix to_le_bits in original ssa

* add test
  • Loading branch information
Ethan-000 authored Jun 20, 2023
1 parent a1dfa67 commit 2541fbd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
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);
if endian == builtin::Endian::Big {
bits.reverse();
}

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

0 comments on commit 2541fbd

Please sign in to comment.