test_float.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * This file is Copyright (c) 2010 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include <stdio.h>
  6. /*
  7. * Copyright (c) 2006 Chris Kuethe <chris.kuethe@gmail.com>
  8. *
  9. * This file is Copyright (c)2005-2019 by the GPSD project
  10. * SPDX-License-Identifier: BSD-2-clause
  11. */
  12. /*
  13. * this simple program tests to see whether your system can do proper
  14. * single and double precision floating point. This is apparently Very
  15. * Hard To Do(tm) on embedded systems, judging by the number of broken
  16. * ARM toolchains I've seen... :(
  17. *
  18. * Added in 2015 by ESR: Test for C99 behavior on negative operand(s)
  19. * of %, that is the result should have the sign of the left operand.
  20. *
  21. * compile with: gcc -O -o test_float test_float.c
  22. * (use whatever -O level you like)
  23. */
  24. int main(void);
  25. int test_single(void);
  26. int test_double(void);
  27. int test_modulo(void);
  28. int main(void) {
  29. int i, j, k;
  30. if ((i = test_single()))
  31. printf("WARNING: Single-precision "
  32. "floating point math might be broken\n");
  33. if ((j = test_double()))
  34. printf("WARNING: Double-precision "
  35. "floating point math might be broken\n");
  36. if ((k = test_modulo()))
  37. printf("WARNING: Modular arithmetic is broken\n");
  38. i += j;
  39. i += k;
  40. if (i == 0)
  41. printf("floating point and modular math appears to work\n");
  42. return i;
  43. }
  44. int test_single(void) {
  45. static float f;
  46. static int i;
  47. static int e = 0;
  48. /* addition test */
  49. f = 1.0;
  50. for(i = 0; i < 10; i++)
  51. f += (1<<i);
  52. if (f != 1024.0) {
  53. printf("s1 ");
  54. e++;
  55. }
  56. /* subtraction test */
  57. f = 1024.0;
  58. for(i = 0; i < 10; i++)
  59. f -= (1<<i);
  60. if (f != 1.0) {
  61. printf("s2 ");
  62. e++;
  63. }
  64. /* multiplication test */
  65. f = 1.0;
  66. for(i = 1; i < 10; i++)
  67. f *= i;
  68. if (f != 362880.0) {
  69. printf("s3 ");
  70. e++;
  71. }
  72. /* division test */
  73. f = 362880.0;
  74. for(i = 1; i < 10; i++)
  75. f /= i;
  76. if (f != 1.0) {
  77. printf("s4 ");
  78. e++;
  79. }
  80. /* multiply-accumulate test */
  81. f = 0.5;
  82. for(i = 1; i < 1000000; i++) {
  83. f += 2.0;
  84. f *= 0.5;
  85. }
  86. if (f != 2.0) {
  87. printf("s5 ");
  88. e++;
  89. }
  90. /* divide-subtract test */
  91. f = 2.0;
  92. for(i = 1; i < 1000000; i++) {
  93. f /= 0.5;
  94. f -= 2.0;
  95. }
  96. if (f != 2.0) {
  97. printf("s6 ");
  98. e++;
  99. }
  100. /* add-multiply-subtract-divide test */
  101. f = 1000000.0;
  102. for(i = 1; i < 1000000; i++)
  103. f = ((((f + 1.5) * 0.5) - 1.25) / 0.5);
  104. if (f != 1.0) {
  105. printf("s7 ");
  106. e++;
  107. }
  108. /* multiply-add-divide-subtract test */
  109. f = 1.0;
  110. for(i = 1; i < 1000000; i++)
  111. f = ((((f * 5.0) + 3.0) / 2.0) - 3.0);
  112. if (f != 1.0)
  113. printf("s8 ");
  114. /* subtract-divide-add-multiply test */
  115. f = 8.0;
  116. for(i = 1; i < 1000000; i++)
  117. f = ((((f - 5.0) / 2.0) + 2.5) * 2.0);
  118. if (f != 8.0) {
  119. printf("s9 ");
  120. e++;
  121. }
  122. /* divide-subtract-multiply-add test */
  123. f = 42.0;
  124. for(i = 1; i < 1000000; i++)
  125. f = ((((f / 6.0) - 5.0) * 19.75 ) + 2.5);
  126. if (f != 42.0) {
  127. printf("s10 ");
  128. e++;
  129. }
  130. if (e) {
  131. printf("\n");
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. int test_double(void) {
  137. static double f;
  138. static int i;
  139. static int e = 0;
  140. /* addition test */
  141. f = 1.0;
  142. for(i = 0; i < 10; i++)
  143. f += (1<<i);
  144. if (f != 1024.0) {
  145. printf("d1 ");
  146. e++;
  147. }
  148. /* subtraction test */
  149. f = 1024.0;
  150. for(i = 0; i < 10; i++)
  151. f -= (1<<i);
  152. if (f != 1.0) {
  153. printf("d2 ");
  154. e++;
  155. }
  156. /* multiplication test */
  157. f = 1.0;
  158. for(i = 1; i < 10; i++)
  159. f *= i;
  160. if (f != 362880.0) {
  161. printf("d3 ");
  162. e++;
  163. }
  164. /* division test */
  165. f = 362880.0;
  166. for(i = 1; i < 10; i++)
  167. f /= i;
  168. if (f != 1.0) {
  169. printf("d4 ");
  170. e++;
  171. }
  172. /* multiply-accumulate test */
  173. f = 0.5;
  174. for(i = 1; i < 1000000; i++) {
  175. f += 2.0;
  176. f *= 0.5;
  177. }
  178. if (f != 2.0) {
  179. printf("d5 ");
  180. e++;
  181. }
  182. /* divide-subtract test */
  183. f = 2.0;
  184. for(i = 1; i < 1000000; i++) {
  185. f /= 0.5;
  186. f -= 2.0;
  187. }
  188. if (f != 2.0) {
  189. printf("d6 ");
  190. e++;
  191. }
  192. /* add-multiply-subtract-divide test */
  193. f = 1000000.0;
  194. for(i = 1; i < 1000000; i++)
  195. f = ((((f + 1.5) * 0.5) - 1.25) / 0.5);
  196. if (f != 1.0) {
  197. printf("d7 ");
  198. e++;
  199. }
  200. /* multiply-add-divide-subtract test */
  201. f = 1.0;
  202. for(i = 1; i < 1000000; i++)
  203. f = ((((f * 5.0) + 3.0) / 2.0) - 3.0);
  204. if (f != 1.0)
  205. printf("d8 ");
  206. /* subtract-divide-add-multiply test */
  207. f = 8.0;
  208. for(i = 1; i < 1000000; i++)
  209. f = ((((f - 5.0) / 2.0) + 2.5) * 2.0);
  210. if (f != 8.0) {
  211. printf("d9 ");
  212. e++;
  213. }
  214. /* divide-subtract-multiply-add test */
  215. f = 42.0;
  216. for(i = 1; i < 1000000; i++)
  217. f = ((((f / 6.0) - 5.0) * 19.75 ) + 2.5);
  218. if (f != 42.0) {
  219. printf("d10 ");
  220. e++;
  221. }
  222. if (e) {
  223. printf("\n");
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. int test_modulo(void) {
  229. static int e = 0;
  230. /* make sure that gcc does not optimize these away */
  231. volatile int a;
  232. volatile int b;
  233. a = -5;
  234. b = 2;
  235. //cppcheck-suppress knownConditionTrueFalse
  236. if (a % b != -1) {
  237. printf("m1 ");
  238. e++;
  239. }
  240. a = -5;
  241. b = -2;
  242. //cppcheck-suppress knownConditionTrueFalse
  243. if (a % b != -1) {
  244. printf("m2 ");
  245. e++;
  246. }
  247. a = 5;
  248. b = -2;
  249. //cppcheck-suppress knownConditionTrueFalse
  250. if (a % b != 1) {
  251. printf("m3 ");
  252. e++;
  253. }
  254. if (e) {
  255. printf("\n");
  256. return 1;
  257. }
  258. return 0;
  259. }