tags:

views:

198

answers:

2

Hi,

I'm writing a bignum library, and I want to use efficient data types to represent the digits. Particularly integer for the digit, and long (if strictly double the size of the integer) for intermediate representations when adding and multiplying.

I will be using some C99 functionality, but trying to conform to ANSI C.

Currently I have the following in my bignum library:

#include <stdint.h>

#if defined(__LP64__) || defined(__amd64) || defined(__x86_64) || defined(__amd64__) || defined(__amd64__) || defined(_LP64)
typedef uint64_t u_w;
typedef uint32_t u_hw;
#define BIGNUM_DIGITS 2048
#define U_HW_BITS 16
#define U_W_BITS 32
#define U_HW_MAX UINT32_MAX
#define U_HW_MIN UINT32_MIN
#define U_W_MAX UINT64_MAX
#define U_W_MIN UINT64_MIN
#else
typedef uint32_t u_w;
typedef uint16_t u_hw;
#define BIGNUM_DIGITS 4096
#define U_HW_BITS 16
#define U_W_BITS 32
#define U_HW_MAX UINT16_MAX
#define U_HW_MIN UINT16_MIN
#define U_W_MAX UINT32_MAX
#define U_W_MIN UINT32_MIN
#endif

typedef struct bn
{
        int sign;
        int n_digits; // #digits should exclude carry (digits = limbs)
        int carry;
        u_hw tab[BIGNUM_DIGITS];
} bn;

As I haven't written a procedure to write the bignum in decimal, I have to analyze the intermediate array and printf the values of each digit. However I don't know which conversion specifier to use with printf. Preferably I would like to write to the terminal the digit encoded in hexadecimal.

The underlying issue is, that I want two data types, one that is twice as long as the other, and further use them with printf using standard conversion specifiers. It would be ideal if int is 32bits and long is 64bits but I don't know how to guarantee this using a preprocessor, and when it comes time to use functions such as printf that solely rely on the standard types I no longer know what to use.

+2  A: 

You could use the macros from <inttypes.h> to help out:

#if defined(__LP64__) || defined(__amd64) || defined(__x86_64) || defined(__amd64__) || defined(__amd64__) || defined(_LP64)
typedef uint64_t u_w;
typedef uint32_t u_hw;
#define BIGNUM_DIGITS 2048
#define U_HW_BITS 16
#define U_W_BITS 32
#define U_HW_MAX UINT32_MAX
#define U_HW_MIN UINT32_MIN
#define U_W_MAX UINT64_MAX
#define U_W_MIN UINT64_MIN
#define PRI_U_HW  PRIu32    // use for formatting a `u_hw` type
#define PRI_U_W   PRIu64    // use for formatting a `u_w` type
#else
typedef uint32_t u_w;
typedef uint16_t u_hw;
#define BIGNUM_DIGITS 4096
#define U_HW_BITS 16
#define U_W_BITS 32
#define U_HW_MAX UINT16_MAX
#define U_HW_MIN UINT16_MIN
#define U_W_MAX UINT32_MAX
#define U_W_MIN UINT32_MIN
#define PRI_U_HW  PRIu16    // use for formatting a `u_hw` type
#define PRI_U_W   PRIu32    // use for formatting a `u_w` type
#endif

Then:

printf( "some u_w variable: %" PRI_U_W "\n", u_w_var);    
printf( "some u_hw variable: %" PRI_U_HW "\n", u_hw_var);

They aren't pretty, but they're how C99 does it.

Michael Burr
You mean to enclose the defines in double quotes?Anyways, handy tip. I thought this would be documented in the printf manpage, but I was wondering how C99 would deal with these types.Thanks.
nn
In the 1st part of the example where they're being defined, `PRI_U_HW` and `PRI_U_W` are simply aliases for the C99 `PRIuXX` values, which will be string literals. When you go to use them (as in the 2 `printf()` examples in the 2nd code snippet) you have to use them outside of quotes (they supply their own quotes) and rely on C's concatenation of adjacent string literals performed in 'phase 6' of translation. Like I said, it's kind of ugly.
Michael Burr
I see. By the way, do you think there's anyway to avoid this processor gunk and stick with int/long? I really just need to have two types, where one is at least double the width in size. I guess char and short would do the trick, but I'd prefer to use the larger datatypes such as int/long/long long if possible.
nn
@nn: I'd probably consider just sticking with uint32_t and uint64_t. I think that pretty much all 32-bit compilers support a 64-bit int type (even if it's not `long long`, for example VC6 has the __int64 type). But you'd have to be able to say that you're limiting support to compilers that have a 64-bit int type, which only you can say if that's acceptable.
Michael Burr
+1  A: 

ANSI C provides no guarantees about the sizes of int and long, and I don't think long long is an ANSI type. If you are unwilling or unable to use C99, the only safe, portable solution is to write a configure script that will create C programs that use sizeof to find a pair of integer types that have the property you require. You can then in that script generate macros including printf format macros.

It's also possible that the reason you're not using C99 is that you're porting to some wacky platform without a C99 compiler. In that case you can just figure out what works, slap it in a header, and not worry about portability.

C99 isn't pretty but it sure does solve some of these annoying C problems.

Norman Ramsey
Thanks for demystifying the process. I guess this is what most autoconf programs do. Would you suggest by hand, or have some tool do the hard work? I only know of autoconf and it seems complex to start a small project with.
nn
@nn: I run screaming from GNU autotools. I would suggest a POSIX sh script written by hand.
Norman Ramsey