Skip to content

Bad code generation with -ffast-math #1762

@binji

Description

@binji

Compiling this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
  char* endptr;
  --argc, ++argv;
  double total = 0.0;
  for (; argc; argc--, argv++) {
    total += strtod(*argv, &endptr);
  }
  printf("total: %g\n", total);
  return 0;
}

with the command line:

em++ -ffast-math fast_double.c -o fast_double.js

Compiles without error, but generates the erroneous output:

...
case 3: 
 var $3=$argv_addr;
 var $4=HEAP32[(($3)>>2)];
 var $call=_strtod($4, $endptr);
 var $5=$total;
 var $add=((double))+($call);
 $total=$add;
 var $6=$argc_addr;
 var $dec1=((($6)-(1))|0);
 $argc_addr=$dec1;
 var $7=$argv_addr;
 var $incdec_ptr2=(($7+4)|0);
 $argv_addr=$incdec_ptr2;
 label = 2; break;
...

Note the line with (double).

Here is a snippet of the generated bitcode:

for.body:                                         ; preds = %for.cond
  %3 = load i8*** %argv.addr, align 4
  %4 = load i8** %3, align 4
  %call = call double @strtod(i8* %4, i8** %endptr)
  %5 = load double* %total, align 8
  %add = fadd fast double %5, %call
  store double %add, double* %total, align 8
  br label %for.inc

It seems that the line "fadd fast double" is misparsed with fast being the type and double being the variable name.

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