[PATCH] D41286: [InstCombine] Missed optimization in math expression: sin(x) / cos(x) => tan(x)

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 8 14:46:19 PST 2018


spatel added a comment.

> If you have a numerical explanation of why this patch can go in, I'll be happy to accept,

I'm not sure what you're looking for. Brute force sin/cos is equal to tan for every possible value?

  #include <math.h>
  #include <stdio.h>
  #include <string.h>
  
  int main() {
    unsigned int i;
    for (i = 0x00000000; i<0x7f800002; i++) {
      float f;
      memcpy(&f, &i, 4);
      float slow = sin(f) / cos(f);
      float fast = tan(f);
      if (slow != fast) {
        printf("\nsin(%f)/cos(%f) = %f, tan(%f) = %f\n", f, f, slow, f, fast);
      }
      if (i % (1024*1024*256) == 0) printf("0x%x...\n", i); 
    }
  
    return 0;
  } 



  $  clang -O2 tan_checker.c ; ./a.out 
  0x0...
  0x10000000...
  0x20000000...
  0x30000000...
  0x40000000...
  0x50000000...
  0x60000000...
  0x70000000...
  
  sin(inf)/cos(inf) = nan, tan(inf) = nan
  
  sin(nan)/cos(nan) = nan, tan(nan) = nan

Note that I'm testing on macOS x86 with strict math (the sin and cos calls are replaced by __sincos_stret). 
Double-check to make sure I haven't screwed anything up there, but this suggests we could do this transform without -ffast-math?


https://reviews.llvm.org/D41286





More information about the llvm-commits mailing list