Guidelines for Converting to LP64
When using lint(1), remember that not all problems result in lint(1) warnings, nor do all lint(1) warnings indicate that a change is required. Examine each possibility for intent. The examples that follow illustrate some of the more common problems you are likely to encounter when converting code. Where appropriate, the corresponding lint(1) warnings are shown.
Do Not Assume int and Pointers Are the Same Size
Since ints and pointers are the same size in the ILP32 environment, a lot of code relies on this assumption. Pointers are often cast to int or unsigned int for address arithmetic. Instead, pointers could be cast to long because long and pointers are the same size in both ILP32 and LP64 worlds. Rather than explicitly using unsigned long, use uintptr_t because it expresses the intent more closely and makes the code more portable, insulating it against future changes. For example,
char *p; p = (char *) ((int)p & PAGEOFFSET); |
produces the warning:
warning: conversion of pointer loses bits |
Using the following code will produce the clean results:
char *p; p = (char *) ((uintptr_t)p & PAGEOFFSET); |
Do Not Assume int and long Are the Same Size
Because ints and longs were never really distinguished in ILP32, a lot of existing code uses them indiscriminately while implicitly or explicitly assuming that they are interchangeable. Any code that makes this assumption must be changed to work for both ILP32 and LP64. While an int and a long are both 32-bits in the ILP32 data model, in the LP64 data model, a long is 64-bits. For example,
int waiting; long w_io; long w_swap; ... waiting = w_io + w_swap; |
produces the warning:
warning: assignment of 64-bit integer to 32-bit integer |
Sign Extension
Sign extension is a common problem when converting to 64-bits. It is hard to detect before the problem actually occurs because lint(1) does not warn you about it. Furthermore, the type conversion and promotion rules are somewhat obscure. To fix sign extension problems, you must use explicit casting to achieve the intended results.
To understand why sign extension occurs, it helps to understand the conversion rules for ANSI C. The conversion rules that seem to cause the most sign extension problems between 32-bit and 64-bit integral values are:
Integral promotion
A char, short, enumerated type, or bit-field, whether signed or unsigned, can be used in any expression that calls for an int. If an int can hold all possible values of the original type, the value is converted to an int. Otherwise, it is converted to an unsigned int.
Conversion between signed and unsigned integers
When a negative signed integer is promoted to an unsigned integer of the same or larger type, it is first promoted to the signed equivalent of the larger type, then converted to the unsigned value.
For a more detailed discussion of the conversion rules, refer to the ANSI C standard. Also included in this standard are useful rules for ordinary arithmetic conversions and integer constants.
When compiled as a 64-bit program, the addr variable in the following example becomes sign-extended, even though both addr and a.base are unsigned types.
Example 4-1 test.c
This sign extension occurs because the conversion rules are applied as follows:
a.base is converted from an unsigned int to an int because of the integral promotion rule. Thus, the expression a.base << 13 is of type int, but no sign extension has yet occurred.
The expression a.base << 13 is of type int, but it is converted to a long and then to an unsigned long before being assigned to addr, because of the signed and unsigned integer promotion rule. The sign extension occurs when it is converted from an int to a long.
% cc -o test64 -xarch=v9 test.c % ./test64 addr 0xffffffff80000000 addr 0x80000000 % |
When this same example is compiled as a 32-bit program it does not display any sign extension:
% cc -o test32 test.c % ./test32 addr 0x80000000 addr 0x80000000 % |
Use Pointer Arithmetic Instead of Address Arithmetic
In general, using pointer arithmetic works better than address arithmetic because pointer arithmetic is independent of the data model, whereas address arithmetic might not be. It usually leads to simpler code as well. For example,
int *end; int *p; p = malloc(4 * NUM_ELEMENTS); end = (int *)((unsigned int)p + 4 * NUM_ELEMENTS); |
produces the warning:
warning: conversion of pointer loses bits |
The following code will produce clean results:
int *end; int *p; p = malloc(sizeof (*p) * NUM_ELEMENTS); end = p + NUM_ELEMENTS; |
Repack Structures
Extra padding may be added to a structure by the compiler to meet alignment requirements as long and pointer fields grow to 64 bits for LP64. In the 64-bit environment on SPARC platforms, all types of structures are aligned to at least the size of the largest quantity within them. A simple rule for repacking a structure is to move the long and pointer fields to the beginning of the structure and rearrange the rest of the fields--usually, but not always, in descending order of size, depending on how well they can be packed. For example,
struct bar { int i; long j; int k; char *p; }; /* sizeof (struct bar) = 32 */ |
For better results, use:
struct bar { char *p; long j; int i; int k; }; /* sizeof (struct bar) = 24 */ |