Skip to content

Commit

Permalink
feat: implement to_be_radix in the comptime interpreter (#6043)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #6042
## Summary\*

This PR implements `to_be_radix` in the comptime interpreter by reusing
our impl for `to_le_radix`.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Sep 16, 2024
1 parent b3accfc commit 1550278
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"struct_def_module" => struct_def_module(self, arguments, location),
"struct_def_name" => struct_def_name(interner, arguments, location),
"struct_def_set_fields" => struct_def_set_fields(interner, arguments, location),
"to_be_radix" => to_be_radix(arguments, return_type, location),
"to_le_radix" => to_le_radix(arguments, return_type, location),
"trait_constraint_eq" => trait_constraint_eq(arguments, location),
"trait_constraint_hash" => trait_constraint_hash(arguments, location),
Expand Down Expand Up @@ -756,6 +757,21 @@ fn quoted_tokens(arguments: Vec<(Value, Location)>, location: Location) -> IResu
))
}

fn to_be_radix(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
let le_radix_limbs = to_le_radix(arguments, return_type, location)?;

let Value::Array(limbs, typ) = le_radix_limbs else {
unreachable!("`to_le_radix` should always return an array");
};
let be_radix_limbs = limbs.into_iter().rev().collect();

Ok(Value::Array(be_radix_limbs, typ))
}

fn to_le_radix(
arguments: Vec<(Value, Location)>,
return_type: Type,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "comptime_to_radix"
type = "bin"
authors = [""]

[dependencies]
10 changes: 10 additions & 0 deletions test_programs/compile_success_empty/comptime_to_radix/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
comptime
{
let le_bytes: [u8; 3] = 257.to_le_bytes();
assert_eq(le_bytes, [1, 1, 0]);

let be_bytes: [u8; 3] = 257.to_be_bytes();
assert_eq(be_bytes, [0, 1, 1]);
}
}

0 comments on commit 1550278

Please sign in to comment.