// TODO: Can be further optimized with array_chunks when it becomes stabilized
let src_chunks = src.chunks_exact(4); letmut dst_chunks = dst.chunks_exact_mut(4); let src_remainder = src_chunks.remainder(); for (s, d) in src_chunks.zip(&mut dst_chunks) { let chunk: &[S; 4] = s.try_into().unwrap();
d.copy_from_slice(unsafe { &f(chunk) });
}
// Process remainder if !src_remainder.is_empty() { letmut buf: [S; 4] = Default::default();
buf[..src_remainder.len()].copy_from_slice(src_remainder); let vec = unsafe { f(&buf) }; let dst_remainder = dst_chunks.into_remainder();
dst_remainder.copy_from_slice(&vec[..dst_remainder.len()]);
}
}
/////////////// Fallbacks ////////////////
// In the below functions, round to nearest, with ties to even. // Let us call the most significant bit that will be shifted out the round_bit. // // Round up if either // a) Removed part > tie. // (mantissa & round_bit) != 0 && (mantissa & (round_bit - 1)) != 0 // b) Removed part == tie, and retained part is odd. // (mantissa & round_bit) != 0 && (mantissa & (2 * round_bit)) != 0 // (If removed part == tie and retained part is even, do not round up.) // These two conditions can be combined into one: // (mantissa & round_bit) != 0 && (mantissa & ((round_bit - 1) | (2 * round_bit))) != 0 // which can be simplified into // (mantissa & round_bit) != 0 && (mantissa & (3 * round_bit - 1)) != 0
#[inline] pub(crate) constfn f32_to_f16_fallback(value: f32) -> u16 { // TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized // Convert to raw bytes let x: u32 = unsafe { mem::transmute::<f32, u32>(value) };
// Extract IEEE754 components let sign = x & 0x8000_0000u32; let exp = x & 0x7F80_0000u32; let man = x & 0x007F_FFFFu32;
// Check for all exponent bits being set, which is Infinity or NaN if exp == 0x7F80_0000u32 { // Set mantissa MSB for NaN (and also keep shifted mantissa bits) let nan_bit = if man == 0 { 0 } else { 0x0200u32 }; return ((sign >> 16) | 0x7C00u32 | nan_bit | (man >> 13)) as u16;
}
// The number is normalized, start assembling half precision version let half_sign = sign >> 16; // Unbias the exponent, then bias for half precision let unbiased_exp = ((exp >> 23) as i32) - 127; let half_exp = unbiased_exp + 15;
// Check for exponent overflow, return +infinity if half_exp >= 0x1F { return (half_sign | 0x7C00u32) as u16;
}
// Check for underflow if half_exp <= 0 { // Check mantissa for what we can do if14 - half_exp > 24 { // No rounding possibility, so this is a full underflow, return signed zero return half_sign as u16;
} // Don't forget about hidden leading mantissa bit when assembling mantissa let man = man | 0x0080_0000u32; letmut half_man = man >> (14 - half_exp); // Check for rounding (see comment above functions) let round_bit = 1 << (13 - half_exp); if (man & round_bit) != 0 && (man & (3 * round_bit - 1)) != 0 {
half_man += 1;
} // No exponent for subnormals return (half_sign | half_man) as u16;
}
// Rebias the exponent let half_exp = (half_exp as u32) << 10; let half_man = man >> 13; // Check for rounding (see comment above functions) let round_bit = 0x0000_1000u32; if (man & round_bit) != 0 && (man & (3 * round_bit - 1)) != 0 { // Round it
((half_sign | half_exp | half_man) + 1) as u16
} else {
(half_sign | half_exp | half_man) as u16
}
}
#[inline] pub(crate) constfn f64_to_f16_fallback(value: f64) -> u16 { // Convert to raw bytes, truncating the last 32-bits of mantissa; that precision will always // be lost on half-precision. // TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized let val: u64 = unsafe { mem::transmute::<f64, u64>(value) }; let x = (val >> 32) as u32;
// Extract IEEE754 components let sign = x & 0x8000_0000u32; let exp = x & 0x7FF0_0000u32; let man = x & 0x000F_FFFFu32;
// Check for all exponent bits being set, which is Infinity or NaN if exp == 0x7FF0_0000u32 { // Set mantissa MSB for NaN (and also keep shifted mantissa bits). // We also have to check the last 32 bits. let nan_bit = if man == 0 && (val as u32 == 0) { 0
} else { 0x0200u32
}; return ((sign >> 16) | 0x7C00u32 | nan_bit | (man >> 10)) as u16;
}
// The number is normalized, start assembling half precision version let half_sign = sign >> 16; // Unbias the exponent, then bias for half precision let unbiased_exp = ((exp >> 20) as i64) - 1023; let half_exp = unbiased_exp + 15;
// Check for exponent overflow, return +infinity if half_exp >= 0x1F { return (half_sign | 0x7C00u32) as u16;
}
// Check for underflow if half_exp <= 0 { // Check mantissa for what we can do if10 - half_exp > 21 { // No rounding possibility, so this is a full underflow, return signed zero return half_sign as u16;
} // Don't forget about hidden leading mantissa bit when assembling mantissa let man = man | 0x0010_0000u32; letmut half_man = man >> (11 - half_exp); // Check for rounding (see comment above functions) let round_bit = 1 << (10 - half_exp); if (man & round_bit) != 0 && (man & (3 * round_bit - 1)) != 0 {
half_man += 1;
} // No exponent for subnormals return (half_sign | half_man) as u16;
}
// Rebias the exponent let half_exp = (half_exp as u32) << 10; let half_man = man >> 10; // Check for rounding (see comment above functions) let round_bit = 0x0000_0200u32; if (man & round_bit) != 0 && (man & (3 * round_bit - 1)) != 0 { // Round it
((half_sign | half_exp | half_man) + 1) as u16
} else {
(half_sign | half_exp | half_man) as u16
}
}
#[inline] pub(crate) constfn f16_to_f32_fallback(i: u16) -> f32 { // Check for signed zero // TODO: Replace mem::transmute with from_bits() once from_bits is const-stabilized if i & 0x7FFFu16 == 0 { returnunsafe { mem::transmute::<u32, f32>((i as u32) << 16) };
}
let half_sign = (i & 0x8000u16) as u32; let half_exp = (i & 0x7C00u16) as u32; let half_man = (i & 0x03FFu16) as u32;
// Check for an infinity or NaN when all exponent bits set if half_exp == 0x7C00u32 { // Check for signed infinity if mantissa is zero if half_man == 0 { returnunsafe { mem::transmute::<u32, f32>((half_sign << 16) | 0x7F80_0000u32) };
} else { // NaN, keep current mantissa but also set most significiant mantissa bit returnunsafe {
mem::transmute::<u32, f32>((half_sign << 16) | 0x7FC0_0000u32 | (half_man << 13))
};
}
}
// Calculate single-precision components with adjusted exponent let sign = half_sign << 16; // Unbias exponent let unbiased_exp = ((half_exp as i32) >> 10) - 15;
// Check for subnormals, which will be normalized by adjusting exponent if half_exp == 0 { // Calculate how much to adjust the exponent by let e = leading_zeros_u16(half_man as u16) - 6;
// Rebias and adjust exponent let exp = (127 - 15 - e) << 23; let man = (half_man << (14 + e)) & 0x7F_FF_FFu32; returnunsafe { mem::transmute::<u32, f32>(sign | exp | man) };
}
// Rebias exponent for a normalized normal let exp = ((unbiased_exp + 127) as u32) << 23; let man = (half_man & 0x03FFu32) << 13; unsafe { mem::transmute::<u32, f32>(sign | exp | man) }
}
#[inline] pub(crate) constfn f16_to_f64_fallback(i: u16) -> f64 { // Check for signed zero // TODO: Replace mem::transmute with from_bits() once from_bits is const-stabilized if i & 0x7FFFu16 == 0 { returnunsafe { mem::transmute::<u64, f64>((i as u64) << 48) };
}
let half_sign = (i & 0x8000u16) as u64; let half_exp = (i & 0x7C00u16) as u64; let half_man = (i & 0x03FFu16) as u64;
// Check for an infinity or NaN when all exponent bits set if half_exp == 0x7C00u64 { // Check for signed infinity if mantissa is zero if half_man == 0 { returnunsafe {
mem::transmute::<u64, f64>((half_sign << 48) | 0x7FF0_0000_0000_0000u64)
};
} else { // NaN, keep current mantissa but also set most significiant mantissa bit returnunsafe {
mem::transmute::<u64, f64>(
(half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 42),
)
};
}
}
// Calculate double-precision components with adjusted exponent let sign = half_sign << 48; // Unbias exponent let unbiased_exp = ((half_exp as i64) >> 10) - 15;
// Check for subnormals, which will be normalized by adjusting exponent if half_exp == 0 { // Calculate how much to adjust the exponent by let e = leading_zeros_u16(half_man as u16) - 6;
// Rebias and adjust exponent let exp = ((1023 - 15 - e) as u64) << 52; let man = (half_man << (43 + e)) & 0xF_FFFF_FFFF_FFFFu64; returnunsafe { mem::transmute::<u64, f64>(sign | exp | man) };
}
// Rebias exponent for a normalized normal let exp = ((unbiased_exp + 1023) as u64) << 52; let man = (half_man & 0x03FFu64) << 42; unsafe { mem::transmute::<u64, f64>(sign | exp | man) }
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.