custom_float_funcs_test.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include <stdio.h>
  2. #include "pico/stdlib.h"
  3. #include "pico/float.h"
  4. #include "math.h"
  5. #if 0
  6. #define printf(...) ((void)0)
  7. #endif
  8. #if 0
  9. #define stop() return -1
  10. #else
  11. #define stop() rc=1
  12. #endif
  13. #define test_assert(x) ({ if (!(x)) { printf("Assertion failed: ");puts(#x);printf(" at " __FILE__ ":%d\n", __LINE__); stop(); } })
  14. #define test_checkf(x, expected, msg) ({ if ((x) != (expected)) { printf(" %s: %f != %f\n", msg, x, expected); stop(); } })
  15. #define test_checki(x, expected, msg) ({ if ((x) != (expected)) { printf(" %s: %d != %d\n", msg, x, expected); stop(); } })
  16. #define test_checku(x, expected, msg) ({ if ((uint32_t)(x) != (uint32_t)(expected)) { printf(" %s: %u != %u\n", msg, x, expected); stop(); } })
  17. #define test_checki64(x, expected, msg) ({ if ((x) != (expected)) { printf(" %s: %lld != %lld\n", msg, (int64_t)(x), (int64_t)(expected)); stop(); } })
  18. #define test_checku64(x, expected, msg) ({ if ((uint64_t)(x) != (uint64_t)(expected)) { printf(" %s: %llu != %llu\n", msg, (uint64_t)(x), (uint64_t)(expected)); stop(); } })
  19. #if !(LIB_PICO_FLOAT_COMPILER || defined(__riscv))
  20. static inline float fix2float_8(int32_t m) { return fix2float(m, 8); }
  21. static inline float fix2float_12(int32_t m) { return fix2float(m, 12); }
  22. static inline float fix2float_16(int32_t m) { return fix2float(m, 16); }
  23. static inline float fix2float_24(int32_t m) { return fix2float(m, 24); }
  24. static inline float fix2float_28(int32_t m) { return fix2float(m, 28); }
  25. static inline float fix2float_32(int32_t m) { return fix2float(m, 32); }
  26. static inline float ufix2float_12(int32_t m) { return ufix2float(m, 12); }
  27. static inline float float2fix_12(int32_t m) { return float2fix(m, 12); }
  28. static inline float float2ufix_12(int32_t m) { return float2ufix(m, 12); }
  29. #endif
  30. #if 1 && (LIB_PICO_FLOAT_COMPILER || defined(__riscv))
  31. #if __SOFTFP__ || defined(__riscv)
  32. #define FREG "+r"
  33. #else
  34. #define FREG "+t"
  35. #endif
  36. // prevent the compiler from eliding the calculations
  37. #define float2int_z(f) ({ float _f = f; pico_default_asm_volatile("" : FREG (_f)); float2 ## int_z(_f); })
  38. #define float2uint_z(f) ({ float _f = f; pico_default_asm_volatile("" : FREG (_f)); float2 ## uint_z(_f); })
  39. #define float2int64_z(f) ({ float _f = f; pico_default_asm_volatile("" : FREG (_f)); float2 ## int64_z(_f); })
  40. #define float2uint64_z(f) ({ float _f = f; pico_default_asm_volatile("" : FREG (_f)); float2 ## uint64_z(_f); })
  41. #define int2float(i) ({ int32_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); int2 ## float(_i); })
  42. #define uint2float(i) ({ uint32_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); uint2 ## float(_i); })
  43. #define int642float(i) ({ int64_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); int642 ## float(_i); })
  44. #define uint642float(i) ({ uint64_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); uint642 ## float(_i); })
  45. #endif
  46. #if 1 && LIB_PICO_FLOAT_VFP
  47. // prevet the compiler from eliding the calculations
  48. #undef float2int_z
  49. #undef float2uint_z
  50. #undef int2float
  51. #undef uint2float
  52. #endif
  53. int test() {
  54. int rc = 0;
  55. #if LIB_PICO_FLOAT_PICO_DCP
  56. printf(">>> Using DCP\n");
  57. #endif
  58. #if LIB_PICO_FLOAT_PICO_VFP
  59. printf(">>> Using VFP\n");
  60. #endif
  61. printf("int2float\n");
  62. test_checkf(int2float(0), 0.0f, "int2float1");
  63. test_checkf(int2float(-1), -1.0f, "int2float2");
  64. test_checkf(int2float(1), 1.0f, "int2float3");
  65. test_checkf(int2float(INT32_MAX), 2147483647.0f, "int2float4");
  66. test_checkf(int2float(INT32_MIN), -2147483648.0f, "int2float5");
  67. // check rounding
  68. test_checkf(int2float(2147483391), 2147483392.0f, "int2float6");
  69. test_checkf(int2float(2147483456), 2147483392.0f, "int2float7");
  70. test_checkf(int2float(2147483457), 2147483520.0f, "int2float8");
  71. test_checkf(int2float(2147483483), 2147483520.0f, "int2float9");
  72. test_checkf(int2float(2147483584), 2147483648.0f, "int2float10");
  73. printf("uint2float\n");
  74. test_checkf(uint2float(0), 0.0f, "uint2float1");
  75. test_checkf(uint2float(1), 1.0f, "uint2float2");
  76. test_checkf(uint2float(INT32_MAX), 2147483647.0f, "uint2float3");
  77. // todo test correct rounding around maximum precision
  78. test_checkf(uint2float(UINT32_MAX), 4294967295.0f, "uint2float4");
  79. printf("int642float\n");
  80. test_checkf(int642float(0), 0.0f, "int642float1");
  81. test_checkf(int642float(-1), -1.0f, "int642float2");
  82. test_checkf(int642float(1), 1.0f, "int642float3");
  83. test_checkf(int642float(INT32_MAX-1), 2147483646.0f, "int642float4"); // note equality is within 1ulp
  84. test_checkf(int642float(INT32_MAX), 2147483647.0f, "int642float5"); // note equality is within 1ulp
  85. test_checkf(int642float(INT32_MAX+1ll), 2147483648.0f, "int642float6");
  86. test_checkf(int642float(INT32_MIN-1ll), -2147483649.0f, "int642float7"); // note equality is within 1ulp
  87. test_checkf(int642float(INT32_MIN), -2147483648.0f, "int642float8");
  88. test_checkf(int642float(INT32_MIN+1ll), -2147483647.0f, "int642float9"); // note equality is within 1ulp
  89. // todo test correct rounding around maximum precision
  90. test_checkf(int642float(INT64_MAX), 9223372036854775807.0f, "int642float10");
  91. test_checkf(int642float(INT64_MIN), -9223372036854775808.0f, "int642float11");
  92. printf("uint642float\n");
  93. test_checkf(uint642float(0), 0.0f, "uint642float1");
  94. test_checkf(uint642float(1), 1.0f, "uint642float2");
  95. test_checkf(uint642float(INT32_MAX-1), 2147483646.0f, "uint642float3"); // note equality is within 1ulp
  96. test_checkf(uint642float(INT32_MAX), 2147483647.0f, "uint642float4"); // note equality is within 1ulp
  97. test_checkf(uint642float(INT32_MAX+1ll), 2147483648.0f, "uint642float5");
  98. test_checkf(uint642float(INT64_MAX), 9223372036854775807.0f, "uint642float6");
  99. // todo test correct rounding around maximum precision
  100. test_checkf(uint642float(UINT64_MAX), 18446744073709551615.0f, "uint642float7");
  101. union {
  102. uint32_t u;
  103. float f;
  104. } u32f;
  105. #if !(LIB_PICO_FLOAT_COMPILER || defined(__riscv))
  106. printf("fix2float\n");
  107. // todo test correct rounding around maximum precision
  108. test_checkf(fix2float(-3, 1), -1.5f, "fix2float1");
  109. test_checkf(fix2float(-3, 1), -1.5f, "fix2float2");
  110. test_checkf(fix2float(-3, -4), -48.0f, "fix2float3");
  111. printf("ufix2float\n");
  112. // todo test correct rounding around maximum precision
  113. test_checkf(ufix2float(0xa0000000, 30), 2.5f, "ufix2float1");
  114. test_checkf(ufix2float(3, -4), 48.0f, "ufix2float2");
  115. printf("fix642float\n");
  116. // todo test correct rounding around maximum precision
  117. test_checkf(fix642float(-0xa000000000ll, 38), -2.5f, "fix6422float1");
  118. test_checkf(fix642float(-3, -34), -51539607552.0f, "fix642float2");
  119. printf("ufix642float\n");
  120. // todo test correct rounding around maximum precision
  121. test_checkf(ufix642float(0xa000000000ll, 38), 2.5f, "ufix642float1");
  122. test_checkf(ufix642float(3, -34), 51539607552.0f, "fix64float2");
  123. test_checkf(fix2float_8(128), 0.5f, "fix2float_8_1");
  124. test_checkf(fix2float_8(-128), -0.5f, "fix2float_8_2");
  125. test_checkf(fix2float_16(8192), 0.125f, "fix2float_8_3");
  126. test_checkf(fix2float_16(-8192), -0.125f, "fix2float_8_4");
  127. test_checkf(fix2float_24(3<<23), 1.5f, "fix2float_8_5");
  128. test_checkf(fix2float_24(-(3<<23)), -1.5f, "fix2float_8_6");
  129. printf("float2fix\n");
  130. test_checki(float2fix(-0.5f, 8), -0x80, "float2fix0");
  131. test_checki(float2fix(3.5f, 8), 0x380, "float2fix1");
  132. test_checki(float2fix(-3.5f, 8), -0x380, "float2fix2");
  133. test_checki(float2fix(32768.0f, 16), INT32_MAX, "float2fix3");
  134. test_checki(float2fix(65536.0f, 16), INT32_MAX, "float2fix4");
  135. test_checki(float2fix(-65536.0f, 16), INT32_MIN, "float2fix4b");
  136. test_checki(float2fix(INFINITY, 16), INT32_MAX, "float2fix5");
  137. test_checki(float2fix(-INFINITY, 16), INT32_MIN, "float2fix5b");
  138. test_checki(float2fix(3.24999f, 2), 12, "float2fix6");
  139. test_checki(float2fix(3.25f, 2), 13, "float2fix7");
  140. test_checki(float2fix(-3.24999f, 2), -13, "float2fix8");
  141. test_checki(float2fix(-3.25f, 2), -13, "float2fix9");
  142. test_checki(float2fix(-0.75f, 1), -2, "float2fix10");
  143. test_checki(float2fix(-3.0f, -1), -2, "float2fix11"); // not very useful
  144. u32f.u = 0x7f012345;
  145. test_checki(float2fix(u32f.f, 1), INT32_MAX, "float2fix12");
  146. u32f.u = 0xff012345;
  147. test_checki(float2fix(u32f.f, 1), INT32_MIN, "float2fix13");
  148. printf("float2ufix\n");
  149. test_checku(float2ufix(3.5f, 8), 0x380, "float2ufix1");
  150. test_checku(float2ufix(-3.5f, 8), 0, "float2ufix2");
  151. test_checku(float2ufix(32768.0f, 16), 32768 << 16, "float2ufix3");
  152. test_checku(float2ufix(65536.0f, 16), UINT32_MAX, "float2ufix4");
  153. test_checku(float2ufix(INFINITY, 16), UINT32_MAX, "float2ufix5");
  154. test_checku(float2ufix(3.24999f, 2), 12, "float2ufix6");
  155. test_checku(float2ufix(3.25f, 2), 13, "float2ufix7");
  156. test_checku(float2ufix(3.0f, -1), 1, "float2ufix8"); // not very useful
  157. printf("float2fix64\n");
  158. test_checki64(float2fix64(3.5f, 8), 0x380, "float2fix641");
  159. test_checki64(float2fix64(-3.5f, 8), -0x380, "float2fix642");
  160. test_checki64(float2fix64(32768.0f, 16), 32768ll << 16, "float2fix643");
  161. test_checki64(float2fix64(65536.0f, 16), 65536ll << 16, "float2fix644");
  162. test_checki64(float2fix64(2147483648.0f, 16), 2147483648ll << 16, "float2ufix644b");
  163. test_checki64(float2fix64(65536.0f * 65536.0f * 32768.0f, 16), INT64_MAX, "float2fix644c");
  164. test_checki64(float2fix64(INFINITY, 16), INT64_MAX, "float2fix645");
  165. test_checki64(float2fix64(3.24999f, 2), 12, "float2fix646");
  166. test_checki64(float2fix64(3.25f, 2), 13, "float2fix647");
  167. test_checki64(float2fix64(-3.24999f, 2), -13, "float2fix648");
  168. test_checki64(float2fix64(-3.25f, 2), -13, "float2fix649");
  169. test_checki64(float2fix64(-3.0f, -1), -2, "float2fix6410"); // not very useful
  170. printf("float2ufix64\n");
  171. test_checku64(float2ufix64(3.5f, 8), 0x380, "float2ufix641");
  172. test_checku64(float2ufix64(-3.5f, 8), 0, "float2ufix642");
  173. test_checku64(float2ufix64(32768.0f, 16), 32768ull << 16, "float2ufix643");
  174. test_checku64(float2ufix64(65536.0f, 16), 65536ull << 16, "float2ufix644");
  175. test_checku64(float2ufix64(2147483648.0f, 16), 2147483648ull << 16, "float2ufix644b");
  176. test_checku64(float2ufix64(INFINITY, 16), UINT64_MAX, "float2ufix645");
  177. test_checku64(float2ufix64(3.24999f, 2), 12, "float2ufix646");
  178. test_checku64(float2ufix64(3.25f, 2), 13, "float2ufix647");
  179. test_checku64(float2ufix64(3.0f, -1), 1, "float2ufix648"); // not very useful
  180. printf("float2fix_z\n");
  181. test_checki(float2fix_z(3.5f, 8), 0x380, "float2fix_z1");
  182. test_checki(float2fix_z(-3.5f, 8), -0x380, "float2fix_z2");
  183. test_checki(float2fix_z(32768.0f, 16), INT32_MAX, "float2fix_z3");
  184. test_checki(float2fix_z(65536.0f, 16), INT32_MAX, "float2fix_z4");
  185. test_checki(float2fix_z(INFINITY, 16), INT32_MAX, "float2fix_z5");
  186. test_checki(float2fix_z(-INFINITY, 16), INT32_MIN, "float2fix_z5b");
  187. test_checki(float2fix_z(3.24999f, 2), 12, "float2fix_z6");
  188. test_checki(float2fix_z(3.25f, 2), 13, "float2fix_z7");
  189. test_checki(float2fix_z(-3.24999f, 2), -12, "float2fix_z8");
  190. test_checki(float2fix_z(-3.25f, 2), -13, "float2fix_z9");
  191. test_checki(float2fix_z(-0.75f, 1), -1, "float2fix_z10");
  192. test_checki(float2fix_z(-3.0f, -1), -1, "float2fix_z11"); // not very useful
  193. u32f.u = 0x7f012345;
  194. test_checki(float2fix_z(u32f.f, 1), INT32_MAX, "float2fix_z12");
  195. u32f.u = 0xff012345;
  196. test_checki(float2fix_z(u32f.f, 1), INT32_MIN, "float2fix_z13");
  197. printf("float2ufix_z\n");
  198. test_checku(float2ufix_z(3.5f, 8), 0x380, "float2ufix_z1");
  199. test_checku(float2ufix_z(-3.5f, 8), 0, "float2ufix_z2");
  200. test_checku(float2ufix_z(32768.0f, 16), 32768 << 16, "float2ufix_z3");
  201. test_checku(float2ufix_z(65536.0f, 16), UINT32_MAX, "float2ufix_z4");
  202. test_checku(float2ufix_z(INFINITY, 16), UINT32_MAX, "float2ufix_z5");
  203. test_checku(float2ufix_z(3.24999f, 2), 12, "float2ufix_z6");
  204. test_checku(float2ufix_z(3.25f, 2), 13, "float2ufix_z7");
  205. test_checku(float2ufix_z(3.0f, -1), 1, "float2ufix_z8"); // not very useful
  206. u32f.u = 0x7f012345;
  207. test_checku(float2ufix_z(u32f.f, 1), UINT32_MAX, "float2fix_z9");
  208. u32f.u = 0xff012345;
  209. test_checku(float2ufix_z(u32f.f, 1), 0, "float2fix_z10");
  210. printf("float2fix64_z\n");
  211. test_checki64(float2fix64_z(3.5f, 8), 0x380, "float2fix64_z1");
  212. test_checki64(float2fix64_z(-3.5f, 8), -0x380, "float2fix64_z2");
  213. test_checki64(float2fix64_z(32768.0f, 16), 32768ll << 16, "float2fix64_z3");
  214. test_checki64(float2fix64_z(65536.0f, 16), 65536ll << 16, "float2fix64_z4");
  215. test_checki64(float2fix64_z(65536.0f * 65536.0f * 32768.0f, 16), INT64_MAX, "float2fix64_z4b");
  216. test_checki64(float2fix64_z(INFINITY, 16), INT64_MAX, "float2fix64_z5");
  217. test_checki64(float2fix64_z(3.24999f, 2), 12, "float2fix64_z6");
  218. test_checki64(float2fix64_z(3.25f, 2), 13, "float2fix64_z7");
  219. test_checki64(float2fix64_z(-3.24999f, 2), -12, "float2fix64_z8");
  220. test_checki64(float2fix64_z(-3.25f, 2), -13, "float2fix64_z9");
  221. test_checki64(float2fix64_z(-3.0f, -1), -1, "float2fix64_z10"); // not very useful
  222. printf("float2ufix64_z\n");
  223. test_checku64(float2ufix64_z(3.5f, 8), 0x380, "float2ufix64_z1");
  224. test_checku64(float2ufix64_z(-3.5f, 8), 0, "float2ufix64_z2");
  225. test_checku64(float2ufix64_z(32768.0f, 16), 32768ll << 16, "float2ufix64_z3");
  226. test_checku64(float2ufix64_z(65536.0f, 16), 65536ll << 16, "float2ufix64_z4");
  227. test_checki64(float2ufix64_z(65536.0f * 65536.0f * 65536.0f, 16), UINT64_MAX, "float2fix64_z4b");
  228. test_checku64(float2ufix64_z(INFINITY, 16), UINT64_MAX, "float2ufix64_z5");
  229. test_checku64(float2ufix64_z(3.24999f, 2), 12, "float2ufix64_z6");
  230. test_checku64(float2ufix64_z(3.25f, 2), 13, "float2ufix64_z7");
  231. test_checki64(float2ufix64_z(3.0f, -1), 1, "float2fuix64_z8"); // not very useful
  232. printf("float2int\n");
  233. test_checki(float2int(0.0f), 0, "float2int1");
  234. test_checki(float2int(0.25f), 0, "float2int1b");
  235. test_checki(float2int(0.5f), 0, "float2int2");
  236. test_checki(float2int(0.75f), 0, "float2int2b");
  237. test_checki(float2int(1.0f), 1, "float2int3");
  238. test_checki(float2int(-10.0f), -10, "float2int3a");
  239. test_checki(float2int(-0.0f), 0, "float2int3b");
  240. test_checki(float2int(-0.25f), -1, "float2int4");
  241. test_checki(float2int(-0.5f), -1, "float2int4b");
  242. test_checki(float2int(-0.75f), -1, "float2int5");
  243. test_checki(float2int(-1.0f), -1, "float2int5b");
  244. // todo test correct rounding around maximum precision
  245. test_checki(float2int(2147483647.0f), INT32_MAX, "float2int6");
  246. test_checki(float2int(21474836470.0f), INT32_MAX, "float2int7");
  247. test_checki(float2int(-2147483648.0f), INT32_MIN, "float2int8");
  248. test_checki(float2int(-21474836480.0f), INT32_MIN, "float2int9");
  249. test_checki(float2int(-2.5f), -3, "float2int10");
  250. test_checki(float2int(-2.4f), -3, "float2int11");
  251. printf("float2uint\n");
  252. test_checku(float2uint(0.0f), 0, "float2uint1");
  253. test_checku(float2uint(0.25f), 0, "float2uint2");
  254. test_checku(float2uint(0.5f), 0, "float2uint3");
  255. test_checku(float2uint(0.75f), 0, "float2uint4");
  256. test_checku(float2uint(1.0f), 1, "float2uint5");
  257. test_checku(float2uint(2147483647.0f), INT32_MAX+1u, "float2uint6"); // note loss of precision
  258. test_checku(float2uint(2147483648.0f), INT32_MAX+1u, "float2uint7");
  259. test_checku(float2uint(4294967294.5f), UINT32_MAX, "float2uint8"); // note loss of precision
  260. test_checku(float2uint(4294967295.0f), UINT32_MAX, "float2uint9");
  261. test_checku(float2uint(42949672950.0f), UINT32_MAX, "float2uint10");
  262. printf("float2int64\n");
  263. test_checki64(float2int64(0.0f), 0, "float2int641");
  264. test_checki64(float2int64(0.25f), 0, "float2int641b");
  265. test_checki64(float2int64(0.5f), 0, "float2int642");
  266. test_checki64(float2int64(0.75f), 0, "float2int642b");
  267. test_checki64(float2int64(1.0f), 1, "float2int643");
  268. test_checki64(float2int64(-10.0f), -10, "float2int643a");
  269. test_checki64(float2int64(-0.0f), 0, "float2int643b");
  270. test_checki64(float2int64(-0.25f), -1, "float2int644");
  271. test_checki64(float2int64(-0.5f), -1, "float2int644b");
  272. test_checki64(float2int64(-0.75f), -1, "float2int645");
  273. test_checki64(float2int64(-1.0f), -1, "float2int645b");
  274. // todo test correct rounding around maximum precision
  275. test_checki64(float2int64(2147483647.0f), INT32_MAX+1ll, "float2int646");
  276. test_checki64(float2int64(21474836470.0f), 21474836480ll, "float2int647"); // note loss of precision
  277. test_checki64(float2int64(-2147483648.0f), INT32_MIN, "float2int648");
  278. test_checki64(float2int64(-21474836480.0f), -21474836480ll, "float2int649");
  279. test_checki64(float2int64(-2.5f), -3, "float2int6410");
  280. test_checki64(float2int64(-2.4f), -3, "float2int6411");
  281. printf("float2uint64\n");
  282. test_checku64(float2uint64(0.0f), 0, "float2uint641");
  283. test_checku64(float2uint64(0.25f), 0, "float2uint642");
  284. test_checku64(float2uint64(0.5f), 0, "float2uint643");
  285. test_checku64(float2uint64(0.75f), 0, "float2uint644");
  286. test_checku64(float2uint64(1.0f), 1, "float2uint645");
  287. test_checku64(float2uint64(2147483647.0f), INT32_MAX+1u, "float2uint646"); // note loss of precision
  288. test_checku64(float2uint64(2147483648.0f), INT32_MAX+1u, "float2uint647");
  289. test_checku64(float2uint64(4294967294.5f), 4294967296ull, "float2uint648"); // note loss of precision
  290. test_checku64(float2uint64(4294967295.0f), 4294967296ull, "float2uint649"); // note loss of precision
  291. test_checku64(float2uint64(42949672950.0f), 42949672960ull, "float2uint6410"); // note loss of precision
  292. #endif
  293. // // These methods round towards 0.
  294. printf("float2int_z\n");
  295. test_checki(float2int_z(0.0f), 0, "float2int_z1");
  296. test_checki(float2int_z(0.25f), 0, "float2int_z1b");
  297. test_checki(float2int_z(0.5f), 0, "float2int_z2");
  298. test_checki(float2int_z(0.75f), 0, "float2int_z2b");
  299. test_checki(float2int_z(1.0f), 1, "float2int_z3");
  300. test_checki(float2int_z(-10.0f), -10, "float2int_z3a");
  301. test_checki(float2int_z(-0.0f), 0, "float2int_z3b");
  302. test_checki(float2int_z(-0.25f), 0, "float2int_z4");
  303. test_checki(float2int_z(-0.5f), 0, "float2int_z4b");
  304. test_checki(float2int_z(-0.75f), 0, "float2int_z5");
  305. test_checki(float2int_z(-1.0f), -1, "float2int_z5b");
  306. // todo test correct rounding around maximum precision
  307. test_checki(float2int_z(2147483647.0f), INT32_MAX, "float2int_z6");
  308. test_checki(float2int_z(21474836470.0f), INT32_MAX, "float2int_z7");
  309. test_checki(float2int_z(-2147483648.0f), INT32_MIN, "float2int_z8");
  310. test_checki(float2int_z(-21474836480.0f), INT32_MIN, "float2int_z9");
  311. test_checki(float2int_z(-2.5f), -2, "float2int_z10");
  312. test_checki(float2int_z(-2.4f), -2, "float2int_z11");
  313. printf("float2int64_z\n");
  314. test_checki64(float2int64_z(0.0f), 0, "float2int64_z1");
  315. test_checki64(float2int64_z(0.25f), 0, "float2int64_z1b");
  316. test_checki64(float2int64_z(0.5f), 0, "float2int64_z2");
  317. test_checki64(float2int64_z(0.75f), 0, "float2int64_z2b");
  318. test_checki64(float2int64_z(1.0f), 1, "float2int64_z3");
  319. test_checki64(float2int64_z(-10.0f), -10, "float2int64_z3a");
  320. test_checki64(float2int64_z(-0.0f), 0, "float2int64_z3b");
  321. test_checki64(float2int64_z(-0.25f), 0, "float2int64_z4");
  322. test_checki64(float2int64_z(-0.5f), 0, "float2int64_z4b");
  323. test_checki64(float2int64_z(-0.75f), 0, "float2int64_z5");
  324. test_checki64(float2int64_z(-1.0f), -1, "float2int64_z5b");
  325. test_checki64(float2int64_z(2147483647.0f), 2147483648ll, "float2int64_z6"); // note loss of precision
  326. test_checki64(float2int64_z(21474836470.0f), 21474836480ll, "float2int64_z7"); // note loss of precision
  327. test_checki64(float2int64_z(-2147483648.0f), INT32_MIN, "float2int64_z8");
  328. test_checki64(float2int64_z(-21474836480.0f), -21474836480ll, "float2int64_z9");
  329. test_checki64(float2int64_z(-2.5f), -2, "float2int64_z10");
  330. test_checki64(float2int64_z(-2.4f), -2, "float2int64_z11");
  331. printf("float2uint_z\n");
  332. test_checku(float2uint_z(0.0f), 0, "float2uint_z1");
  333. test_checku(float2uint_z(0.25f), 0, "float2uint_z2");
  334. test_checku(float2uint_z(0.5f), 0, "float2uint_z3");
  335. test_checku(float2uint_z(0.75f), 0, "float2uint_z4");
  336. test_checku(float2uint_z(1.0f), 1, "float2uint_z5");
  337. test_checku(float2uint_z(2147483647.0f), INT32_MAX+1u, "float2uint_z6"); // note loss of precision
  338. test_checku(float2uint_z(2147483648.0f), INT32_MAX+1u, "float2uint_z7");
  339. // todo test correct rounding around maximum precision
  340. test_checku(float2uint_z(4294967294.5f), UINT32_MAX, "float2uint_z8"); // note loss of precision
  341. test_checku(float2uint_z(4294967295.0f), UINT32_MAX, "float2uint_z9");
  342. test_checku(float2uint_z(42949672950.0f), UINT32_MAX, "float2uint_z10");
  343. printf("float2uint64_z\n");
  344. test_checku64(float2uint64_z(0.0f), 0, "float2uint64_z1");
  345. test_checku64(float2uint64_z(0.25f), 0, "float2uint64_z2");
  346. test_checku64(float2uint64_z(0.5f), 0, "float2uint64_z3");
  347. test_checku64(float2uint64_z(0.75f), 0, "float2uint64_z4");
  348. test_checku64(float2uint64_z(1.0f), 1, "float2uint64_z5");
  349. test_checku64(float2uint64_z(2147483647.0f), INT32_MAX+1u, "float2uint64_z6"); // note loss of precision
  350. test_checku64(float2uint64_z(2147483648.0f), INT32_MAX+1u, "float2uint64_z7");
  351. test_checku64(float2uint64_z(4294967294.5f), 4294967296ull, "float2uint64_z8"); // note loss of precision
  352. test_checku64(float2uint64_z(4294967295.0f), 4294967296ull, "float2uint64_z9"); // note loss of precision
  353. test_checku64(float2uint64_z(42949672950.0f), 42949672960ull, "float2uint64_z10"); // note loss of precision
  354. // float exp10f(float x);
  355. // void sincosf(float x, float *sinx, float *cosx);
  356. // float powintf(float x, int y);
  357. return rc;
  358. }
  359. int main() {
  360. stdio_init_all();
  361. int rc = test();
  362. if (rc) {
  363. printf("FAILED\n");
  364. } else {
  365. printf("PASSED\n");
  366. }
  367. }