There is an intrinsic in the stdsimd crate that initializes a vector register to zero. The way that intrinsic ought to implemented is by a pxor instruction that xors the register with itself [0]. In a nutshell:
fn main() {
unsafe {
// u32 for exposition only, the real type is LLVM's x86_mmx
// which in rust is a `#[repr(simd)] struct __m64(i64);`
let a: u32 = ::std::mem::uninitialized();
let b = a ^ a; // read of uninitialized memory
println!("{}", b); // should always print: "0"
}
}
The question is: is this undefined behavior? The nomicon says:
Attempting to interpret this memory as a value of any type will cause Undefined Behavior. Do Not Do This.
and the docs of mem::uninitialized say:
It is undefined behavior to read uninitialized memory, even just an uninitialized boolean.
Citing the reference which says:
Behavior considered undefined: Reads of undef (uninitialized) memory.
So I would guess that the answer is yes?
In the same spirit, this is just a personal question of mine, is this:
fn main() {
unsafe {
let a: u32 = ::std::mem::uninitialized();
let b = a * 0; // read of uninitialized memory
println!("{}", b); // should always print: "0"
}
}
undefined behavior?
[0]: In particular, we are implementing _mm_setzero_si64, which is the function that sets an MMX register to zero, that is, our let a: MMX = 0, so we can’t really set the register to anything but mem::uninitialized, and the only way to implement it is by xoring the uninitialized register with itself.