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.

124 lines
3.3 KiB

9 months ago
  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. /* Copyright (C) 1991-2020 Free Software Foundation, Inc.
  24. This file is part of the GNU C Library.
  25. The GNU C Library is free software; you can redistribute it and/or
  26. modify it under the terms of the GNU Lesser General Public
  27. License as published by the Free Software Foundation; either
  28. version 2.1 of the License, or (at your option) any later version.
  29. The GNU C Library is distributed in the hope that it will be useful,
  30. but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. Lesser General Public License for more details.
  33. You should have received a copy of the GNU Lesser General Public
  34. License along with the GNU C Library; if not, see
  35. <https://www.gnu.org/licenses/>. */
  36. /* This header is separate from features.h so that the compiler can
  37. include it implicitly at the start of every compilation. It must
  38. not itself include <features.h> or any other header that includes
  39. <features.h> because the implicit include comes before any feature
  40. test macros that may be defined in a source file before it first
  41. explicitly includes a system header. GCC knows the name of this
  42. header in order to preinclude it. */
  43. /* glibc's intent is to support the IEC 559 math functionality, real
  44. and complex. If the GCC (4.9 and later) predefined macros
  45. specifying compiler intent are available, use them to determine
  46. whether the overall intent is to support these features; otherwise,
  47. presume an older compiler has intent to support these features and
  48. define these macros by default. */
  49. /* wchar_t uses Unicode 10.0.0. Version 10.0 of the Unicode Standard is
  50. synchronized with ISO/IEC 10646:2017, fifth edition, plus
  51. the following additions from Amendment 1 to the fifth edition:
  52. - 56 emoji characters
  53. - 285 hentaigana
  54. - 3 additional Zanabazar Square characters */
  55. //1
  56. int test_bitXor(int x, int y)
  57. {
  58. return x^y;
  59. }
  60. int test_tmin(void) {
  61. return 0x80000000;
  62. }
  63. //2
  64. int test_isTmax(int x) {
  65. return x == 0x7FFFFFFF;
  66. }
  67. int test_allOddBits(int x) {
  68. int i;
  69. for (i = 1; i < 32; i+=2)
  70. if ((x & (1<<i)) == 0)
  71. return 0;
  72. return 1;
  73. }
  74. int test_negate(int x) {
  75. return -x;
  76. }
  77. //3
  78. int test_isAsciiDigit(int x) {
  79. return (0x30 <= x) && (x <= 0x39);
  80. }
  81. int test_conditional(int x, int y, int z)
  82. {
  83. return x?y:z;
  84. }
  85. int test_isLessOrEqual(int x, int y)
  86. {
  87. return x <= y;
  88. }
  89. //4
  90. int test_logicalNeg(int x)
  91. {
  92. return !x;
  93. }
  94. int test_howManyBits(int x) {
  95. unsigned int a, cnt;
  96. x = x<0 ? -x-1 : x;
  97. a = (unsigned int)x;
  98. for (cnt=0; a; a>>=1, cnt++)
  99. ;
  100. return (int)(cnt + 1);
  101. }
  102. //float
  103. unsigned test_floatScale2(unsigned uf) {
  104. float f = u2f(uf);
  105. float tf = 2*f;
  106. if (isnan(f))
  107. return uf;
  108. else
  109. return f2u(tf);
  110. }
  111. int test_floatFloat2Int(unsigned uf) {
  112. float f = u2f(uf);
  113. int x = (int) f;
  114. return x;
  115. }
  116. // #include "floatPower2.c"