custom_double_funcs_test.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #include <stdio.h>
  2. #include "pico/stdlib.h"
  3. #include "pico/double.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_checkd(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_DOUBLE_COMPILER || defined(__riscv))
  20. static inline double fix2double_8(int32_t m) { return fix2double(m, 8); }
  21. static inline double fix2double_12(int32_t m) { return fix2double(m, 12); }
  22. static inline double fix2double_16(int32_t m) { return fix2double(m, 16); }
  23. static inline double fix2double_24(int32_t m) { return fix2double(m, 24); }
  24. static inline double fix2double_28(int32_t m) { return fix2double(m, 28); }
  25. static inline double fix2double_32(int32_t m) { return fix2double(m, 32); }
  26. static inline double ufix2double_12(int32_t m) { return ufix2double(m, 12); }
  27. static inline double double2fix_12(int32_t m) { return double2fix(m, 12); }
  28. static inline double double2ufix_12(int32_t m) { return double2ufix(m, 12); }
  29. #endif
  30. #if 1 && (LIB_PICO_DOUBLE_COMPILER || defined(__riscv))
  31. #define double2int_z(f) ({ double _d = f; pico_default_asm_volatile("" : "+r" (_d)); double2 ## int_z(_d); })
  32. #define double2uint_z(f) ({ double _d = f; pico_default_asm_volatile("" : "+r" (_d)); double2 ## uint_z(_d); })
  33. #define double2int64_z(f) ({ double _d = f; pico_default_asm_volatile("" : "+r" (_d)); double2 ## int64_z(_d); })
  34. #define double2uint64_z(f) ({ double _d = f; pico_default_asm_volatile("" : "+r" (_d)); double2 ## uint64_z(_d); })
  35. #define int2double(i) ({ int32_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); int2 ## double(_i); })
  36. #define uint2double(i) ({ uint32_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); uint2 ## double(_i); })
  37. #define int642double(i) ({ int64_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); int642 ## double(_i); })
  38. #define uint642double(i) ({ uint64_t _i = i; pico_default_asm_volatile("" : "+r" (_i)); uint642 ## double(_i); })
  39. #endif
  40. int test() {
  41. int rc = 0;
  42. #if LIB_PICO_DOUBLE_PICO
  43. printf(">>> Using PICO\n");
  44. #endif
  45. printf("int2double\n");
  46. test_checkd(int2double(0), 0.0, "int2double1");
  47. test_checkd(int2double(-1), -1.0, "int2double2");
  48. test_checkd(int2double(1), 1.0, "int2double3");
  49. test_checkd(int2double(INT32_MAX), 2147483647.0, "int2double4");
  50. test_checkd(int2double(INT32_MIN), -2147483648.0, "int2double5");
  51. // these have rounding behavior on float but not double
  52. test_checkd(int2double(2147483391), 2147483391.0, "int2double6");
  53. test_checkd(int2double(2147483391), 2147483391.0, "int2double7");
  54. test_checkd(int2double(2147483457), 2147483457.0, "int2double8");
  55. test_checkd(int2double(2147483483), 2147483483.0, "int2double9");
  56. test_checkd(int2double(2147483584), 2147483584.0, "int2double10");
  57. printf("uint2double\n");
  58. test_checkd(uint2double(0), 0.0, "uint2double1");
  59. test_checkd(uint2double(1), 1.0, "uint2double2");
  60. test_checkd(uint2double(INT32_MAX), 2147483647.0, "uint2double3");
  61. // todo test correct rounding around maximum precision
  62. test_checkd(uint2double(UINT32_MAX), 4294967295.0, "uint2double4");
  63. printf("int642double\n");
  64. test_checkd(int642double(0), 0.0, "int642double1");
  65. test_checkd(int642double(-1), -1.0, "int642double2");
  66. test_checkd(int642double(1), 1.0, "int642double3");
  67. test_checkd(int642double(INT32_MAX-1), 2147483646.0, "int642double4");
  68. test_checkd(int642double(INT32_MAX), 2147483647.0, "int642double5");
  69. test_checkd(int642double(INT32_MAX+1ll), 2147483648.0, "int642double6");
  70. test_checkd(int642double(INT32_MIN-1ll), -2147483649.0, "int642double7");
  71. test_checkd(int642double(INT32_MIN), -2147483648.0, "int642double8");
  72. test_checkd(int642double(INT32_MIN+1ll), -2147483647.0, "int642double9");
  73. // todo test correct rounding around maximum precision
  74. test_checkd(int642double(INT64_MAX), 9223372036854775807.0, "int642double10");
  75. test_checkd(int642double(INT64_MIN), -9223372036854775808.0, "int642doubl11e");
  76. printf("uint642double\n");
  77. test_checkd(uint642double(0), 0.0, "uint642double1");
  78. test_checkd(uint642double(1), 1.0, "uint642double2");
  79. test_checkd(uint642double(INT32_MAX-1), 2147483646.0, "uint642double3");
  80. test_checkd(uint642double(INT32_MAX), 2147483647.0, "uint642double4");
  81. test_checkd(uint642double(INT32_MAX+1ll), 2147483648.0, "uint642double5");
  82. test_checkd(uint642double(INT64_MAX), 9223372036854775807.0, "uint642double6");
  83. // todo test correct rounding around maximum precision
  84. test_checkd(uint642double(UINT64_MAX), 18446744073709551615.0, "uint642double7");
  85. union {
  86. uint64_t u;
  87. double d;
  88. } u64d;
  89. #if !(LIB_PICO_DOUBLE_COMPILER || defined(__riscv))
  90. printf("fix2double\n");
  91. // todo test correct rounding around maximum precision
  92. test_checkd(fix2double(-3, 1), -1.5, "fix2double1");
  93. test_checkd(fix2double(-3, 1), -1.5, "fix2double2");
  94. test_checkd(fix2double(-3, -4), -48.0, "fix2double3");
  95. printf("ufix2double\n");
  96. // todo test correct rounding around maximum precision
  97. test_checkd(ufix2double(0xa0000000, 30), 2.5, "ufix2double1");
  98. test_checkd(ufix2double(3, -4), 48.0, "ufix2double2");
  99. printf("fix64double\n");
  100. // todo test correct rounding around maximum precision
  101. test_checkd(fix642double(-0xa000000000ll, 38), -2.5, "fix642double1");
  102. test_checkd(fix642double(-3, -34), -51539607552.0, "fix642double2");
  103. printf("ufix642double\n");
  104. // todo test correct rounding around maximum precision
  105. test_checkd(ufix642double(0xa000000000ll, 38), 2.5, "ufix642double1");
  106. test_checkd(ufix642double(3, -34), 51539607552.0, "fix64double2");
  107. test_checkd(fix2double_8(128), 0.5, "fix2double_8_1");
  108. test_checkd(fix2double_8(-128), -0.5, "fix2double_8_2");
  109. test_checkd(fix2double_16(8192), 0.125, "fix2double_8_3");
  110. test_checkd(fix2double_16(-8192), -0.125, "fix2double_8_4");
  111. test_checkd(fix2double_24(3<<23), 1.5, "fix2double_8_5");
  112. test_checkd(fix2double_24(-(3<<23)), -1.5, "fix2double_8_6");
  113. printf("double2fix\n");
  114. test_checki(double2fix(-0.5, 8), -0x80, "double2fix0");
  115. test_checki(double2fix(3.5, 8), 0x380, "double2fix1");
  116. test_checki(double2fix(-3.5, 8), -0x380, "double2fix2");
  117. test_checki(double2fix(32768.0, 16), INT32_MAX, "double2fix3");
  118. test_checki(double2fix(65536.0, 16), INT32_MAX, "double2fix4");
  119. test_checki(double2fix(-65536.0, 16), INT32_MIN, "double2fix4b");
  120. test_checki(double2fix(INFINITY, 16), INT32_MAX, "double2fix5");
  121. test_checki(double2fix(-INFINITY, 16), INT32_MIN, "double2fix5b");
  122. test_checki(double2fix(INFINITY, -16), INT32_MAX, "double2fix5c");
  123. test_checki(double2fix(-INFINITY, -16), INT32_MIN, "double2fix5d");
  124. test_checki(double2fix(3.24999, 2), 12, "double2fix6");
  125. test_checki(double2fix(3.25, 2), 13, "double2fix7");
  126. test_checki(double2fix(-3.24999, 2), -13, "double2fix8");
  127. test_checki(double2fix(-3.25, 2), -13, "double2fix9");
  128. test_checki(double2fix(-0.75, 1), -2, "double2fix10");
  129. test_checki(double2fix(-3.0, -1), -2, "double2fix11"); // not very useful
  130. test_checki(double2fix(0.0, 16), 0, "double2fix12");
  131. test_checki(double2fix(-0.0, 16), 0, "double2fix13");
  132. test_checki(double2fix(0.0, -16), 0, "double2fix14");
  133. test_checki(double2fix(-0.0, -16), 0, "double2fix15");
  134. printf("double2ufix\n");
  135. test_checku(double2ufix(3.5, 8), 0x380, "double2ufix1");
  136. test_checku(double2ufix(-3.5, 8), 0, "double2ufix2");
  137. test_checku(double2ufix(32768.0, 16), 32768 << 16, "double2ufix3");
  138. test_checku(double2ufix(65536.0, 16), UINT32_MAX, "double2ufix4");
  139. test_checku(double2ufix(INFINITY, 16), UINT32_MAX, "double2ufix5");
  140. test_checku(double2ufix(-INFINITY, 16), 0, "double2ufix5b");
  141. test_checku(double2ufix(INFINITY, -16), UINT32_MAX, "double2ufix5c");
  142. test_checku(double2ufix(-INFINITY, -16), 0, "double2ufix5d");
  143. test_checku(double2ufix(3.24999, 2), 12, "double2ufix6");
  144. test_checku(double2ufix(3.25, 2), 13, "double2ufix7");
  145. test_checku(double2ufix(3.0, -1), 1, "double2ufix8"); // not very useful
  146. test_checki(double2ufix(0.0, 16), 0, "double2ufix12");
  147. test_checki(double2ufix(-0.0, 16), 0, "double2fix13");
  148. test_checki(double2ufix(0.0, -16), 0, "double2ufix14");
  149. test_checki(double2ufix(-0.0, -16), 0, "double2fix15");
  150. printf("double2fix64\n");
  151. test_checki64(double2fix64(3.5, 8), 0x380, "double2fix641");
  152. test_checki64(double2fix64(-3.5, 8), -0x380, "double2fix642");
  153. test_checki64(double2fix64(32768.0, 16), 32768ll << 16, "double2fix643");
  154. test_checki64(double2fix64(65536.0, 16), 65536ll << 16, "double2fix644");
  155. test_checki64(double2fix64(2147483648.0, 16), 2147483648ll << 16, "double2ufix644b");
  156. test_checki64(double2fix64(65536.0 * 65536.0 * 32768.0, 16), INT64_MAX, "double2fix644c");
  157. test_checki64(double2fix64(INFINITY, 16), INT64_MAX, "double2fix645");
  158. test_checki64(double2fix64(-INFINITY, 16), INT64_MIN, "double2fix645b");
  159. test_checki64(double2fix64(INFINITY, -16), INT64_MAX, "double2fix645c");
  160. test_checki64(double2fix64(-INFINITY, -16), INT64_MIN, "double2fix645d");
  161. test_checki64(double2fix64(3.24999, 2), 12, "double2fix646");
  162. test_checki64(double2fix64(3.25, 2), 13, "double2fix647");
  163. test_checki64(double2fix64(-3.24999, 2), -13, "double2fix648");
  164. test_checki64(double2fix64(-3.25, 2), -13, "double2fix649");
  165. test_checki64(double2fix64(-3.0, -1), -2, "double2fix6410"); // not very useful
  166. test_checki64(double2fix64(2147483648.0 * 2147483648.0, 16), INT64_MAX, "double2ufix6411");
  167. test_checki64(double2fix64(0.0, 16), 0, "double2fix6412");
  168. test_checki64(double2fix64(-0.0, 16), 0, "double2fix6413");
  169. test_checki64(double2fix64(0.0, -16), 0, "double2fix6412b");
  170. test_checki64(double2fix64(-0.0, -16), 0, "double2fix6413b");
  171. test_checki64(double2fix64(-3.25, 40), -13ll * (1ll << 38), "double2fix6414");
  172. u64d.u = 0xc00a000000000001;
  173. test_checki64(double2fix64(u64d.d, 40), -13ll * (1ll << 38) - 1ll, "double2fix6414b");
  174. u64d.u = 0xc00a000080000001;
  175. test_checki64(double2fix64(u64d.d, 20), -13ll * (1ll << 18) - 2ll, "double2fix6415c");
  176. u64d.u = 0xc00a000080000000;
  177. test_checki64(double2fix64(u64d.d, 20), -13ll * (1ll << 18) - 1ll, "double2fix6415d");
  178. u64d.u = 0xc00a000000000001;
  179. test_checki64(double2fix64(u64d.d, 20), -13ll * (1ll << 18) - 1ll, "double2fix6415e");
  180. u64d.u = 0xc00a000000000000;
  181. test_checki64(double2fix64(u64d.d, 20), -13ll * (1ll << 18), "double2fix6415g");
  182. u64d.u = 0xc00a000080000001;
  183. test_checki64(double2fix64(u64d.d, 19), -13ll * (1ll << 17) - 1ll, "double2fix6415h");
  184. u64d.u = 0xc00a000080000000;
  185. test_checki64(double2fix64(u64d.d, 19), -13ll * (1ll << 17) - 1ll, "double2fix6415i");
  186. u64d.u = 0xc00a000000000001;
  187. test_checki64(double2fix64(u64d.d, 19), -13ll * (1ll << 17) - 1ll, "double2fix6415j");
  188. u64d.u = 0xc00a000000000000;
  189. test_checki64(double2fix64(u64d.d, 19), -13ll * (1ll << 17), "double2fix6415k");
  190. printf("double2ufix64\n");
  191. test_checku64(double2ufix64(3.5, 8), 0x380, "double2ufix641");
  192. test_checku64(double2ufix64(-3.5, 8), 0, "double2ufix642");
  193. test_checku64(double2ufix64(32768.0, 16), 32768ull << 16, "double2ufix643");
  194. test_checku64(double2ufix64(65536.0, 16), 65536ull << 16, "double2ufix644");
  195. test_checku64(double2ufix64(2147483648.0, 16), 2147483648ull << 16, "double2ufix644b");
  196. test_checku64(double2ufix64(INFINITY, 16), UINT64_MAX, "double2ufix645");
  197. test_checku64(double2ufix64(-INFINITY, 16), 0, "double2ufix645b");
  198. test_checku64(double2ufix64(INFINITY, -16), UINT64_MAX, "double2ufix645c");
  199. test_checku64(double2ufix64(-INFINITY, -16), 0, "double2ufix645d");
  200. test_checku64(double2ufix64(3.24999, 2), 12, "double2ufix646");
  201. test_checku64(double2ufix64(3.25, 2), 13, "double2ufix647");
  202. test_checku64(double2ufix64(3.0, -1), 1, "double2ufix648"); // not very useful
  203. test_checki64(double2ufix64(0.0, 16), 0, "double2ufix649");
  204. test_checki64(double2ufix64(-0.0, 16), 0, "double2ufix6410");
  205. printf("double2fix_z\n");
  206. test_checki(double2fix_z(3.5, 8), 0x380, "double2fix_z1");
  207. test_checki(double2fix_z(-3.5, 8), -0x380, "double2fix_z2");
  208. test_checki(double2fix_z(32768.0, 16), INT32_MAX, "double2fix_z3");
  209. test_checki(double2fix_z(65536.0, 16), INT32_MAX, "double2fix_z4");
  210. test_checki(double2fix_z(INFINITY, 16), INT32_MAX, "double2fix_z5");
  211. test_checki(double2fix_z(-INFINITY, 16), INT32_MIN, "double2fix_z5b");
  212. test_checki(double2fix_z(INFINITY, -50), INT32_MAX, "double2fix_z5c");
  213. test_checki(double2fix_z(-INFINITY, -50), INT32_MIN, "double2fix_z5d");
  214. test_checki(double2fix_z(3.24999, 2), 12, "double2fix_z6");
  215. test_checki(double2fix_z(3.25, 2), 13, "double2fix_z7");
  216. test_checki(double2fix_z(-3.24999, 2), -12, "double2fix_z8");
  217. test_checki(double2fix_z(-3.25, 2), -13, "double2fix_z9");
  218. test_checki(double2fix_z(-0.75, 1), -1, "double2fix_z10");
  219. test_checki(double2fix_z(-3.0, -1), -1, "double2fix_z11"); // not very useful
  220. test_checki(double2fix_z(0.0, 16), 0, "double2fix_z12");
  221. test_checki(double2fix_z(-0.0, 16), 0, "double2fix_z13");
  222. test_checki(double2fix_z(0.0, -16), 0, "double2fix_z12b");
  223. test_checki(double2fix_z(-0.0, -16), 0, "double2fix_z13b");
  224. printf("double2ufix_z\n");
  225. test_checku(double2ufix_z(3.5, 8), 0x380, "double2ufix_z1");
  226. test_checku(double2ufix_z(-3.5, 8), 0, "double2ufix_z2");
  227. test_checku(double2ufix_z(32768.0, 16), 32768 << 16, "double2ufix_z3");
  228. test_checku(double2ufix_z(65536.0, 16), UINT32_MAX, "double2ufix_z4");
  229. test_checku(double2ufix_z(INFINITY, 16), UINT32_MAX, "double2ufix_z5");
  230. test_checku(double2ufix_z(-INFINITY, 16), 0, "double2ufix_z5b");
  231. test_checku(double2ufix_z(INFINITY, 16), UINT32_MAX, "double2ufix_z5c");
  232. test_checku(double2ufix_z(-INFINITY, 16), 0, "double2ufix_z5d");
  233. test_checku(double2ufix_z(3.24999, 2), 12, "double2ufix_z6");
  234. test_checku(double2ufix_z(3.25, 2), 13, "double2ufix_z7");
  235. test_checku(double2ufix_z(3.0, -1), 1, "double2ufix_z8"); // not very useful
  236. test_checki(double2ufix_z(0.0, 16), 0, "double2fix_z9");
  237. test_checki(double2ufix_z(-0.0, 16), 0, "double2fix_z10");
  238. test_checki(double2ufix_z(0.0, -16), 0, "double2fix_z11");
  239. test_checki(double2ufix_z(-0.0, -16), 0, "double2fix_z12");
  240. printf("double2fix64_z\n");
  241. test_checki64(double2fix64_z(3.5, 8), 0x380, "double2fix64_z1");
  242. test_checki64(double2fix64_z(-3.5, 8), -0x380, "double2fix64_z2");
  243. test_checki64(double2fix64_z(32768.0, 16), 32768ll << 16, "double2fix64_z3");
  244. test_checki64(double2fix64_z(65536.0, 16), 65536ll << 16, "double2fix64_z4");
  245. test_checki64(double2fix64_z(65536.0 * 65536.0 * 32768.0, 16), INT64_MAX, "double2fix64_z4b");
  246. test_checki64(double2fix64_z(INFINITY, 16), INT64_MAX, "double2fix64_z5");
  247. test_checki64(double2fix64_z(-INFINITY, 16), INT64_MIN, "double2fix64_z5");
  248. test_checki64(double2fix64_z(INFINITY, 16), INT64_MAX, "double2fix64_z5");
  249. test_checki64(double2fix64_z(-INFINITY, 16), INT64_MIN, "double2fix64_z5");
  250. test_checki64(double2fix64_z(3.24999, 2), 12, "double2fix64_z6");
  251. test_checki64(double2fix64_z(3.25, 2), 13, "double2fix64_z7");
  252. test_checki64(double2fix64_z(-3.24999, 2), -12, "double2fix64_z8");
  253. test_checki64(double2fix64_z(-3.25, 2), -13, "double2fix64_z9");
  254. test_checki64(double2fix64_z(-3.0, -1), -1, "double2fix64_z10"); // not very useful
  255. test_checki64(double2fix64_z(0.0, 16), 0, "double2fix64_z11");
  256. test_checki64(double2fix64_z(-0.0, 16), 0, "double2fix64_z12");
  257. test_checki64(double2fix64_z(0.0, -16), 0, "double2fix64_z13");
  258. test_checki64(double2fix64_z(-0.0, -16), 0, "double2fix64_z14");
  259. test_checki64(double2fix64_z(-3.25, 40), -13ll * (1ll << 38), "double2fix64_z15");
  260. u64d.u = 0xc00a000000000001;
  261. test_checki64(double2fix64_z(u64d.d, 40), -13ll * (1ll << 38), "double2fix64_z15b");
  262. u64d.u = 0xc00a000080000001;
  263. test_checki64(double2fix64_z(u64d.d, 20), -13ll * (1ll << 18) - 1ll, "double2fix64_z15c");
  264. u64d.u = 0xc00a000080000000;
  265. test_checki64(double2fix64_z(u64d.d, 20), -13ll * (1ll << 18) - 1ll, "double2fix64_z15d");
  266. u64d.u = 0xc00a000000000001;
  267. test_checki64(double2fix64_z(u64d.d, 20), -13ll * (1ll << 18), "double2fix64_z15e");
  268. u64d.u = 0xc00a000000000000;
  269. test_checki64(double2fix64_z(u64d.d, 20), -13ll * (1ll << 18), "double2fix64_z15g");
  270. u64d.u = 0xc00a000080000001;
  271. test_checki64(double2fix64_z(u64d.d, 19), -13ll * (1ll << 17), "double2fix64_z15h");
  272. u64d.u = 0xc00a000080000000;
  273. test_checki64(double2fix64_z(u64d.d, 19), -13ll * (1ll << 17), "double2fix64_z15i");
  274. u64d.u = 0xc00a000000000001;
  275. test_checki64(double2fix64_z(u64d.d, 19), -13ll * (1ll << 17), "double2fix64_z15j");
  276. u64d.u = 0xc00a000000000000;
  277. test_checki64(double2fix64_z(u64d.d, 19), -13ll * (1ll << 17), "double2fix64_z15k");
  278. printf("double2ufix64_z\n");
  279. test_checku64(double2ufix64_z(3.5, 8), 0x380, "double2ufix64_z1");
  280. test_checku64(double2ufix64_z(-3.5, 8), 0, "double2ufix64_z2");
  281. test_checku64(double2ufix64_z(32768.0, 16), 32768ll << 16, "double2ufix64_z3");
  282. test_checku64(double2ufix64_z(65536.0, 16), 65536ll << 16, "double2ufix64_z4");
  283. test_checki64(double2ufix64_z(65536.0 * 65536.0 * 65536.0, 16), UINT64_MAX, "double2fix64_z4b");
  284. test_checku64(double2ufix64_z(INFINITY, 16), UINT64_MAX, "double2ufix64_z5");
  285. test_checku64(double2ufix64_z(-INFINITY, 16), 0, "double2ufix64_z5b");
  286. test_checku64(double2ufix64_z(INFINITY, 16), UINT64_MAX, "double2ufix64_z5c");
  287. test_checku64(double2ufix64_z(-INFINITY, 16), 0, "double2ufix64_z5d");
  288. test_checku64(double2ufix64_z(3.24999, 2), 12, "double2ufix64_z6");
  289. test_checku64(double2ufix64_z(3.25, 2), 13, "double2ufix64_z7");
  290. test_checki64(double2ufix64_z(3.0, -1), 1, "double2fuix64_z8"); // not very useful
  291. test_checki64(double2ufix64_z(0.0, 16), 0, "double2ufix64_z9");
  292. test_checki64(double2ufix64_z(-0.0, 16), 0, "double2ufix64_z10");
  293. test_checki64(double2ufix64_z(0.0, -16), 0, "double2ufix64_z11");
  294. test_checki64(double2ufix64_z(-0.0, -16), 0, "double2ufix64_z12");
  295. printf("double2int\n");
  296. test_checki(double2int(0.0), 0, "double2int1");
  297. test_checki(double2int(0.25), 0, "double2int1b");
  298. test_checki(double2int(0.5), 0, "double2int2");
  299. test_checki(double2int(0.75), 0, "double2int2b");
  300. test_checki(double2int(1.0), 1, "double2int3");
  301. test_checki(double2int(-10.0), -10, "double2int3a");
  302. test_checki(double2int(-0.0), 0, "double2int3b");
  303. test_checki(double2int(-0.25), -1, "double2int4");
  304. test_checki(double2int(-0.5), -1, "double2int4b");
  305. test_checki(double2int(-0.75), -1, "double2int5");
  306. test_checki(double2int(-1.0), -1, "double2int5b");
  307. // todo test correct rounding around maximum precision
  308. test_checki(double2int(2147483646.0), INT32_MAX-1, "double2int6");
  309. test_checki(double2int(2147483647.0), INT32_MAX, "double2int6b");
  310. test_checki(double2int(21474836470.0), INT32_MAX, "double2int7");
  311. test_checki(double2int(-2147483648.0), INT32_MIN, "double2int8");
  312. test_checki(double2int(-21474836480.0), INT32_MIN, "double2int9");
  313. test_checki(double2int(-2.5), -3, "double2int10");
  314. test_checki(double2int(-2.4), -3, "double2int11");
  315. u64d.u = 0xc000000000000000ull;
  316. test_checki(double2int(u64d.d), -2, "double2int12");
  317. u64d.u = 0xc008000000000000ull;
  318. test_checki(double2int(u64d.d), -3, "double2int12b");
  319. u64d.u = 0xc000000000000001ull;
  320. test_checki(double2int(u64d.d), -3, "double2int12c");
  321. u64d.u = 0xc000000080000000ull;
  322. test_checki(double2int(u64d.d), -3, "double2int12d");
  323. u64d.u = 0xc000000100000000ull;
  324. test_checki(double2int(u64d.d), -3, "double2int12e");
  325. u64d.u = 0xc000000100000001ull;
  326. test_checki(double2int(u64d.d), -3, "double2int12f");
  327. test_checki(double2int(-2147483647.0), INT32_MIN+1, "double2int13");
  328. test_checki(double2int(-2147483647.1), INT32_MIN, "double2int14");
  329. test_checki(double2int(-2147483647.9), INT32_MIN, "double2int15");
  330. test_checki(double2int(-2147483648.0), INT32_MIN, "double2int16");
  331. test_checki(double2int(-2147483648.1), INT32_MIN, "double2int17");
  332. test_checki(double2int(-21474836480.1), INT32_MIN, "double2int18");
  333. printf("double2uint\n");
  334. test_checku(double2uint(0.0), 0, "double2uint1");
  335. test_checku(double2uint(0.25), 0, "double2uint2");
  336. test_checku(double2uint(0.5), 0, "double2uint3");
  337. test_checku(double2uint(0.75), 0, "double2uint4");
  338. test_checku(double2uint(1.0), 1, "double2uint5");
  339. test_checku(double2uint(2147483647.0), INT32_MAX, "double2uint6");
  340. test_checku(double2uint(2147483648.0), INT32_MAX+1u, "double2uint7");
  341. test_checku(double2uint(4294967294.5), UINT32_MAX-1, "double2uint8");
  342. test_checku(double2uint(4294967295.0), UINT32_MAX, "double2uint9");
  343. test_checku(double2uint(42949672950.0), UINT32_MAX, "double2uint10");
  344. printf("double2int64\n");
  345. test_checki64(double2int64(0.0), 0, "double2int641");
  346. test_checki64(double2int64(0.25), 0, "double2int641b");
  347. test_checki64(double2int64(0.5), 0, "double2int642");
  348. test_checki64(double2int64(0.75), 0, "double2int642b");
  349. test_checki64(double2int64(1.0), 1, "double2int643");
  350. test_checki64(double2int64(-10.0), -10, "double2int643a");
  351. test_checki64(double2int64(-0.0), 0, "double2int643b");
  352. test_checki64(double2int64(-0.25), -1, "double2int644");
  353. test_checki64(double2int64(-0.5), -1, "double2int644b");
  354. test_checki64(double2int64(-0.75), -1, "double2int645");
  355. test_checki64(double2int64(-1.0), -1, "double2int645b");
  356. // todo test correct rounding around maximum precision
  357. test_checki64(double2int64(2147483647.0), INT32_MAX, "double2int646");
  358. test_checki64(double2int64(21474836470.0), 21474836470ll, "double2int647");
  359. test_checki64(double2int64(-2147483648.0), INT32_MIN, "double2int648");
  360. test_checki64(double2int64(-21474836480.0), -21474836480ll, "double2int649");
  361. test_checki64(double2int64(-2.5), -3, "double2int6410");
  362. test_checki64(double2int64(-2.4), -3, "double2int6411");
  363. u64d.u = 0xc000000000000000ull;
  364. test_checki64(double2int64(u64d.d), -2, "double2int6412");
  365. u64d.u = 0xc008000000000000ull;
  366. test_checki64(double2int64(u64d.d), -3, "double2int6412b");
  367. u64d.u = 0xc000000000000001ull;
  368. test_checki64(double2int64(u64d.d), -3, "double2int6412c");
  369. u64d.u = 0xc000000080000000ull;
  370. test_checki64(double2int64(u64d.d), -3, "double2int6412d");
  371. u64d.u = 0xc000000100000000ull;
  372. test_checki64(double2int64(u64d.d), -3, "double2int6412e");
  373. u64d.u = 0xc000000100000001ull;
  374. test_checki64(double2int64(u64d.d), -3, "double2int6412f");
  375. printf("double2uint64\n");
  376. test_checku64(double2uint64(0.0), 0, "double2uint641");
  377. test_checku64(double2uint64(0.25), 0, "double2uint642");
  378. test_checku64(double2uint64(0.5), 0, "double2uint643");
  379. test_checku64(double2uint64(0.75), 0, "double2uint644");
  380. test_checku64(double2uint64(1.0), 1, "double2uint645");
  381. test_checku64(double2uint64(2147483647.0), INT32_MAX, "double2uint646");
  382. test_checku64(double2uint64(2147483648.0), INT32_MAX+1u, "double2uint647");
  383. // todo test correct rounding around maximum precision
  384. test_checku64(double2uint64(4294967294.5), 4294967294ull, "double2uint648");
  385. test_checku64(double2uint64(4294967295.0), 4294967295ull, "double2uint649");
  386. test_checku64(double2uint64(42949672950.0), 42949672950, "double2uint6410");
  387. #endif
  388. // // These methods round towards 0.
  389. printf("double2int_z\n");
  390. test_checki(double2int_z(0.0), 0, "double2int_z1");
  391. test_checki(double2int_z(0.25), 0, "double2int_z1b");
  392. test_checki(double2int_z(0.5), 0, "double2int_z2");
  393. test_checki(double2int_z(0.75), 0, "double2int_z2b");
  394. test_checki(double2int_z(1.0), 1, "double2int_z3");
  395. test_checki(double2int_z(-10.0), -10, "double2int_z3a");
  396. test_checki(double2int_z(-0.0), 0, "double2int_z3b");
  397. test_checki(double2int_z(-0.25), 0, "double2int_z4");
  398. test_checki(double2int_z(-0.5), 0, "double2int_z4b");
  399. test_checki(double2int_z(-0.75), 0, "double2int_z5");
  400. test_checki(double2int_z(-1.0), -1, "double2int_z5b");
  401. // todo test correct rounding around maximum precision
  402. test_checki(double2int_z(2147483647.0), INT32_MAX, "double2int_z6");
  403. test_checki(double2int_z(21474836470.0), INT32_MAX, "double2int_z7");
  404. test_checki(double2int_z(-2147483648.0), INT32_MIN, "double2int_z8");
  405. test_checki(double2int_z(-21474836480.0), INT32_MIN, "double2int_z9");
  406. test_checki(double2int_z(-2.5), -2, "double2int_z10");
  407. test_checki(double2int_z(-2.4), -2, "double2int_z11");
  408. u64d.u = 0xc000000000000000ull;
  409. test_checki(double2int_z(u64d.d), -2, "double2int_z12");
  410. u64d.u = 0xc008000000000000ull;
  411. test_checki(double2int_z(u64d.d), -3, "double2int_z12b");
  412. u64d.u = 0xc000000000000001ull;
  413. test_checki(double2int_z(u64d.d), -2, "double2int_z12c");
  414. u64d.u = 0xc000000080000000ull;
  415. test_checki(double2int_z(u64d.d), -2, "double2int_z12d");
  416. u64d.u = 0xc000000100000000ull;
  417. test_checki(double2int_z(u64d.d), -2, "double2int_z12e");
  418. u64d.u = 0xc000000100000001ull;
  419. test_checki(double2int_z(u64d.d), -2, "double2int_z12f");
  420. printf("double2int64_z\n");
  421. test_checki64(double2int64_z(0.0), 0, "double2int64_z1");
  422. test_checki64(double2int64_z(0.25), 0, "double2int64_z1b");
  423. test_checki64(double2int64_z(0.5), 0, "double2int64_z2");
  424. test_checki64(double2int64_z(0.75), 0, "double2int64_z2b");
  425. test_checki64(double2int64_z(1.0), 1, "double2int64_z3");
  426. test_checki64(double2int64_z(-10.0), -10, "double2int64_z3a");
  427. test_checki64(double2int64_z(-0.0), 0, "double2int64_z3b");
  428. test_checki64(double2int64_z(-0.25), 0, "double2int64_z4");
  429. test_checki64(double2int64_z(-0.5), 0, "double2int64_z4b");
  430. test_checki64(double2int64_z(-0.75), 0, "double2int64_z5");
  431. test_checki64(double2int64_z(-1.0), -1, "double2int64_z5b");
  432. // todo test correct rounding around maximum precision
  433. test_checki64(double2int64_z(2147483647.0), 2147483647ll, "double2int64_z6");
  434. test_checki64(double2int64_z(21474836470.0), 21474836470ll, "double2int64_z7");
  435. test_checki64(double2int64_z(-2147483648.0), INT32_MIN, "double2int64_z8");
  436. test_checki64(double2int64_z(-21474836480.0), -21474836480ll, "double2int64_z9");
  437. test_checki64(double2int64_z(-2.5), -2, "double2int64_z10");
  438. test_checki64(double2int64_z(-2.4), -2, "double2int64_z11");
  439. printf("double2uint_z\n");
  440. test_checku(double2uint_z(0.0), 0, "double2uint_z1");
  441. test_checku(double2uint_z(0.25), 0, "double2uint_z2");
  442. test_checku(double2uint_z(0.5), 0, "double2uint_z3");
  443. test_checku(double2uint_z(0.75), 0, "double2uint_z4");
  444. test_checku(double2uint_z(1.0), 1, "double2uint_z5");
  445. test_checku(double2uint_z(2147483647.0), INT32_MAX, "double2uint_z6");
  446. test_checku(double2uint_z(2147483648.0), INT32_MAX+1u, "double2uint_z7");
  447. // todo test correct rounding around maximum precision
  448. test_checku(double2uint_z(4294967294.5), UINT32_MAX-1u, "double2uint_z8");
  449. test_checku(double2uint_z(4294967295.0), UINT32_MAX, "double2uint_z9");
  450. test_checku(double2uint_z(42949672950.0), UINT32_MAX, "double2uint_z10");
  451. printf("double2uint64_z\n");
  452. test_checku64(double2uint64_z(0.0), 0, "double2uint64_z1");
  453. test_checku64(double2uint64_z(0.25), 0, "double2uint64_z2");
  454. test_checku64(double2uint64_z(0.5), 0, "double2uint64_z3");
  455. test_checku64(double2uint64_z(0.75), 0, "double2uint64_z4");
  456. test_checku64(double2uint64_z(1.0), 1, "double2uint64_z5");
  457. test_checku64(double2uint64_z(2147483647.0), INT32_MAX, "double2uint64_z6");
  458. test_checku64(double2uint64_z(2147483648.0), INT32_MAX+1u, "double2uint64_z7");
  459. // todo test correct rounding around maximum precision
  460. test_checku64(double2uint64_z(4294967294.5), 4294967294ull, "double2uint64_z8");
  461. test_checku64(double2uint64_z(4294967295.0), 4294967295ull, "double2uint64_z9");
  462. test_checku64(double2uint64_z(4294967296.0), 4294967296ull, "double2uint64_z9b");
  463. test_checku64(double2uint64_z(42949672950.0), 42949672950ull, "double2uint64_z10");
  464. // double exp10(double x);
  465. // void sincos(double x, double *sinx, double *cosx);
  466. // double powint(double x, int y);
  467. return rc;
  468. }
  469. int main() {
  470. stdio_init_all();
  471. int rc = test();
  472. if (rc) {
  473. printf("FAILED\n");
  474. } else {
  475. printf("PASSED\n");
  476. }
  477. }