Defining a type depending on the word size of platform

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Defining a type depending on the word size of platform

Robert Clausecker
Hi guys!

I'm working on a random generator package and want to ask, whether it's
possible to generate a type like Int, that can have different sizes at
runtime.

Motivation:

The package ships to generators, one for 32 (lets name it MyGen32) and
one for 64 bits (MyGen64). As it is not good for performance to use the
wrong one, I also want to provide a type MyGen, that is either a type
synonym for MyGen32 or MyGen64, depending on the platform. Any ideas how
to do this at compile time? I want this to compile with Cabal so that
anybody can download it as a package.

I guess the easiest way would be to use CPP, but is there any flag for
the wordsize?

Yours, Robert Clausecker


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Defining a type depending on the word size of platform

Daniel Fischer
On Sunday 02 January 2011 13:45:13, Robert Clausecker wrote:
>
> I guess the easiest way would be to use CPP, but is there any flag for
> the wordsize?

Not directly, but I think

{-# LANGUAGE CPP #-}

#include "MachDeps.h"

#if WORD_SIZE_IN_BITS == 32

type MyGen = MyGen32

#else    // GHC only works with 32- and 64-bit words

type MyGen = MyGen64

#endif

should do it.

MachDeps.h ties you to GHC of course, if you want it to work on other
compilers too, I guess you need a configure script.



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Defining a type depending on the word size of platform

Diego Souza-5
Hi,

maybe you have already considered this and dropped it out for whenever reasons. Anyways, what if you stick with Int or Data.Word.Word types and use Data.Bits.bitSize or maxBound to check in runtime what the word size is. It might be easier than using CPP extension.

~dsouza

On Sun, Jan 2, 2011 at 11:16 AM, Daniel Fischer <[hidden email]> wrote:
On Sunday 02 January 2011 13:45:13, Robert Clausecker wrote:
>
> I guess the easiest way would be to use CPP, but is there any flag for
> the wordsize?

Not directly, but I think

{-# LANGUAGE CPP #-}

#include "MachDeps.h"

#if WORD_SIZE_IN_BITS == 32

type MyGen = MyGen32

#else    // GHC only works with 32- and 64-bit words

type MyGen = MyGen64

#endif

should do it.

MachDeps.h ties you to GHC of course, if you want it to work on other
compilers too, I guess you need a configure script.



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



--
~dsouza
yahoo!im: paravinicius
gpg key fingerprint: 71B8 CE21 3A6E F894 5B1B  9ECE F88E 067F E891 651E
gpg pub key: http://bitforest.org/~dsouza/pub/gpg-pubkey.txt
authorized_keys: http://bitforest.org/~dsouza/pub/authorized_keys.txt

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe