/js/src/ctypes/libffi/testsuite/libffi.call/float1.c

http://github.com/zpao/v8monkey · C · 58 lines · 32 code · 15 blank · 11 comment · 3 complexity · b2e05f5c5acbdc121034880fa7ee407d MD5 · raw file

  1. /* Area: ffi_call
  2. Purpose: Check return value double.
  3. Limitations: none.
  4. PR: none.
  5. Originator: From the original ffitest.c */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. #include "float.h"
  9. typedef union
  10. {
  11. double d;
  12. unsigned char c[sizeof (double)];
  13. } value_type;
  14. #define CANARY 0xba
  15. static double dblit(float f)
  16. {
  17. return f/3.0;
  18. }
  19. int main (void)
  20. {
  21. ffi_cif cif;
  22. ffi_type *args[MAX_ARGS];
  23. void *values[MAX_ARGS];
  24. float f;
  25. value_type result[2];
  26. unsigned int i;
  27. args[0] = &ffi_type_float;
  28. values[0] = &f;
  29. /* Initialize the cif */
  30. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
  31. &ffi_type_double, args) == FFI_OK);
  32. f = 3.14159;
  33. /* Put a canary in the return array. This is a regression test for
  34. a buffer overrun. */
  35. memset(result[1].c, CANARY, sizeof (double));
  36. ffi_call(&cif, FFI_FN(dblit), &result[0].d, values);
  37. /* These are not always the same!! Check for a reasonable delta */
  38. CHECK(result[0].d - dblit(f) < DBL_EPSILON);
  39. /* Check the canary. */
  40. for (i = 0; i < sizeof (double); ++i)
  41. CHECK(result[1].c[i] == CANARY);
  42. exit(0);
  43. }