-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76aac7b
commit 3bc1ad4
Showing
13 changed files
with
194 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,118 @@ | ||
pub mod bit_reverse; | ||
|
||
use std::ops::Index; | ||
|
||
use bytemuck::checked::cast_slice_mut; | ||
use num_traits::Zero; | ||
|
||
use self::bit_reverse::bit_reverse_m31; | ||
use crate::core::fields::m31::BaseField; | ||
use crate::core::fields::{Column, FieldOps}; | ||
use crate::core::utils; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct AVX512Backend; | ||
|
||
// BaseField. | ||
type PackedBaseField = [BaseField; 16]; | ||
#[derive(Clone, Debug)] | ||
pub struct BaseFieldVec { | ||
data: Vec<PackedBaseField>, | ||
length: usize, | ||
} | ||
impl FieldOps<BaseField> for AVX512Backend { | ||
type Column = BaseFieldVec; | ||
|
||
fn bit_reverse_column(column: &mut Self::Column) { | ||
if column.data.len().ilog2() < bit_reverse::MIN_LOG_SIZE { | ||
let data: &mut [BaseField] = cast_slice_mut(&mut column.data[..]); | ||
utils::bit_reverse(&mut data[..column.length]); | ||
return; | ||
} | ||
bit_reverse_m31(&mut column.data); | ||
} | ||
} | ||
|
||
impl Column<BaseField> for BaseFieldVec { | ||
fn zeros(len: usize) -> Self { | ||
Self { | ||
data: vec![PackedBaseField::default(); (len + 15) / 16], | ||
length: len, | ||
} | ||
} | ||
fn to_vec(&self) -> Vec<BaseField> { | ||
self.data | ||
.iter() | ||
.flatten() | ||
.copied() | ||
.take(self.length) | ||
.collect() | ||
} | ||
fn len(&self) -> usize { | ||
self.length | ||
} | ||
} | ||
|
||
impl Index<usize> for BaseFieldVec { | ||
type Output = BaseField; | ||
fn index(&self, index: usize) -> &Self::Output { | ||
&self.data[index / 8][index % 8] | ||
} | ||
} | ||
|
||
impl FromIterator<BaseField> for BaseFieldVec { | ||
fn from_iter<I: IntoIterator<Item = BaseField>>(iter: I) -> Self { | ||
let mut chunks = iter.into_iter().array_chunks(); | ||
let mut res: Vec<_> = (&mut chunks).collect(); | ||
let mut length = res.len() * 16; | ||
|
||
if let Some(remainder) = chunks.into_remainder() { | ||
if !remainder.is_empty() { | ||
length += remainder.len(); | ||
let pad_len = 16 - remainder.len(); | ||
let last: PackedBaseField = remainder | ||
.chain(std::iter::repeat(BaseField::zero()).take(pad_len)) | ||
.collect::<Vec<_>>() | ||
.try_into() | ||
.unwrap(); | ||
res.push(last); | ||
} | ||
} | ||
|
||
Self { data: res, length } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::core::fields::{Col, Column}; | ||
|
||
type B = AVX512Backend; | ||
|
||
#[test] | ||
fn test_column() { | ||
for i in 0..100 { | ||
let col = Col::<B, BaseField>::from_iter((0..i).map(BaseField::from)); | ||
assert_eq!( | ||
col.to_vec(), | ||
(0..i).map(BaseField::from).collect::<Vec<_>>() | ||
); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_bit_reverse() { | ||
for i in 1..16 { | ||
let len = 1 << i; | ||
let mut col = Col::<B, BaseField>::from_iter((0..len).map(BaseField::from)); | ||
B::bit_reverse_column(&mut col); | ||
assert_eq!( | ||
col.to_vec(), | ||
(0..len) | ||
.map(|x| BaseField::from(utils::bit_reverse_index(x, i as u32))) | ||
.collect::<Vec<_>>() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.