|
|
White Paper: 64-bit
Versus 32-bit AddressingWhat
does a programmer need to know about 64-bit addressing on Alpha versus 32-bit addressing
on SPARC? To write portable code as well as code that runs in a mixed 64- and 32-bit
environment, you need to understand how the different addressing modes affect files
and file systems, data representation, data alignment, constants, structures, variables,
and library calls.
The SunOS to DEC
OSF/1 Porting Guide describes in detail how to write portable code that accommodates
the 64-bit versus 32-bit differences. This paper highlights the major impacts. See the
porting guide for further information.
Note |
While you port the source code, run
translated versions of your applications on DIGITAL UNIX. FreePort Express and the
run-time environment automatically make the adjustments necessary to run 32-bit programs
on a 64-bit system. |
File
systems
On a DIGITAL UNIX
system you can create much larger files and file systems than you can on a SunOS SPARC
system. When working in a mixed 64- and 32-bit environment, be aware that SunOS systems
can't fully access files or file systems built to utilize 64-bit addressing.
Data
representation
The size in bits of C
language data types on DIGITAL UNIX and SunOS systems is the same with the exception of
the long and pointer data types. On SunOS, long and pointer
data types are 32 bits. On DIGITAL UNIX, long and pointer data types are
64 bits. The size difference between long and pointer DIGITAL UNIX and
SunOS SPARC systems affects data alignment and structures.
Natural data
alignment
When using the long
and pointer data types in structure definitions shared between 64- and 32-bit
systems, you need to understand the natural alignment differences between the two types of
systems. Table 1 shows the alignment differences.
Table 1 Natural
data alignment
Data Type |
Byte multiples |
|
SunOS SPARC |
DIGITAL UNIX |
char |
1 |
1 |
short |
2 |
2 |
int |
4 |
4 |
long |
4 |
8 |
long long |
8 |
8 |
float |
4 |
4 |
double |
8 |
8 |
pointer |
4 |
8 |
Higher level
language compilers like C automatically align data and variables to their natural
boundaries. But if you use assembly language, you need to code to the alignment
restrictions described below.
Structures
The 64-bit size of
the long and pointer data types affects the size of a structure, its
alignment, and its bit field declarations as follows:
Size |
The size of structures and unions on
DIGITAL UNIX systems can be different from those on 32-bit SunOS SPARC systems. In a mixed
environment, be careful about using long and pointer data types as members in shared
structures. These DIGITAL UNIX data types introduce sizes that are not available on 32-bit
systems. |
Structure and member alignment |
On DIGITAL UNIX systems, structure
members are padded to ensure they align on their natural boundaries: char on a byte
boundary, short on a word boundary, int on a longword boundary, and long and pointer on
quadword boundaries. In turn, the structures themselves are aligned according to the
strictest aligned member. To ensure this alignment, DIGITAL UNIX uses padding within or at
the end of structures. Guidelines
Always
use the sizeof operator to determine the size of a structure. You cannot assume that the
size of a structure is equal to the sum of its members.
Order
members within a structure in a way that minimizes the amount of padding needed to ensure
proper alignment. |
Bit fields |
In a C structure declaration, adjacent
bit fields are packed together in the previous bit field's unit on both 64- and 32-bit
systems. But because a DIGITAL UNIX long is 64 rather than 32 bits long, adjacent
declarations of bit fields of type long on DIGITAL UNIX might differ from the equivalent
declarations on SunOS SPARC and lead to different results. To ensure the same behavior on
both systems, change bit field definitions of type long to type int.
|
Integer and
long constants
If you are exchanging
data between 64- and 32-bit systems, some constants can have different values depending on
the type of system and consequently the behavior of some operators can change. The SunOS
to DIGITAL UNIX Porting Guide discusses ways to assign values and pass arguments to
minimize such errors. The guide also describes how to perform bit shift operations that
achieve the intended result.
Variables
The 64-bit DIGITAL
UNIX environment changes assumptions about how you declare your variables and how you use
them in assignments and in function arguments.
Declarations |
When an application must run on both
64-bit and 32-bit systems, define your int and long declarations as follows. For a
variable that needs to be 32 bits in size on both DIGITAL UNIX on SunOS systems, define
the type to be int. For a variable that should be 32 bits on SunOS systems but 64 bits on
DIGITAL UNIX, define the variable to be long. |
Assignments and function
arguments |
Variables of type pointer, int, and
long are not the same size on DIGITAL UNIX as they are on SunOS SPARC. Unless you follow
certain guidelines, you can encounter problems depending on how the variables are assigned
and used in your application. The SunOS to DIGITAL UNIX Porting Guide lists these
guidelines. |
Other variable differences |
Several other differences in variables
between DIGITAL UNIX and SunOS SPARC include: sizeof operator -- The result of
the sizeof operator is of type size_t, which is an unsigned long on DIGITAL UNIX.
Pointer
subtraction -- On DIGITAL UNIX, the length of the integer required to hold the
difference between two pointers to members of the same array, ptrdiff_t (stddef.h), is a
signed long.
Functions
with a variable number of arguments -- When writing a routine that receives a variable
(context-dependent) number of arguments you must use the stdargs (stdarg.h) or varargs
(varargs.h) mechanism. Also, there are some uses of vararg that are nonportable. |
Library
calls
The 64-bit data types
also affect some library calls. The SunOS to DIGITAL UNIX Porting Guide describes
how to use assignments correctly with these functions to ensure you don't lose information
or otherwise achieve the wrong results. The affected functions are: printf and scanf,
malloc and calloc, lseek, and fsetpos and fgetpos.
If you have
questions about FreePort Express, send email to fpx-info@scrugs.lkg.dec.com.
|