Skip to content

Commit

Permalink
Update zerocopy; trim unused methods (#1393)
Browse files Browse the repository at this point in the history
* Remove mention of stdsimd
* Move fns FloatSIMDUtils::replace, extract to new cfg-gated trait FloatSIMDScalarUtils
* Remove unused utility methods on floats, bool
* Remove unused import
* Update to zerocopy 0.8.0-alpha.5
* Remove unneeded import of Float
  • Loading branch information
dhardy committed Feb 13, 2024
1 parent 58add64 commit e5a366d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ rand_core = { path = "rand_core", version = "0.7.0", default-features = false }
log = { version = "0.4.4", optional = true }
serde = { version = "1.0.103", features = ["derive"], optional = true }
rand_chacha = { path = "rand_chacha", version = "0.4.0", default-features = false, optional = true }
zerocopy = { version = "0.7.20", default-features = false, features = ["simd"] }
zerocopy = { version = "=0.8.0-alpha.5", default-features = false, features = ["simd"] }

[target.'cfg(unix)'.dependencies]
# Used for fork protection (reseeding.rs)
Expand Down
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ serde1 = ["serde"] # enables serde for BlockRng wrapper
[dependencies]
serde = { version = "1", features = ["derive"], optional = true }
getrandom = { version = "0.2", optional = true }
zerocopy = { version = "0.7.20", default-features = false }
zerocopy = { version = "=0.8.0-alpha.5", default-features = false }
4 changes: 2 additions & 2 deletions rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use crate::RngCore;
use core::cmp::min;
use zerocopy::AsBytes;
use zerocopy::{IntoBytes, NoCell};

/// Implement `next_u64` via `next_u32`, little-endian order.
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
}
}

trait Observable: AsBytes + Copy {
trait Observable: IntoBytes + NoCell + Copy {
fn to_le(self) -> Self;
}
impl Observable for u32 {
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/tests/pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![allow(clippy::float_cmp)]

use average::Histogram;
use rand::{Rng, SeedableRng};
use rand::Rng;
use rand_distr::{Normal, SkewNormal};

const HIST_LEN: usize = 100;
Expand Down
5 changes: 1 addition & 4 deletions src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ use crate::distributions::Distribution;
use crate::distributions::Standard;
use crate::{Rng, RngCore};

#[cfg(not(feature = "std"))]
#[allow(unused_imports)] // rustc doesn't detect that this is actually used
use crate::distributions::utils::Float;

#[cfg(feature = "simd_support")] use core::simd::prelude::*;
#[cfg(feature = "simd_support")] use core::simd::{LaneCount, SupportedLaneCount};

Expand Down Expand Up @@ -1250,6 +1246,7 @@ impl UniformSampler for UniformDuration {
mod tests {
use super::*;
use crate::rngs::mock::StepRng;
use crate::distributions::utils::FloatSIMDScalarUtils;

#[test]
#[cfg(feature = "serde1")]
Expand Down
59 changes: 17 additions & 42 deletions src/distributions/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,16 @@ pub(crate) trait FloatSIMDUtils {
// value, not by retaining the binary representation.
type UInt;
fn cast_from_int(i: Self::UInt) -> Self;
}

#[cfg(test)]
pub(crate) trait FloatSIMDScalarUtils: FloatSIMDUtils {
type Scalar;

fn replace(self, index: usize, new_value: Self::Scalar) -> Self;
fn extract(self, index: usize) -> Self::Scalar;
}

/// Implement functions available in std builds but missing from core primitives
#[cfg(not(std))]
// False positive: We are following `std` here.
#[allow(clippy::wrong_self_convention)]
pub(crate) trait Float: Sized {
fn is_nan(self) -> bool;
fn is_infinite(self) -> bool;
fn is_finite(self) -> bool;
}

/// Implement functions on f32/f64 to give them APIs similar to SIMD types
pub(crate) trait FloatAsSIMD: Sized {
const LEN: usize = 1;
Expand All @@ -265,50 +259,19 @@ impl IntAsSIMD for u64 {}

pub(crate) trait BoolAsSIMD: Sized {
fn any(self) -> bool;
fn all(self) -> bool;
fn none(self) -> bool;
}

impl BoolAsSIMD for bool {
#[inline(always)]
fn any(self) -> bool {
self
}

#[inline(always)]
fn all(self) -> bool {
self
}

#[inline(always)]
fn none(self) -> bool {
!self
}
}

macro_rules! scalar_float_impl {
($ty:ident, $uty:ident) => {
#[cfg(not(std))]
impl Float for $ty {
#[inline]
fn is_nan(self) -> bool {
self != self
}

#[inline]
fn is_infinite(self) -> bool {
self == ::core::$ty::INFINITY || self == ::core::$ty::NEG_INFINITY
}

#[inline]
fn is_finite(self) -> bool {
!(self.is_nan() || self.is_infinite())
}
}

impl FloatSIMDUtils for $ty {
type Mask = bool;
type Scalar = $ty;
type UInt = $uty;

#[inline(always)]
Expand Down Expand Up @@ -351,6 +314,11 @@ macro_rules! scalar_float_impl {
fn cast_from_int(i: Self::UInt) -> Self {
i as $ty
}
}

#[cfg(test)]
impl FloatSIMDScalarUtils for $ty {
type Scalar = $ty;

#[inline]
fn replace(self, index: usize, new_value: Self::Scalar) -> Self {
Expand Down Expand Up @@ -380,7 +348,6 @@ macro_rules! simd_impl {
where LaneCount<LANES>: SupportedLaneCount
{
type Mask = Mask<<$fty as SimdElement>::Mask, LANES>;
type Scalar = $fty;
type UInt = Simd<$uty, LANES>;

#[inline(always)]
Expand Down Expand Up @@ -429,6 +396,14 @@ macro_rules! simd_impl {
fn cast_from_int(i: Self::UInt) -> Self {
i.cast()
}
}

#[cfg(test)]
impl<const LANES: usize> FloatSIMDScalarUtils for Simd<$fty, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
type Scalar = $fty;

#[inline]
fn replace(mut self, index: usize, new_value: Self::Scalar) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#![deny(missing_debug_implementations)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
#![no_std]
#![cfg_attr(feature = "simd_support", feature(stdsimd, portable_simd))]
#![cfg_attr(feature = "simd_support", feature(portable_simd))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![allow(
clippy::float_cmp,
Expand Down

0 comments on commit e5a366d

Please sign in to comment.