Target 32- and 64-bit Platforms Together with a Few Simple Datatype Changes

Target 32- and 64-bit Platforms Together with a Few Simple Datatype Changes

n the early 1990s, 64-bit systems were considered “a solution waiting for a problem.” In 2005, however, this technology is rapidly gaining a critical mass of users. Even if you’re aiming at 32-bit platforms for the time being, adding 64-bit compliance to your coding checklist guarantees a smooth migration to 64-bit platforms in the future. This solution shows you how to write dually-targeted code.


How to write single-source code that can be deployed on both 32- and 64-bit environments without necessitating code rewriting?


Follow the 64-bit compliance coding guidelines.

When I’m 64
64-bit architectures offer several advantages:

  • Large file support: a large file is one that contains 2GB of data or more.
  • 64-bit addressing: a 64-bit process has a virtual address space of 16 exabytes; that’s about four billion times larger than the current upper limit of 4GB.
  • Breaking the 4GB RAM barrier: on 64-bit systems, the theoretical upper limit of physical RAM is 16 exabytes.
Author’s Note: The memory units used in 64-bit environments are:

  • 1 terabyte = 1024 gigabyte
  • 1 petabyte = 1024 terabytes = 1048576 gigabytes
  • 1 exabyte = 1024 petabytes = 1048576 terabytes=1,073,741,824 gigabytes

To benefit from these advantages (and others) you must recompile and relink the original source files using a 64-bit compiler and 64-bit libraries, respectively. The good news is that existing 32-bit executables will still run on a 64-bit kernel. The converse isn’t true; 64-bit binaries will only run on a 64-bit kernel.

ILP32 Versus LP64
The problems associated with writing 64-bit compliant programs stem from the different datatype models of 32-bit and 64-bit systems. Under the 32-bit datatype model known as ILP32, int, long, and pointers all have the same size of 32-bits. Under the 64-bit datatype model which is called LP64, int still occupies 32-bits, whereas long and pointers occupy 64-bits. The following table summarizes the differences between the two models with respect to the number of bits in each fundamental type:

C/C++ native type

ILP32

LP64

char

8

8

bool

usually the same as char

usually the same as char

short

16

16

int

32

32

long

32

64

long long

64

64

pointers (of data and functions)

32

64

enum

32

32

float

32

32

double

64

64

long double

64/80/128

128

Platform-independent Datatype Widths
When writing dually-targeted code, it is important to clearly define which objects should have a fixed width, regardless of the target architecture. Fixed-width datatypes may be required in the following cases:

  • Reading data from a network connection
  • On-disk data that was written by a 32-bit application
  • Interfacing with certain binary standards

For such fixed-width datatypes, use the standard typedefs defined in :

int8_t   int16_t   int32_t   int64_t //signed uint8_t   uint16_t   uint32_t   uint64_t //unsigned 

Strictly speaking, is a C99 header. However, most C++ compilers nowadays support it.

Automatic Mapping of Platform-dependent Datatypes
C and C++ define standard typedefs such as size_t, ptrdiff_t, fpos_t, time_t, etc., that abstract the underlying type of an object, thereby enabling the compiler to map them to the suitable native datatype automatically. Always use these typedefs in dually-targeted code. Take for example size_t. In ILP32 it’s defined as 32-bit unsigned integer. However, in LP64 it’s defined as an unsigned 64-bit integer.

High-level libraries such as usually offer a clean migration path because they use abstraction layers. For instance, the tellp() function returns a std::streampos object rather than an int. Likewise, fstream::write() is declared as follows:

typedef size_t streamsize;basic_ostream&  write(const char_type* s, streamsize n);

That said, you can still use fundamental types such as int and long as loop counters, array indexes, file descriptors, etc.

Pointer Issues
Several libraries cast pointers to integral types and vice versa. Examples of this include the standard function:

handler signal(int signum, handler); 

Here, handler may either be a pointer to a function or one of the two integral constant SIG_DFL and SIG_IGN. The POSIX library uses this idiom too in its dlsym() function. The problem is that a pointer’s size is platform-dependent; you certainly don’t want to store a 64-bit pointer in an int. The recommended approach is to use the intptr_t and unintptr_t typedefs as integral datatypes that can safely hold a pointer.

ABI IssuesAn Application Binary Interface (ABI) specifies the binary representation of a programming language’s entities, including the name mangling scheme, memory layout, and the default alignment. Consider the following struct:

struct Record{ long idx; bool cached;};

On a typical 32-bit system Record occupies eight bytes. However, on 64-bit systems its size increases to 12 or 16 bytes. Therefore, never assume that the size of a data structure is invariant. Even if it consists of fixed-width data members, the target platform’s alignment scheme may still affect its total size. Even abstract classes are subjected to ABI-issues:

