Skip to content
TheChatArchiveThe Chat Archive
o3-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

Please convert the following C code to rust: [code]

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash’s answer.
  1. User

    Please convert the following C code to rust:

    static inline float to_srgb(float in) {
        float abs = fabsf(in);
        float x = abs - 5.35862651e-04;
        float x2 = x * x;
        float even1 = fmaf(x, -9.12795913e-01, -2.88143143e-02);
        float even2 = fmaf(x2, -7.29192910e-01, even1);
        float odd1 = fmaf(x, 1.06133172e+00, 1.40194533e+00);
        float odd2 = fmaf(x2, 2.07758287e-01, odd1);
        __builtin_assume(x >= 0);
        float poly = fmaf(odd2, sqrtf(x), even2);
        float lin = abs * 12.92;
        float z = abs > 0.0031308 ? poly : lin;
        return copysignf(z, in);
    }
    
    void to_srgb_vec(float *restrict dst, float *restrict src, size_t n) {
        for (; n--; ++src, ++dst) {
            *dst = to_srgb(*src);
            if ((n & 3) == 3) *dst = *src;
        }
    }
    
  2. Assistant

    Below is a line-for-line, “keep-the-quirks” translation of your C routine into safe, idiomatic Rust.
    It uses the standard‐library equivalents of fabsf, fmaf, copysignf, etc., and preserves the odd (n & 3) == 3 overwrite that the original loop performs.

    // ─────────────────────────────────────────────────────────────────────────────
    //  sRGB helper – direct port of the C version
    // ─────────────────────────────────────────────────────────────────────────────
    #[inline]
    fn to_srgb(input: f32) -> f32 {
        // fabsf
        let abs = input.abs();
    
        // Original polynomial approximation
        let x  = abs - 5.358_626_51e-4;
        let x2 = x * x;
    
        // fmaf → f32::mul_add
        let even1 = x .mul_add(-9.127_959_13e-1, -2.881_431_43e-2);
        let even2 = x2.mul_add(-7.291_929_10e-1, even1);
    
        let odd1  = x .mul_add( 1.061_331_72,  1.401_945_33);
        let odd2  = x2.mul_add( 2.077_582_87e-1, odd1);
    
        // No exact `__builtin_assume`; we simply omit it.
    
        let poly = odd2.mul_add(x.sqrt(), even2);
        let lin  = abs * 12.92;
    
        let z = if abs > 0.003_130_8 { poly } else { lin };
    
        // copysignf
        z.copysign(input)
    }
    
    // ─────────────────────────────────────────────────────────────────────────────
    //  Vector variant – same semantics as the C version
    // ─────────────────────────────────────────────────────────────────────────────
    pub fn to_srgb_vec(dst: &mut [f32], src: &[f32]) {
        assert!(dst.len() >= src.len(), "dst must be at least as long as src");
    
        // n is the “remaining element count”, decremented *before* the body
        let mut n = src.len();
    
        for (s, d) in src.iter().zip(dst.iter_mut()) {
            n -= 1;                   // mimic `n--` in the C for-loop header
            *d = to_srgb(*s);
    
            // Keep the same strange overwrite as the C code.
            if (n & 3) == 3 {
                *d = *s;
            }
        }
    }
    

    How the C constructs map to Rust ────────────────────────────────

    C constructRust equivalent
    fabsf(x)x.abs()
    fmaf(a, b, c)a.mul_add(b, c)
    sqrtf(x)x.sqrt()
    copysignf(z, in)z.copysign(in)
    static inline#[inline] attribute
    pointer loopslice iteration (for (s,d) in …)
    restrictslices already guarantee no aliasing
    __builtin_assumeomitted (LLVM may still constant-fold)

    Compile & test ──────────────

    rustc -C target-cpu=native your_file.rs
    

    The code is 100 % safe Rust—no unsafe blocks needed—yet still enables the compiler to emit FMA instructions and vectorize the loop when possible.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.