You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
1.7 KiB

пре 2 година
  1. /* Testing Code */
  2. #include <limits.h>
  3. #include <math.h>
  4. /* Routines used by floation point test code */
  5. /* Convert from bit level representation to floating point number */
  6. float u2f(unsigned u) {
  7. union {
  8. unsigned u;
  9. float f;
  10. } a;
  11. a.u = u;
  12. return a.f;
  13. }
  14. /* Convert from floating point number to bit-level representation */
  15. unsigned f2u(float f) {
  16. union {
  17. unsigned u;
  18. float f;
  19. } a;
  20. a.f = f;
  21. return a.u;
  22. }
  23. //1
  24. int test_bitXor(int x, int y)
  25. {
  26. return x^y;
  27. }
  28. int test_tmin(void) {
  29. return 0x80000000;
  30. }
  31. //2
  32. int test_isTmax(int x) {
  33. return x == 0x7FFFFFFF;
  34. }
  35. int test_allOddBits(int x) {
  36. int i;
  37. for (i = 1; i < 32; i+=2)
  38. if ((x & (1<<i)) == 0)
  39. return 0;
  40. return 1;
  41. }
  42. int test_negate(int x) {
  43. return -x;
  44. }
  45. //3
  46. int test_isAsciiDigit(int x) {
  47. return (0x30 <= x) && (x <= 0x39);
  48. }
  49. int test_conditional(int x, int y, int z)
  50. {
  51. return x?y:z;
  52. }
  53. int test_isLessOrEqual(int x, int y)
  54. {
  55. return x <= y;
  56. }
  57. //4
  58. int test_logicalNeg(int x)
  59. {
  60. return !x;
  61. }
  62. int test_howManyBits(int x) {
  63. unsigned int a, cnt;
  64. x = x<0 ? -x-1 : x;
  65. a = (unsigned int)x;
  66. for (cnt=0; a; a>>=1, cnt++)
  67. ;
  68. return (int)(cnt + 1);
  69. }
  70. //float
  71. unsigned test_floatScale2(unsigned uf) {
  72. float f = u2f(uf);
  73. float tf = 2*f;
  74. if (isnan(f))
  75. return uf;
  76. else
  77. return f2u(tf);
  78. }
  79. int test_floatFloat2Int(unsigned uf) {
  80. float f = u2f(uf);
  81. int x = (int) f;
  82. return x;
  83. }
  84. unsigned test_floatPower2(int x) {
  85. float result = 1.0;
  86. float p2 = 2.0;
  87. int recip = (x < 0);
  88. /* treat tmin specially */
  89. if ((unsigned)x == 0x80000000) {
  90. return 0;
  91. }
  92. if (recip) {
  93. x = -x;
  94. p2 = 0.5;
  95. }
  96. while (x > 0) {
  97. if (x & 0x1)
  98. result = result * p2;
  99. p2 = p2 * p2;
  100. x >>= 1;
  101. }
  102. return f2u(result);
  103. }