testapp.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. int
  10. intToStr(int x, char *str, int str_len, int digit);
  11. int
  12. get_pow(int x, int y);
  13. int32_t
  14. calculate_native(int32_t n, int32_t func1, int32_t func2);
  15. //
  16. // Primitive parameters functions
  17. //
  18. float
  19. generate_float(int iteration, double seed1, float seed2)
  20. {
  21. float ret;
  22. printf("calling into WASM function: %s\n", __FUNCTION__);
  23. for (int i = 0; i < iteration; i++) {
  24. ret += 1.0f / seed1 + seed2;
  25. }
  26. return ret;
  27. }
  28. // Converts a floating-point/double number to a string.
  29. // intToStr() is implemented outside wasm app
  30. void
  31. float_to_string(float n, char *res, int res_size, int afterpoint)
  32. {
  33. printf("calling into WASM function: %s\n", __FUNCTION__);
  34. // Extract integer part
  35. int ipart = (int)n;
  36. // Extract floating part
  37. float fpart = n - (float)ipart;
  38. // convert integer part to string
  39. int i = intToStr(ipart, res, res_size, 0);
  40. // check for display option after point
  41. if (afterpoint != 0) {
  42. res[i] = '.'; // add dot
  43. // Get the value of fraction part upto given no.
  44. // of points after dot. The third parameter
  45. // is needed to handle cases like 233.007
  46. fpart = fpart * get_pow(10, afterpoint);
  47. intToStr((int)fpart, res + i + 1, res_size - i - 1, afterpoint);
  48. }
  49. }
  50. int32_t
  51. mul7(int32_t n)
  52. {
  53. printf("calling into WASM function: %s,", __FUNCTION__);
  54. n = n * 7;
  55. printf(" %s return %d \n", __FUNCTION__, n);
  56. return n;
  57. }
  58. int32_t
  59. mul5(int32_t n)
  60. {
  61. printf("calling into WASM function: %s,", __FUNCTION__);
  62. n = n * 5;
  63. printf(" %s return %d \n", __FUNCTION__, n);
  64. return n;
  65. }
  66. int32_t
  67. calculate(int32_t n)
  68. {
  69. printf("calling into WASM function: %s\n", __FUNCTION__);
  70. int32_t (*f1)(int32_t) = &mul5;
  71. int32_t (*f2)(int32_t) = &mul7;
  72. return calculate_native(n, (uint32_t)f1, (uint32_t)f2);
  73. }