Modernize code without rewriting it by hand, and turn decades of IP into an asset, not a liability.
Bring modeling and simulation codebases onto modern languages and hardware, with results verified against the original.
The Code Metal Platform
hover or select a capability
hardware
Write once, run on any hardware, with peak performance on every chip you choose.
Skip the hand-port to low-level code and take prototypes to production faster.
segment shadow
theme
TRUSTED BY
theme
One platform, any code-to-code task
Code Metal automates complete code-to-code engineering workflows, from understanding an existing system to delivering verified software optimized for its target hardware.
legacy/shading.c → src/shading.rsFUNC · EQ · 9442
input · C99
/* legacy/shading.c — rev 1999 */typedefstruct { float x, y, z; } vec3;
float Q_rsqrt(float number) {
long i;
float x2, y;
constfloat threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = *(long *) &y; /* UB: type pun */ i = 0x5f3759df - (i >> 1);
y = *(float *) &i;
y = y * (threehalfs - (x2 * y * y));
return y;
}
float lambert(vec3 n, vec3 l) {
float d = n.x*l.x + n.y*l.y + n.z*l.z;
return d < 0 ? 0 : d; /* n, l assumed unit */}
output · Rust 2024
#![forbid(unsafe_code)]#[derive(Clone, Copy, Debug)]struct Vec3(f32, f32, f32);
impl Vec3 {
fn new(x: f32, y: f32, z: f32) -> Self {
Self(x, y, z) } }
fn q_rsqrt(x: f32) -> f32 {
let half = 0.5 * x;
assert!(x.is_normal() && x > 0.0, "positive normal f32 required");
let bits = x.to_bits();
let bits = 0x5f3759df - (bits >> 1);
let y = f32::from_bits(bits);
y * (1.5 - half * y * y) }
impl Vec3 {
fn length_squared(self) -> f32 { self.dot(self) }
#[inline]fn dot(self, other: Self) -> f32 {
self.0 * other.0 + self.1 * other.1 + self.2 * other.2 }
fn scale(self, s: f32) -> Self { Self(self.0*s, self.1*s, self.2*s) }
fn normalized_approx(self) -> Option<Self> { let n=self.length_squared();
if !n.is_normal() || n <= 0.0 { returnNone; }
Some(self.scale(q_rsqrt(n))) } }