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

Change memory to little endian format. #161

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 6 additions & 6 deletions alu_u32/src/add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ impl Add32Chip {

let mut carry_1 = 0;
let mut carry_2 = 0;
if b[3] as u32 + c[3] as u32 > 255 {
if b[0] as u32 + c[0] as u32 > 255 {
carry_1 = 1;
cols.carry[0] = F::one();
cols.carry[3] = F::one();
}
if b[2] as u32 + c[2] as u32 + carry_1 > 255 {
if b[1] as u32 + c[1] as u32 + carry_1 > 255 {
carry_2 = 1;
cols.carry[1] = F::one();
}
if b[1] as u32 + c[1] as u32 + carry_2 > 255 {
cols.carry[2] = F::one();
}
if b[2] as u32 + c[2] as u32 + carry_2 > 255 {
cols.carry[1] = F::one();
}
cols.is_real = F::one();
}
}
Expand Down
15 changes: 8 additions & 7 deletions machine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::cmp::Ordering;
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Index, IndexMut, Mul, Shl, Shr, Sub};
use p3_field::{Field, PrimeField};

// Currently stored in big-endian form.
// Stored in little-endian form, to avoid conversion between the compiler and the VM, and to be consistent with our file format.
#[derive(Copy, Clone, Debug, Default)]
pub struct Word<F>(pub [F; MEMORY_CELL_BYTES]);

Expand All @@ -18,6 +18,7 @@ pub fn addr_of_word(addr: u32) -> u32 {
(addr & !3) as u32
}

/// Check that an address is a multiple of 4.
pub fn is_mul_4(addr: u32) -> bool {
addr.rem_euclid(4) == 0
}
Expand All @@ -26,7 +27,7 @@ pub fn is_mul_4(addr: u32) -> bool {
impl Word<u8> {
pub fn from_u8(byte: u8) -> Self {
let mut result = [0; MEMORY_CELL_BYTES];
result[MEMORY_CELL_BYTES - 1] = byte;
result[0] = byte;
Self(result)
}
}
Expand All @@ -35,7 +36,7 @@ impl Word<u8> {
pub fn sign_extend_byte(byte: u8) -> Self {
let sign = byte as i8 >> 7;
let mut result: [u8; MEMORY_CELL_BYTES] = [sign as u8; MEMORY_CELL_BYTES];
result[3] = byte;
result[0] = byte;
Self(result)
}
}
Expand All @@ -56,7 +57,7 @@ impl<F: Copy> Word<F> {
{
let mut result: [T; MEMORY_CELL_BYTES] = [T::default(); MEMORY_CELL_BYTES];
for (i, item) in self.0.iter().enumerate() {
result[i] = f(*item);
result[3 - i] = f(*item);
}
Word(result)
}
Expand All @@ -76,7 +77,7 @@ impl Into<u32> for Word<u8> {
fn into(self) -> u32 {
let mut result = 0u32;
for i in 0..MEMORY_CELL_BYTES {
result += (self[MEMORY_CELL_BYTES - i - 1] as u32) << (i * 8);
result += (self[i] as u32) << (i * 8);
}
result
}
Expand All @@ -86,7 +87,7 @@ impl From<u32> for Word<u8> {
fn from(value: u32) -> Self {
let mut result = Word::<u8>::default();
for i in 0..MEMORY_CELL_BYTES {
result[MEMORY_CELL_BYTES - i - 1] = ((value >> (i * 8)) & 0xFF) as u8;
result[i] = ((value >> (i * 8)) & 0xFF) as u8;
}
result
}
Expand Down Expand Up @@ -272,7 +273,7 @@ impl BitOr for Word<u8> {

impl<F: Field> From<F> for Word<F> {
fn from(bytes: F) -> Self {
Self([F::zero(), F::zero(), F::zero(), bytes])
Self([bytes, F::zero(), F::zero(), F::zero()])
}
}

Expand Down
Loading