[LLVMdev] complex numbers with LLVM

David A. Greene greened at obbligato.org
Tue Dec 21 12:47:43 PST 2010


Richard <legalize at xmission.com> writes:

> This leaves me with two basic types of questions:
>
> 1. Is there a "best" representation for complex numbers in LLVM?
>    Is my simple "struct of two floats" idea sufficient?

IMHO, the best representation would use a vector of two floats or two
doubles.  That way many of the operations map directly onto LLVM vector
operations and can in turn make use of any target SIMD instructions.
Complex multiplication is a typical example where the x86-style addsub
instructions are very useful.

>    Is there a way to maintain binary compatability with
>    std::complex<float> or std::complex<double> so that those types can
>    be used directly when calling LLVM generated native code?

You'll need some way to convert between your representation and whatever
complex<> is on your platform.  That's true no matter what your
representation is.  If you don't care too much about portability you can
pick a representation that naturally converts to complex<> on your
platform.

> 2. What's the best way to expose the C++ complex trig, etc., functions?
>    Is there a way I can use LLVM IR to write the type signature of
>    these functions?

I'm not exactly sure what you're asking.  It's certainly possible to
create a function signature that matches the C++ trig functions.  How
you expose that in your language is entirely up to you.  Pick a
signature that matches your language naturally and then do any
conversion required to invoke the C++ trig functions with the arguments
passed to your language trig API functions.

>    Do I need to provide my own wrapper matching the representation
>    used in 1. above?

It depends on how you want to do it.  With a wrapper you can write the
representation conversion outside the compiler and link it in as a
runtime library.  But that means more little bits to keep track of.

                            -Dave



More information about the llvm-dev mailing list