[PATCH] D63038: [SimplifyLibCalls] powf(x, sitofp(n)) -> powi(x, n)

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 10 09:30:08 PDT 2019


spatel added a comment.

In D63038#1535140 <https://reviews.llvm.org/D63038#1535140>, @xbolva00 wrote:

> In D63038#1535121 <https://reviews.llvm.org/D63038#1535121>, @nikic wrote:
>
> > Can you please provide some more information under which circumstances `powf(x, (float) y)` will provide a different result than `powi(x, y)`, and which fast-math flags specifically are necessary to make that transform legal?
>
>
> I don’t have such info, I personally think this is fine also without fast math, but since I cannot say it for sure, In the first version, I put it under fast math.
>
> Maybe some experts like @spatel or @efriedma can confirm that this transformation is always fine?


I don't see how this is valid without some kind of fast-math. What if the integer exponent is not exactly representable as an FP value?

  $ cat powi.c 
  #include <stdio.h>
  #include <math.h>
  #include <stdlib.h>
  
  int main(int argc, char *argv[]) {
    float base = atof(argv[1]);
    printf("base as float = %.8f\n", base);
  
    int exponent = atoi(argv[2]);
    printf("exponent = %d\n", exponent);
    printf("exponent as float = %.8f\n", (float)exponent);
  
    float d = powf(base, exponent);
    float i = __builtin_powif(base, exponent);
    printf("powf = %f\n", d);
    printf("powif = %f\n", i);
    return 0;
  }
  
  $ ./a.out 1.0000001 16777217
  base as float = 1.00000012
  exponent = 16777217
  exponent as float = 16777216.00000000
  powf = 7.389055
  powif = 7.385338


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63038/new/

https://reviews.llvm.org/D63038





More information about the llvm-commits mailing list