Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.8.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC)
Options
Source code
#![feature(int_log)] #![feature(adt_const_params)] #![feature(generic_const_exprs)] pub trait GetBit { fn bit(self, i: usize) -> bool; } impl GetBit for usize { #[inline(always)] fn bit(self, i: usize) -> bool { ((self >> i) & 1) == 1 } } #[derive(Copy, Clone, Debug)] pub struct BitArray64<const L: usize>([u64; L]); pub trait BitSlice { const N: usize; fn zeros() -> Self; fn ones() -> Self; fn splat(sign: bool) -> Self; fn xor(self, rhs: Self) -> Self; fn or(self, rhs: Self) -> Self; fn and(self, rhs: Self) -> Self; fn not(self) -> Self; fn count_bits(self) -> u32; } impl<const L: usize> BitSlice for BitArray64<L> { const N: usize = 64 * L; fn zeros() -> Self { BitArray64([0; L]) } fn ones() -> Self { BitArray64([!0; L]) } fn splat(sign: bool) -> Self { BitArray64([0u64 - sign as u64; L]) } fn xor(self, rhs: Self) -> Self { let mut target = BitArray64([0; L]); for w in 0..L { target.0[w] = self.0[w] ^ rhs.0[w]; } target } fn or(self, rhs: Self) -> Self { let mut target = BitArray64([0; L]); for w in 0..L { target.0[w] = self.0[w] | rhs.0[w]; } target } fn and(self, rhs: Self) -> Self { let mut target = BitArray64([0; L]); for w in 0..L { target.0[w] = self.0[w] & rhs.0[w]; } target } fn not(self) -> Self { let mut target = BitArray64([0; L]); for w in 0..L { target.0[w] = !self.0[w]; } target } fn count_bits(self) -> u32 { let mut target = 0u32; for w in 0..L { target += self.0[w].count_ones(); } target } } fn half_comparator<T: BitSlice + Copy>(a: T, b: T) -> (T, T, T) { let lt = a.not().and(b); let gt = a.and(b.not()); let eq = lt.or(gt).not(); (lt, eq, gt) } fn full_comparator<T: BitSlice + Copy>(a: T, b: T, c: (T, T, T)) -> (T, T, T) { let x = half_comparator(a, b); let lt = x.0.or(x.2.not().and(c.0)); let gt = x.2.or(x.0.not().and(c.2)); (lt, lt.or(gt).not(), gt) } fn half_adder<T: BitSlice + Copy>(a: T, b: T) -> (T, T) { (a.xor(b), a.and(b)) } fn full_adder<T: BitSlice + Copy>(a: T, b: T, c: T) -> (T, T) { let u = a.xor(b); (u.xor(c), a.and(b).or(u.and(c))) } fn comparator<T: BitSlice + Copy, const L: usize>(a: &[T; L], b: &[T; L]) -> (T, T, T) { let mut acc = half_comparator(a[0], b[0]); for i in 1..L { acc = full_comparator(a[i], b[i], acc); } acc } fn bit_add<T: BitSlice + Copy, const L: usize>(a: &[T; L], b: &[T; L]) -> [T; L + 1] { let mut acc = [T::zeros(); L + 1]; let (zero, c) = half_adder(a[0], b[0]); acc[0] = zero; let mut carry = c; for i in 1..L { let (bit, c) = full_adder(a[i], b[i], carry); acc[i] = bit; carry = c; } acc[L] = carry; acc } fn bit_add_wrapping<T: BitSlice + Copy, const L: usize>(a: &[T; L], b: &[T; L]) -> [T; L] { let mut acc = [T::zeros(); L]; let (zero, c) = half_adder(a[0], b[0]); acc[0] = zero; let mut carry = c; for i in 1..L { let (bit, c) = full_adder(a[i], b[i], carry); acc[i] = bit; carry = c; } acc } fn extend<T: BitSlice + Copy, const I: usize, const O: usize>(v: &[T; I]) -> [T; O] { let mut target = [T::zeros(); O]; for i in 0..I { target[i] = v[i]; } target } fn bit_splat<T: BitSlice + Copy, const L: usize>(value: u32) -> [T; L] { let mut target = [T::zeros(); L]; for i in 0..L { let sign = (value >> i) & 1 == 1; target[i] = T::splat(sign); } target } fn exp_count<T: BitSlice + Copy, const N: usize, const L: usize, const E: u32>( partial_sum: &[T; L], bits: &[T; E as usize], target_bit: &T, thresholds: &[[T; L]; N], counters: &mut [[u64; N]; 2usize.pow(E)], ) where T: std::fmt::Debug, [T; E.log2() as usize + 1]: , { for mask in 0..2usize.pow(E) { let mut exp_sum = [T::zeros(); E.log2() as usize + 1]; for b in 0..E { let expanded = extend(&[T::splat(mask.bit(b as usize)).and(bits[b as usize])]); exp_sum = bit_add_wrapping(&exp_sum, &expanded); } let exp_sum = extend(&exp_sum); let full_sum = bit_add_wrapping(&exp_sum, &partial_sum); for i in 0..N { let (_, _, gt) = comparator(&full_sum, &thresholds[i]); counters[mask][i] += gt.xor(*target_bit).not().count_bits() as u64; } } } fn unit_count<T: BitSlice + Copy, const I: usize, const N: usize, const P: usize>( partial_sum: &[T; P], inputs: &[T; I], target_bit: &T, thresholds: &[[T; P]; N], counters: &mut [[[u64; N]; 2]; I], ) { for i in 0..I { for t in 0..N { for s in 0..2 { let full_count = bit_add_wrapping(&partial_sum, &extend(&[inputs[i].xor(T::splat(s == 1))])); let (_, _, gt) = comparator(&full_count, &thresholds[t]); counters[i][s][t] += gt.xor(*target_bit).not().count_bits() as u64; } } } } type BitWordType = BitArray64<1>; const EXP_SIZE: u32 = 8; const ACC_SIZE: usize = 5; pub fn unit_count_demo( partial_sum: &[BitWordType; ACC_SIZE], inputs: &[BitWordType; 512], target_bit: &BitWordType, thresholds: &[[BitWordType; ACC_SIZE]; 2], counters: &mut [[[u64; 2]; 2]; 512], ) { unit_count::<BitWordType, 512, 2, ACC_SIZE>(partial_sum, inputs, target_bit, thresholds, counters) } pub fn exp_count_demo( partial_sum: &[BitWordType; ACC_SIZE], bits: &[BitWordType; EXP_SIZE as usize], target_bit: &BitWordType, thresholds: &[[BitWordType; ACC_SIZE]; 3], counters: &mut [[u64; 3]; 2usize.pow(EXP_SIZE)], ) { exp_count::<BitWordType, 3, ACC_SIZE, EXP_SIZE>(partial_sum, bits, target_bit, thresholds, counters) } fn main() {}
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree