/gcc/testsuite/go.test/test/cmplxdivide.c

https://bitbucket.org/pizzafactory/pf-gcc · C · 82 lines · 60 code · 12 blank · 10 comment · 14 complexity · 0e77b438ff6af30dbb2efc36c42b1a80 MD5 · raw file

  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // gcc '-std=c99' cmplxdivide.c && a.out >cmplxdivide1.go
  5. #include <complex.h>
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  10. double f[] = {
  11. 0,
  12. 1,
  13. -1,
  14. 2,
  15. NAN,
  16. INFINITY,
  17. -INFINITY,
  18. };
  19. char*
  20. fmt(double g)
  21. {
  22. static char buf[10][30];
  23. static int n;
  24. char *p;
  25. p = buf[n++];
  26. if(n == 10)
  27. n = 0;
  28. sprintf(p, "%g", g);
  29. if(strcmp(p, "-0") == 0)
  30. strcpy(p, "negzero");
  31. return p;
  32. }
  33. int
  34. iscnan(double complex d)
  35. {
  36. return !isinf(creal(d)) && !isinf(cimag(d)) && (isnan(creal(d)) || isnan(cimag(d)));
  37. }
  38. double complex zero; // attempt to hide zero division from gcc
  39. int
  40. main(void)
  41. {
  42. int i, j, k, l;
  43. double complex n, d, q;
  44. printf("// # generated by cmplxdivide.c\n");
  45. printf("\n");
  46. printf("package main\n");
  47. printf("var tests = []Test{\n");
  48. for(i=0; i<nelem(f); i++)
  49. for(j=0; j<nelem(f); j++)
  50. for(k=0; k<nelem(f); k++)
  51. for(l=0; l<nelem(f); l++) {
  52. n = f[i] + f[j]*I;
  53. d = f[k] + f[l]*I;
  54. q = n/d;
  55. // BUG FIX.
  56. // Gcc gets the wrong answer for NaN/0 unless both sides are NaN.
  57. // That is, it treats (NaN+NaN*I)/0 = NaN+NaN*I (a complex NaN)
  58. // but it then computes (1+NaN*I)/0 = Inf+NaN*I (a complex infinity).
  59. // Since both numerators are complex NaNs, it seems that the
  60. // results should agree in kind. Override the gcc computation in this case.
  61. if(iscnan(n) && d == 0)
  62. q = (NAN+NAN*I) / zero;
  63. printf("\tTest{cmplx(%s, %s), cmplx(%s, %s), cmplx(%s, %s)},\n",
  64. fmt(creal(n)), fmt(cimag(n)),
  65. fmt(creal(d)), fmt(cimag(d)),
  66. fmt(creal(q)), fmt(cimag(q)));
  67. }
  68. printf("}\n");
  69. return 0;
  70. }