Unsigned long in ccall

I need to call a MPFR library function whose signature is int mpfr_sub_ui(mpfr_t rop, mpfr_t op1, unsigned long int op2, mpfr_rnd_t rnd).

The manual says that unsigned long is an Int on Unix and an Int32 on Windows. Is there a way to ccall the library function that works on both Unix and Windows?

Culong should do the right thing:

julia> Culong
1 Like


1 Like

But the manual says:

System Dependent Types

C name Standard Julia Alias Julia Base Type
char Cchar Int8 (x86, x86_64), UInt8 (powerpc, arm)
long Clong Int (UNIX), Int32 (Windows)
unsigned long Culong UInt (UNIX), UInt32 (Windows)
wchar_t Cwchar_t Int32 (UNIX), UInt16 (Windows)

The library has also function mpfr_sub_si with signature long int. What I should have asked is how should the Julia wrappers be defined.

function mpfr_sub(rop::BigFloat, op1::BigFloat, op2::UInt, rnd) # for unsigned long int
    ccall((:mpfr_sub_ui, :libmpfr), Int32,
              (Ref{BigFloat}, Ref{BigFloat}, Culong, MPFR.MPFRRoundingMode),
              rop, op1, op2, rnd)


function mpfr_sub(rop::BigFloat, op1::BigFloat, op2::Int, rnd) # for long int
    ccall((:mpfr_sub_si, :libmpfr), Int32,
              (Ref{BigFloat}, Ref{BigFloat}, Clong, MPFR.MPFRRoundingMode),
              rop, op1, op2, rnd)

Julia’s MPFR module in Base uses ClongMax and CulongMax defined in GMP:

julia> Base.GMP.CulongMax
Union{UInt16, UInt32, UInt8}

julia> Base.GMP.ClongMax
Union{Int16, Int32, Int8}

Because you are on a Unix-like system? On Windows, I get:

julia> Culong