> I've been working on an interface to BLAS recently, and I encountered
> a problem with a call to a C function having a `float' argument. The C
> function (declaration) is the following:
> 
> void cblas_saxpy(const int N, const float alpha, const float *X, const
> int incX, float *Y, const int incY);
> 
> When I use `foreign import' in ghc-4.08 in the most obvious way (at
> least with respect to the `alpha' arg), that is
> 
> foreign import "cblas_saxpy" unsafe
>   cblas_saxpy :: CInt -> CFloat -> ByteArray# -> CInt ->
>                  MutableByteArray# s -> CInt -> IO ()
> 
> my code does not produce correct results. However, when I wrap the
> `cblas_saxpy' function with a function uses a double arg for `alpha',
> and then demotes the value to a float before calling `cblas_saxpy', my
> code produces correct results. It appears to me that the FFI is
> producing a call to a K&R-style C function that promotes float
> arguments to double. If this is what is really happening, is there
> some way to change the default style of C-function call to ANSI? On
> the other hand, return values of either precision appear to work as I
> expect (I don't recall whether K&R also promote float return values to
> double).

This was a problem with pre-4.08 GHC's, but 4.08 should be ok.  Remember if
you're compiling via C (i.e. using -O or -fvia-C, or you're on a non-x86
machine), then you need to make sure the relevant prototype is in scope for
the C compiler to generate the correct code.  See the notes in the User's
Guide on the -#include flag for more details.

Cheers,
        Simon

Reply via email to