Skip to content

redundant second cast to integer after already casted int value in asm.js? #2951

Open
@MaxGraey

Description

@MaxGraey

https://webassembly.studio/?f=oeg02m22gmq

export function add_div(x: i32): i32 {
  return u32(x) / 100 + x / -100;
}

output:

 function main_add_div($0) {
  $0 = $0 | 0;
  return (($0 | 0) / (-100 | 0) | 0) + (($0 >>> 0) / (100 >>> 0) | 0) | 0 | 0;
 }

but I'm expecting single | 0 at the end:

 function main_add_div($0) {
  $0 = $0 | 0;
  return (($0 | 0) / (-100 | 0) | 0) + (($0 >>> 0) / (100 >>> 0) | 0) | 0;
 }

similar here
but after extra optimizations it normalized to expected. But I guess it could be done earlier without extra opts?

also since asm2js is more like js code and less than asm.js could we simplify some conversion? Like this one:

function i32_trunc_u_f32($0) {
  $0 = Math_fround($0);
  return ~~$0 >>> 0 | 0;
}

could be just:

function i32_trunc_u_f32($0) {
  $0 = Math_fround($0);
  return $0 | 0;
}

And Closure Compiler can't do such optimizations btw

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions