testapp.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. int intToStr(int x, char* str, int str_len, int digit);
  9. int get_pow(int x, int y);
  10. //
  11. // Primitive parameters functions
  12. //
  13. float generate_float(int iteration, double seed1, float seed2)
  14. {
  15. float ret;
  16. printf ("calling into WASM function: %s\n", __FUNCTION__);
  17. for (int i=0; i<iteration; i++){
  18. ret += 1.0f/seed1 + seed2;
  19. }
  20. return ret;
  21. }
  22. // Converts a floating-point/double number to a string.
  23. // intToStr() is implemented outside wasm app
  24. void float_to_string(float n, char* res, int res_size, int afterpoint)
  25. {
  26. printf ("calling into WASM function: %s\n", __FUNCTION__);
  27. // Extract integer part
  28. int ipart = (int)n;
  29. // Extract floating part
  30. float fpart = n - (float)ipart;
  31. // convert integer part to string
  32. int i = intToStr(ipart, res, res_size, 0);
  33. // check for display option after point
  34. if (afterpoint != 0) {
  35. res[i] = '.'; // add dot
  36. // Get the value of fraction part upto given no.
  37. // of points after dot. The third parameter
  38. // is needed to handle cases like 233.007
  39. fpart = fpart * get_pow(10, afterpoint);
  40. intToStr((int)fpart, res + i + 1, sizeof(res + i + 1), afterpoint);
  41. }
  42. }