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