Keywords: quad

Next: 3 Portability Macros Up: Quad Integer Library Reference Previous: 1 Introduction Contents
Subsections

- 2.1 Basic Integer Operations
- 2.2 Comparisons
- 2.3 Type Conversions
- 2.4 Rules

# 2 Issues

In this section I will attempt to explain the issues involved in using this library to write a more portable (with respect to quad_t operations) program.

## 2.1 Basic Integer Operations

The most obvious thing to keep in mind is simple arithmetic operations. On a compiler that does not have a builtin quad_t, the Quad Integer Library will create a quad_t type that is a structure composed of two long integers. The compiler does not understand that we wish to use this type as a long long integer, it only knows that it is a structure. To write code that adds two structures means nothing to the compiler and and it will readily complain about it. The solution is to wrap all arithmetic (and bitwise) operations with macros supplied by this library. This is simple enough to do. Take for example this code.

        quad_t x, a, b;
        x = a + b;

This code is wrong, a compiler with no native quad_t has no clue what to do with this. The addition should be wrapped like this:

        quad_t x, a, b;
        x = QADD( a, b );

This snippet will compile and will work optimally on both a compiler that have a native quad_t and one that doesn't.

All of the arithmetic and bitwise operators that are available in C for standard integer types have corresponding macros that work in this manner.

## 2.2 Comparisons

Comparing two quad_t's is also something to look out for. The compiler with no quad_t also has no idea how to compare two quad_t's, so you must also wrap these operations. Take the following snippet:

        void test( quad_t a, quad_t b )
        {
          if( a == b )
            printf(``a equals b\n'');
          else if( a > b )
            printf(``a greater than b\n'');
          else if( a < b )
            printf(``a less than b\n'');
        }

The above should be rewritten like this, to allow it to be compiled on all compilers.

        void test( quad_t a, quad_t b )
        {
          if( QEQ(a, b) )
            printf(``a equals b\n'');
          else if( QGT(a, b) )
            printf(``a greater than b\n'');
          else if( QLT(a, b) )
            printf(``a less than b\n'');
        }

## 2.3 Type Conversions

The automatic conversion of types could be viewed as a wonderfully flexible feature of C, but when it comes to introducing abstract integer-like types this feature makes programmers expect a lot. Unfortunately there is not much we can do other than supply a set of macros to convert between types.

In C, it is very common to see the following code:

        int a = 5;
        long b;
        b = a + 99;

This is perfectly valid code when using int's and long's but not when using quad_t's. Similar code using quad_t's would look like this:

        int a = 5;
        long b;
        quad_t q;
        q = QADD( LTOQ(a), LTOQ(99) ); // This will promote a and
                                       // 99 to quad_t and then
                                       // perform the addition
                                       // and assignment.
        b = QTOL(q); // This will truncate q to fit into a long.

## 2.4 Rules

If you follow these rules, and understand fully that quad_t is not just another integer type, you will be able to create perfectly portable(with respect to quad_t, anyway) programs.

- Never assign anything to a quad_t except a quad_t.
- Always pass/return integers of the proper type to and from functions.
- Do not use C's built-in arithmetic, comparison or bitwise operators for operations involving quad_t's.
- Do not assume a quad_t has the same boolean properties as standard integer types. Always convert using QTOBOOL.
- Never use [s]printf()'s %lld option for reading/writing quad_t's. It will not exist on compilers with no quad_t.
- Never assume a integer type will be automatically promoted or truncated to or from a quad_t.

Next: 3 Portability Macros Up: Quad Integer Library Reference Previous: 1 Introduction Contents