79235201

Date: 2024-11-28 20:09:57
Score: 2
Natty:
Report link

Here's a version that's basically a one-to-one translation of the original C code. This is far from idiomatic Rust of course, but helps illustrate how raw pointers work in unsafe Rust.

fn Q_rsqrt( number: f32 ) -> f32
{
    let mut i: i32;
    let (x2, mut y): (f32, f32);
    const threehalfs: f32 = 1.5;

    x2 = number * 0.5;
    y  = number;
    i  = unsafe { * ( &raw const y as *const i32 ) }; // evil floating-point bit level hacking
    i  = 0x5f3759df - ( i >> 1 );                     // what the fuck?
    y  = unsafe { * ( &raw const i as *const f32 ) };
    y  = y * ( threehalfs - ( x2 * y * y ) );         // 1st iteration
//  y  = y * ( threehalfs - ( x2 * y * y ) );         // 2nd iteration, this can be removed

    return y;
}

(playground link)

Reasons:
  • Blacklisted phrase (2): fuck
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Arbel Groshaus