class NetInterface{public: int virtual Connect(enum conn_type)=0; //..additional members virtual ~NetInterface()=0;};

NetInterface contains no overt data members. Yet as all polymorphic classes, it contains an implicit vptr whose size is platform dependent. Similarly, the member functions’ mangled names are also ABI-dependent. The upside is that accidentally linking 64-bit libraries with 32-bit object files (or vice versa) would fail noisily.

I/O Formatting
The printf() family of functions uses format flags that control the output’s justification, notation, and width. Always use the “%p” flag for pointers, the l- prefix for arguments of types, long and ul- for their unsigned counterparts. Check also that enough room is allowed for output when using either or . Finally, ensure that char buffers are large enough to accommodate up to 20 characters for unsigned long and 18 hex characters for pointer.

devx-admin

devx-admin

Share the Post:
Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and

Anthropic Investment

Amazon’s Bold Anthropic Investment

On Monday, Amazon announced its plan to invest up to $4 billion in the AI firm Anthropic, acquiring a minority stake in the process. This

AI Experts Get Hired

Tech Industry Rehiring Wave: AI Experts Wanted

A few months ago, Big Tech companies were downsizing their workforce, but currently, many are considering rehiring some of these employees, especially in popular fields

Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their Razer Blade 17 model. Typically

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations with industry professionals. The gathering,

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and Dell Technologies Inc. more time

Semiconductor Stock Plummet

Dramatic Downturn in Semiconductor Stocks Looms

Recent events show that the S&P Semiconductors Select Industry Index seems to be experiencing a downturn, which could result in a decline in semiconductor stocks. Known as a key indicator

Anthropic Investment

Amazon’s Bold Anthropic Investment

On Monday, Amazon announced its plan to invest up to $4 billion in the AI firm Anthropic, acquiring a minority stake in the process. This decision demonstrates Amazon’s commitment to

AI Experts Get Hired

Tech Industry Rehiring Wave: AI Experts Wanted

A few months ago, Big Tech companies were downsizing their workforce, but currently, many are considering rehiring some of these employees, especially in popular fields such as artificial intelligence. The

Lagos Migration

Middle-Class Migration: Undermining Democracy?

As the middle class in Lagos, Nigeria, increasingly migrates to private communities, a PhD scholar from a leading technology institute has been investigating the impact of this development on democratic

AI Software Development

ChatGPT is Now Making Video Games

Pietro Schirano’s foray into using ChatGPT, an AI tool for programming, has opened up new vistas in game and software development. As design lead at business finance firm Brex, Schirano

Llama Codebot

Developers! Here’s Your Chatbot

Meta Platforms has recently unveiled Code Llama, a free chatbot designed to aid developers in crafting coding scripts. This large language model (LLM), developed using Meta’s Llama 2 model, serves

Tech Layoffs

Unraveling the Tech Sector’s Historic Job Losses

Throughout 2023, the tech sector has experienced a record-breaking number of job losses, impacting tens of thousands of workers across various companies, including well-established corporations and emerging startups in areas

Chinese 5G Limitation

Germany Considers Limiting Chinese 5G Tech

A recent report has put forth the possibility that Germany’s Federal Ministry of the Interior and Community may consider limiting the use of Chinese 5G technology by local network providers

Modern Warfare

The Barak Tank is Transforming Modern Warfare

The Barak tank is a groundbreaking addition to the Israeli Defense Forces’ arsenal, significantly enhancing their combat capabilities. This AI-powered military vehicle is expected to transform the way modern warfare

AI Cheating Growth

AI Plagiarism Challenges Shake Academic Integrity

As generative AI technologies like ChatGPT become increasingly prevalent among students and raise concerns about widespread cheating, prominent universities have halted their use of AI detection software, such as Turnitin’s

US Commitment

US Approves Sustainable Battery Research

The US Department of Energy has revealed a $325 million commitment in the research of innovative battery types, designed to enable solar and wind power as continuous, 24-hour energy sources.

Netanyahu Musk AI

Netanyahu and Musk Discuss AI Future

On September 22, 2023, Israeli Prime Minister Benjamin Netanyahu met with entrepreneur Elon Musk in San Francisco prior to attending the United Nations. In a live-streamed discussion, Netanyahu lauded Musk

Urban Gardening

Creating Thriving Cities Through Urban Gardening

The rising popularity of urban gardening is receiving increased recognition for its numerous advantages, as demonstrated in a recent study featured in the Environmental Research Letters journal. Carried out by

What You Need to Know About Cloud Security Strategies

What You Need to Know About Cloud Security Strategies

Today, many businesses are adopting cloud computing services. As a result, it’s important to recognize that security measures for data in the cloud are different from those in traditional on-premises

Romanian Energy Security

Eastern Europe is Achieving Energy Security

Canada and Romania have solidified their commitment to energy security and independence from Russian energy exports by signing a $3-billion export development agreement. The deal is centered on constructing two

Seamless Integration

Unlocking Seamless Smart Home Integration

The vision of an intelligently organized and interconnected smart home that conserves time, energy, and resources has long been desired by many homeowners. However, this aspiration has often been hindered

New Algorithm

MicroAlgo’s Groundbreaking Algorithm

MicroAlgo Inc. has revealed the creation of a knowledge-augmented backtracking search algorithm, developed through extensive research in evolutionary computational techniques. The algorithm is designed to boost problem-solving effectiveness, precision, and

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles