io.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Functions:
  9. CGLM_INLINE void glm_mat4_print(mat4 matrix, FILE *ostream);
  10. CGLM_INLINE void glm_mat3_print(mat3 matrix, FILE *ostream);
  11. CGLM_INLINE void glm_vec4_print(vec4 vec, FILE *ostream);
  12. CGLM_INLINE void glm_ivec4_print(ivec4 vec, FILE *ostream);
  13. CGLM_INLINE void glm_vec3_print(vec3 vec, FILE *ostream);
  14. CGLM_INLINE void glm_ivec3_print(ivec3 vec, FILE *ostream);
  15. CGLM_INLINE void glm_vec2_print(vec2 vec, FILE *ostream);
  16. CGLM_INLINE void glm_ivec2_print(ivec2 vec, FILE *ostream);
  17. CGLM_INLINE void glm_versor_print(versor vec, FILE *ostream);
  18. CGLM_INLINE void glm_arch_print(FILE *ostream);
  19. */
  20. /*
  21. cglm tried to enable print functions in debug mode and disable them in
  22. release/production mode to eliminate printing costs.
  23. if you need to force enable then define CGLM_DEFINE_PRINTS macro not DEBUG one
  24. Print functions are enabled if:
  25. - DEBUG or _DEBUG macro is defined (mostly defined automatically in debugging)
  26. - CGLM_DEFINE_PRINTS macro is defined including release/production
  27. which makes enabled printing always
  28. - glmc_ calls for io are always prints
  29. */
  30. /* DEPRECATED: CGLM_NO_PRINTS_NOOP (use CGLM_DEFINE_PRINTS) */
  31. #ifndef cglm_io_h
  32. #define cglm_io_h
  33. #if !defined(NDEBUG) \
  34. || defined(CGLM_DEFINE_PRINTS) || defined(CGLM_LIB_SRC) \
  35. || defined(CGLM_NO_PRINTS_NOOP)
  36. #include "common.h"
  37. #include "util.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #ifndef CGLM_PRINT_PRECISION
  41. # define CGLM_PRINT_PRECISION 5
  42. #endif
  43. #ifndef CGLM_PRINT_MAX_TO_SHORT
  44. # define CGLM_PRINT_MAX_TO_SHORT 1e5f
  45. #endif
  46. #ifndef GLM_TESTS_NO_COLORFUL_OUTPUT
  47. # ifndef CGLM_PRINT_COLOR
  48. # define CGLM_PRINT_COLOR "\033[36m"
  49. # endif
  50. # ifndef CGLM_PRINT_COLOR_RESET
  51. # define CGLM_PRINT_COLOR_RESET "\033[0m"
  52. # endif
  53. #else
  54. # ifndef CGLM_PRINT_COLOR
  55. # define CGLM_PRINT_COLOR
  56. # endif
  57. # ifndef CGLM_PRINT_COLOR_RESET
  58. # define CGLM_PRINT_COLOR_RESET
  59. # endif
  60. #endif
  61. /*!
  62. * @brief prints current SIMD path in general
  63. *
  64. * @param[in] ostream stream to print e.g. stdout, stderr, FILE ...
  65. */
  66. CGLM_INLINE
  67. void
  68. glm_arch_print(FILE* __restrict ostream) {
  69. fprintf(ostream, CGLM_PRINT_COLOR "arch: "
  70. #if defined(CGLM_SIMD_WASM)
  71. "wasm SIMD128"
  72. #elif defined(CGLM_SIMD_x86)
  73. "x86 SSE* "
  74. # ifdef __AVX__
  75. " AVX"
  76. # endif
  77. #elif defined(CGLM_SIMD_ARM)
  78. "arm"
  79. # ifndef __ARM_NEON_FP
  80. " NEON_FP"
  81. # endif
  82. # ifdef CGLM_ARM64
  83. " ARM64"
  84. # endif
  85. #else
  86. "uncommon"
  87. #endif
  88. CGLM_PRINT_COLOR_RESET);
  89. }
  90. /*!
  91. * @brief prints current SIMD path in general
  92. *
  93. * @param[in] ostream stream to print e.g. stdout, stderr, FILE ...
  94. */
  95. CGLM_INLINE
  96. void
  97. glm_arch_print_name(FILE* __restrict ostream) {
  98. fprintf(ostream, CGLM_PRINT_COLOR "\ncglm ");
  99. glm_arch_print(ostream);
  100. fprintf(ostream, "\n\n" CGLM_PRINT_COLOR_RESET);
  101. }
  102. CGLM_INLINE
  103. void
  104. glm_mat4_print(mat4 matrix,
  105. FILE * __restrict ostream) {
  106. char buff[16];
  107. int i, j, cw[4], cwi;
  108. #define m 4
  109. #define n 4
  110. fprintf(ostream, "Matrix (float%dx%d): " CGLM_PRINT_COLOR "\n" , m, n);
  111. cw[0] = cw[1] = cw[2] = cw[3] = 0;
  112. for (i = 0; i < m; i++) {
  113. for (j = 0; j < n; j++) {
  114. if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  115. cwi = snprintf(buff, sizeof(buff), "% .*f", CGLM_PRINT_PRECISION, (double)matrix[i][j]);
  116. else
  117. cwi = snprintf(buff, sizeof(buff), "% g", (double)matrix[i][j]);
  118. cw[i] = GLM_MAX(cw[i], cwi);
  119. }
  120. }
  121. for (i = 0; i < m; i++) {
  122. fprintf(ostream, " |");
  123. for (j = 0; j < n; j++)
  124. if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  125. fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, (double)matrix[j][i]);
  126. else
  127. fprintf(ostream, " % *g", cw[j], (double)matrix[j][i]);
  128. fprintf(ostream, " |\n");
  129. }
  130. fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n");
  131. #undef m
  132. #undef n
  133. }
  134. CGLM_INLINE
  135. void
  136. glm_mat3_print(mat3 matrix,
  137. FILE * __restrict ostream) {
  138. char buff[16];
  139. int i, j, cw[4], cwi;
  140. #define m 3
  141. #define n 3
  142. fprintf(ostream, "Matrix (float%dx%d): " CGLM_PRINT_COLOR "\n", m, n);
  143. cw[0] = cw[1] = cw[2] = 0;
  144. for (i = 0; i < m; i++) {
  145. for (j = 0; j < n; j++) {
  146. if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  147. cwi = snprintf(buff, sizeof(buff), "% .*f", CGLM_PRINT_PRECISION, (double)matrix[i][j]);
  148. else
  149. cwi = snprintf(buff, sizeof(buff), "% g", (double)matrix[i][j]);
  150. cw[i] = GLM_MAX(cw[i], cwi);
  151. }
  152. }
  153. for (i = 0; i < m; i++) {
  154. fprintf(ostream, " |");
  155. for (j = 0; j < n; j++)
  156. if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  157. fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, (double)matrix[j][i]);
  158. else
  159. fprintf(ostream, " % *g", cw[j], (double)matrix[j][i]);
  160. fprintf(ostream, " |\n");
  161. }
  162. fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n");
  163. #undef m
  164. #undef n
  165. }
  166. CGLM_INLINE
  167. void
  168. glm_mat2_print(mat2 matrix,
  169. FILE * __restrict ostream) {
  170. char buff[16];
  171. int i, j, cw[4], cwi;
  172. #define m 2
  173. #define n 2
  174. fprintf(ostream, "Matrix (float%dx%d): " CGLM_PRINT_COLOR "\n", m, n);
  175. cw[0] = cw[1] = 0;
  176. for (i = 0; i < m; i++) {
  177. for (j = 0; j < n; j++) {
  178. if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  179. cwi = snprintf(buff, sizeof(buff), "% .*f", CGLM_PRINT_PRECISION, (double)matrix[i][j]);
  180. else
  181. cwi = snprintf(buff, sizeof(buff), "% g", (double)matrix[i][j]);
  182. cw[i] = GLM_MAX(cw[i], cwi);
  183. }
  184. }
  185. for (i = 0; i < m; i++) {
  186. fprintf(ostream, " |");
  187. for (j = 0; j < n; j++)
  188. if (matrix[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  189. fprintf(ostream, " % *.*f", cw[j], CGLM_PRINT_PRECISION, (double)matrix[j][i]);
  190. else
  191. fprintf(ostream, " % *g", cw[j], (double)matrix[j][i]);
  192. fprintf(ostream, " |\n");
  193. }
  194. fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n");
  195. #undef m
  196. #undef n
  197. }
  198. CGLM_INLINE
  199. void
  200. glm_vec4_print(vec4 vec,
  201. FILE * __restrict ostream) {
  202. int i;
  203. #define m 4
  204. fprintf(ostream, "Vector (float%d): " CGLM_PRINT_COLOR "\n (", m);
  205. for (i = 0; i < m; i++) {
  206. if (vec[i] < CGLM_PRINT_MAX_TO_SHORT)
  207. fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, (double)vec[i]);
  208. else
  209. fprintf(ostream, " % g", (double)vec[i]);
  210. }
  211. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  212. #undef m
  213. }
  214. CGLM_INLINE
  215. void
  216. glm_ivec4_print(ivec4 vec,
  217. FILE * __restrict ostream) {
  218. int i;
  219. #define m 4
  220. fprintf(ostream, "Vector (int%d): " CGLM_PRINT_COLOR "\n (", m);
  221. for (i = 0; i < m; i++)
  222. fprintf(ostream, " % d", vec[i]);
  223. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  224. #undef m
  225. }
  226. CGLM_INLINE
  227. void
  228. glm_vec3_print(vec3 vec,
  229. FILE * __restrict ostream) {
  230. int i;
  231. #define m 3
  232. fprintf(ostream, "Vector (float%d): " CGLM_PRINT_COLOR "\n (", m);
  233. for (i = 0; i < m; i++) {
  234. if (vec[i] < CGLM_PRINT_MAX_TO_SHORT)
  235. fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, (double)vec[i]);
  236. else
  237. fprintf(ostream, " % g", (double)vec[i]);
  238. }
  239. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  240. #undef m
  241. }
  242. CGLM_INLINE
  243. void
  244. glm_ivec3_print(ivec3 vec,
  245. FILE * __restrict ostream) {
  246. int i;
  247. #define m 3
  248. fprintf(ostream, "Vector (int%d): " CGLM_PRINT_COLOR "\n (", m);
  249. for (i = 0; i < m; i++)
  250. fprintf(ostream, " % d", vec[i]);
  251. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  252. #undef m
  253. }
  254. CGLM_INLINE
  255. void
  256. glm_vec2_print(vec2 vec,
  257. FILE * __restrict ostream) {
  258. int i;
  259. #define m 2
  260. fprintf(ostream, "Vector (float%d): " CGLM_PRINT_COLOR "\n (", m);
  261. for (i = 0; i < m; i++) {
  262. if (vec[i] < CGLM_PRINT_MAX_TO_SHORT)
  263. fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, (double)vec[i]);
  264. else
  265. fprintf(ostream, " % g", (double)vec[i]);
  266. }
  267. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  268. #undef m
  269. }
  270. CGLM_INLINE
  271. void
  272. glm_ivec2_print(ivec2 vec,
  273. FILE * __restrict ostream) {
  274. int i;
  275. #define m 2
  276. fprintf(ostream, "Vector (int%d): " CGLM_PRINT_COLOR "\n (", m);
  277. for (i = 0; i < m; i++)
  278. fprintf(ostream, " % d", vec[i]);
  279. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  280. #undef m
  281. }
  282. CGLM_INLINE
  283. void
  284. glm_versor_print(versor vec,
  285. FILE * __restrict ostream) {
  286. int i;
  287. #define m 4
  288. fprintf(ostream, "Quaternion (float%d): " CGLM_PRINT_COLOR "\n (", m);
  289. for (i = 0; i < m; i++) {
  290. if (vec[i] < CGLM_PRINT_MAX_TO_SHORT)
  291. fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, (double)vec[i]);
  292. else
  293. fprintf(ostream, " % g", (double)vec[i]);
  294. }
  295. fprintf(ostream, " )" CGLM_PRINT_COLOR_RESET "\n\n");
  296. #undef m
  297. }
  298. CGLM_INLINE
  299. void
  300. glm_aabb_print(vec3 bbox[2],
  301. const char * __restrict tag,
  302. FILE * __restrict ostream) {
  303. int i, j;
  304. #define m 3
  305. fprintf(ostream, "AABB (%s): " CGLM_PRINT_COLOR "\n", tag ? tag: "float");
  306. for (i = 0; i < 2; i++) {
  307. fprintf(ostream, " (");
  308. for (j = 0; j < m; j++) {
  309. if (bbox[i][j] < CGLM_PRINT_MAX_TO_SHORT)
  310. fprintf(ostream, " % .*f", CGLM_PRINT_PRECISION, (double)bbox[i][j]);
  311. else
  312. fprintf(ostream, " % g", (double)bbox[i][j]);
  313. }
  314. fprintf(ostream, " )\n");
  315. }
  316. fprintf(ostream, CGLM_PRINT_COLOR_RESET "\n");
  317. #undef m
  318. }
  319. #else
  320. #include "common.h"
  321. #include <stdio.h>
  322. #include <stdlib.h>
  323. /* NOOP: Remove print from DEBUG */
  324. #define glm_mat4_print(v, s) (void)v; (void)s;
  325. #define glm_mat3_print(v, s) (void)v; (void)s;
  326. #define glm_mat2_print(v, s) (void)v; (void)s;
  327. #define glm_vec4_print(v, s) (void)v; (void)s;
  328. #define glm_ivec4_print(v, s) (void)v; (void)s;
  329. #define glm_vec3_print(v, s) (void)v; (void)s;
  330. #define glm_ivec3_print(v, s) (void)v; (void)s;
  331. #define glm_vec2_print(v, s) (void)v; (void)s;
  332. #define glm_ivec2_print(v, s) (void)v; (void)s;
  333. #define glm_versor_print(v, s) (void)v; (void)s;
  334. #define glm_aabb_print(v, t, s) (void)v; (void)t; (void)s;
  335. #define glm_arch_print(s) (void)s;
  336. #define glm_arch_print_name(s) (void)s;
  337. #endif
  338. #endif /* cglm_io_h */