Re: Problems with C floats
- Posted by irv Feb 01, 2010
- 2291 views
// irv.c // build with $ gcc -shared -fPIC -ldl irv3.c -o irv3.so #include <stdio.h> static int a = 12; static double b = 34567890; static float c = 67.890123; void test_float(int *foo, double *bar, float *baz){ printf( "C in:\n\tfoo: %d\n\tbar: %e\n\tbaz: %f\n\n", *foo,*bar,*baz); printf("C routine changes values & should return \n\t12, \t34567890, \t67.890123\n\n"); *foo = a; *bar = b; *baz = c; printf( "C out:\n\tfoo: %d\n\tbar: %e\n\tbaz: %f\n\n", *foo,*bar,*baz); }
-- irv3.ex include std/dll.e include std/memory.e include std/convert.e as convert constant irv = open_dll( "./irv3.so" ), test1 = define_c_proc( irv, "test_float", {C_POINTER,C_POINTER,C_POINTER}), test2 = define_c_proc( irv, "test_float", {C_INT,C_DOUBLE,C_FLOAT}) atom ptr1 = allocate(4), ptr2 = allocate(8), ptr3 = allocate(8) poke(ptr1,convert:int_to_bytes(123)) poke(ptr2,convert:atom_to_float64(123456789)) poke(ptr3,convert:atom_to_float32(3.14159)) puts(1,"Eu sends: 123, 123456789, 3.14159 to C routine:\n") -- change the following to test2, and you get a machine exception: c_proc( test1, {ptr1,ptr2,ptr3}) puts(1,"Eu received:\n") puts(1,"\tINT:") ? peek(ptr1) puts(1,"\tDBL:") ? convert:float64_to_atom(peek({ptr2,8})) puts(1,"\tFLT:") ? convert:float32_to_atom(peek({ptr3,4}))
This is the best I can do without knowing any C. Test 1 successfully passes values both ways. Test 2 crashes every time, no matter what values are sent.