Search code examples
racketieee-754

Fused multiply-add in Racket


Is is it possible to use fused multiply-add in Racket?

I'm attempting to port some numerical code that relies on the single rounding of the fused operation, but after several searches haven't found any way to use fma or similar in Racket.

Thanks!!


Solution

  • You can get it from the C library using the FFI:

    (require ffi/unsafe ffi/unsafe/define)
    (define-ffi-definer define-libc #f)
    (define-libc fma (_fun _double _double _double -> _double))
    (fma 3.0 2.0 1.0)
    ;; => 7.0
    

    That works for me on Linux. On Windows you may need to give a different library to define-ffi-definer.

    You can also use _double* instead of _double for the arguments to automatically convert Racket's other numeric types to doubles.

    If the performance is critical, you may need compiler and/or runtime support to avoid the overhead the FFI imposes. In that case, you might want to ask on the Racket Users mailing list.