main.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <inttypes.h>
  6. #include <libfixmath/fixmath.h>
  7. #include "hiclock.h"
  8. //#define fix_func fix16_exp
  9. //#define fix_func_str "fix16_exp"
  10. //#define flt_func expf
  11. //#define flt_func_str "expf"
  12. //#define fix_func fix16_atan
  13. //#define fix_func_str "fix16_atan"
  14. //#define flt_func atanf
  15. //#define flt_func_str "atanf"
  16. #define fix_func fix16_sin
  17. #define fix_func_str "fix16_sin"
  18. #define flt_func sinf
  19. #define flt_func_str "sinf"
  20. //#define fix_func fix16_sqrt
  21. //#define fix_func_str "fix16_sqrt"
  22. //#define flt_func sqrtf
  23. //#define flt_func_str "sqrtf"
  24. int main(int argc, char** argv) {
  25. printf("libfixmath test tool\n");
  26. hiclock_init();
  27. uintptr_t args = (1 << 8);
  28. uintptr_t iter = (1 << 8);
  29. uintptr_t pass = (1 << 8);
  30. uintptr_t i;
  31. srand(time(NULL));
  32. hiclock_t fix_duration = 0;
  33. hiclock_t flt_duration = 0;
  34. fix16_t fix_error = 0;
  35. uintptr_t k;
  36. for(k = 0; k < pass; k++) {
  37. fix16_t fix_args[args];
  38. for(i = 0; i < args; i++)
  39. fix_args[i] = (rand() ^ (rand() << 16));
  40. fix16_t fix_result[args];
  41. hiclock_t fix_start = hiclock();
  42. for(i = 0; i < iter; i++) {
  43. uintptr_t j;
  44. for(j = 0; j < args; j++)
  45. fix_result[j] = fix_func(fix_args[j]);
  46. }
  47. hiclock_t fix_end = hiclock();
  48. float flt_args[args];
  49. for(i = 0; i < args; i++)
  50. flt_args[i] = fix16_to_float(fix_args[i]);
  51. float flt_result[args];
  52. hiclock_t flt_start = hiclock();
  53. for(i = 0; i < iter; i++) {
  54. uintptr_t j;
  55. for(j = 0; j < args; j++)
  56. flt_result[j] = flt_func(flt_args[j]);
  57. }
  58. hiclock_t flt_end = hiclock();
  59. for(i = 0; i < args; i++)
  60. fix_error += abs(fix16_from_float(flt_result[i]) - fix_result[i]);
  61. flt_duration += (flt_end - flt_start);
  62. fix_duration += (fix_end - fix_start);
  63. }
  64. printf("%16s: %08"PRIuHICLOCK" @ %"PRIu32"Hz\n", flt_func_str, flt_duration, HICLOCKS_PER_SEC);
  65. printf("%16s: %08"PRIuHICLOCK" @ %"PRIu32"Hz\n", fix_func_str, fix_duration, HICLOCKS_PER_SEC);
  66. printf(" Difference: %08"PRIiHICLOCK" (% 3.2f%%)\n", (flt_duration - fix_duration), ((fix_duration * 100.0) / flt_duration));
  67. printf(" Error: %f%%\n", ((fix16_to_dbl(fix_error) * 100.0) / (args * pass)));
  68. return EXIT_SUCCESS;
  69. }