Skip to content

Commit

Permalink
Merge potential soundess protection from 1.8 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
starkat99 committed Feb 10, 2024
1 parent cabfc74 commit a513d3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 40 deletions.
24 changes: 4 additions & 20 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,7 @@ impl HalfFloatSliceExt for [f16] {
#[inline]
#[allow(clippy::uninit_vec)]
fn to_f32_vec(&self) -> Vec<f32> {
let mut vec = Vec::with_capacity(self.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(self.len()) };
let mut vec = vec![0f32; self.len()];
self.convert_to_f32_slice(&mut vec);
vec
}
Expand All @@ -377,11 +373,7 @@ impl HalfFloatSliceExt for [f16] {
#[inline]
#[allow(clippy::uninit_vec)]
fn to_f64_vec(&self) -> Vec<f64> {
let mut vec = Vec::with_capacity(self.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(self.len()) };
let mut vec = vec![0f64; self.len()];
self.convert_to_f64_slice(&mut vec);
vec
}
Expand Down Expand Up @@ -466,11 +458,7 @@ impl HalfFloatSliceExt for [bf16] {
#[inline]
#[allow(clippy::uninit_vec)]
fn to_f32_vec(&self) -> Vec<f32> {
let mut vec = Vec::with_capacity(self.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(self.len()) };
let mut vec = vec![0f32; self.len()];
self.convert_to_f32_slice(&mut vec);
vec
}
Expand All @@ -479,11 +467,7 @@ impl HalfFloatSliceExt for [bf16] {
#[inline]
#[allow(clippy::uninit_vec)]
fn to_f64_vec(&self) -> Vec<f64> {
let mut vec = Vec::with_capacity(self.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(self.len()) };
let mut vec = vec![0f64; self.len()];
self.convert_to_f64_slice(&mut vec);
vec
}
Expand Down
24 changes: 4 additions & 20 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,14 @@ impl HalfFloatVecExt for Vec<f16> {

#[allow(clippy::uninit_vec)]
fn from_f32_slice(slice: &[f32]) -> Self {
let mut vec = Vec::with_capacity(slice.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(slice.len()) };
let mut vec = vec![f16::from_bits(0); slice.len()];
vec.convert_from_f32_slice(slice);
vec
}

#[allow(clippy::uninit_vec)]
fn from_f64_slice(slice: &[f64]) -> Self {
let mut vec = Vec::with_capacity(slice.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(slice.len()) };
let mut vec = vec![f16::from_bits(0); slice.len()];
vec.convert_from_f64_slice(slice);
vec
}
Expand Down Expand Up @@ -178,22 +170,14 @@ impl HalfFloatVecExt for Vec<bf16> {

#[allow(clippy::uninit_vec)]
fn from_f32_slice(slice: &[f32]) -> Self {
let mut vec = Vec::with_capacity(slice.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(slice.len()) };
let mut vec = vec![bf16::from_bits(0); slice.len()];
vec.convert_from_f32_slice(slice);
vec
}

#[allow(clippy::uninit_vec)]
fn from_f64_slice(slice: &[f64]) -> Self {
let mut vec = Vec::with_capacity(slice.len());
// SAFETY: convert will initialize every value in the vector without reading them,
// so this is safe to do instead of double initialize from resize, and we're setting it to
// same value as capacity.
unsafe { vec.set_len(slice.len()) };
let mut vec = vec![bf16::from_bits(0); slice.len()];
vec.convert_from_f64_slice(slice);
vec
}
Expand Down

0 comments on commit a513d3b

Please sign in to comment.