Skip to content

255u8.leading_zeros() is wrong on emscripten #39119

Closed
@alexcrichton

Description

@alexcrichton

Running some tests this program is incorrect when run through emscripten currently:

fn main() {                                               
    println!("{}", 255u8.leading_zeros());                
}

This program prints 232 when I'd expect 0. This may be due to the implementation in emscripten-core/emscripten#4544 which looks like:

function _llvm_ctlz_i8(x, isZeroUndef) { 
    x = x | 0;                           
    isZeroUndef = isZeroUndef | 0;       
    return (Math_clz32(x) | 0) - 24 | 0; 
}                                        

(in the compiled JS at least)

x coming in is -1, so Math_clz32(x) returns 0, and then chopping of 24 returns -24. When interpreted as a u8 that's 232 (what we see).

@kripken I'm not familiar with asmjs internals/representations, but is this a bug in the intrinsic? Or perhaps in our own representation of integers along the line? Or did I get unlucky with an LLVM mismatch or something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-asmjsTarget: asm.js - http://asmjs.org/O-emscriptenTarget: 50% off wasm32-unknown-musl. the savings come out of stdio.h, but hey, you get SDL!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions