testapp.c 2.0 KB

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