How to Seamlessly Port C/C++ Applications from x86 to ARM64 TaiShan Servers

This guide shares practical experience and step‑by‑step solutions for migrating C/C++ and other language projects from x86 servers to ARM64 TaiShan platforms, covering compilation differences, code adjustments, compiler options, and performance tuning to achieve stable and efficient execution.

Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
How to Seamlessly Port C/C++ Applications from x86 to ARM64 TaiShan Servers

After migrating two projects from x86 servers to aarch64 TaiShan servers, the author summarizes the experience and provides a detailed migration guide.

Programming Language Overview

Languages can be divided into compiled and interpreted categories. Compiled languages such as C/C++ produce machine instructions that are architecture‑specific, while interpreted languages like Java and Python generate platform‑independent bytecode executed by a virtual machine.

Compiled C/C++ programs built for x86 cannot run directly on TaiShan servers and require a porting compilation; interpreted Java/Python programs can run unchanged, though native libraries (e.g., .so files) must be recompiled.

Preparation

Install a recent GCC (≥7.3, minimum 4.8.5). Download links: http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/ and installation guide https://gcc.gnu.org/install/ .

Common Porting Issues and Fixes

1.1 -m64 Compilation Option

Problem: gcc: error: unrecognized command line option ‘-m64’ on ARM64.

Cause: -m64 is an x86‑64 option.

Solution: Use -mabi=lp64 for ARM64.

1.2 char Signedness

Problem:

warning: comparison is always false due to limited range of data type

.

Cause: char is signed on x86 but unsigned on ARM64.

Solution: Add -fsigned-char to the compile flags.

2.1 Assembly Instruction Rewrite

ARM assembly differs from x86; all inline assembly must be rewritten for ARM64.

Example (x86):

Example (ARM64 using GCC built‑ins):

2.2 Replace x86 CRC32 Instructions

Problem: Unknown mnemonic crc32q on ARM64.

Solution: Use ARM64 CRC32 instructions ( crc32cb, crc32ch, crc32cw, crc32cx) and add -mcpu=generic+crc to the compile options.

Example (x86):

Example (ARM64):

2.3 Replace x86 bswap with ARM64 rev

Problem: Error: unknown mnemonic ‘bswap’.

Solution: Use rev on ARM64.

Illustration (x86):

Illustration (ARM64):

2.4 Replace x86 rep with ARM64 rept

Problem: unknown mnemonic ‘rep’.

Solution: Replace with rept as shown in the before/after images.

Before:

After:

2.5 Fast Porting of Inline SSE/SSE2

Use the open‑source sse2neon library ( https://github.com/open-estuary/sse2neon.git ) and follow the three‑step integration procedure described.

2.6 Weak Memory Ordering

ARM64’s weak memory model can cause unexpected results in lock‑free code; ensure proper memory barriers are used.

2.7 Atomic Operations on Misaligned Struct Members

ARM64 requires proper alignment for atomic instructions; remove or adjust #pragma pack usage that forces misalignment.

2.8 Hard‑coded Core Count

Search for hard‑coded CPU core numbers (e.g., in sched_setaffinity) and replace them with dynamic queries such as sysconf(_SC_NPROCESSORS_CONF).

2.9 Double‑to‑Integer Conversion Differences

Floating‑point to integer conversion behaves differently on x86 and TaiShan; follow the provided conversion tables to handle overflow and underflow consistently.

Compiler Optimization Options

4.1 GCC Floating‑Point Optimization

Using -O2 or higher enables fused multiply‑add ( fmadd) which can cause up to 16‑digit precision differences between x86 and ARM64.

Solution: Add -ffp-contract=off to disable this optimization.

4.2 Targeting Kunpeng Architecture

Add -march=armv8-a to generate code optimized for Kunpeng processors.

4.3 Tuning for Kunpeng Pipeline

For GCC ≥9.1, add -mtune=tsv110 to exploit the Kunpeng pipeline.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceCompiler Optimizationcode migrationARM64TaishanC++
Huawei Cloud Developer Alliance
Written by

Huawei Cloud Developer Alliance

The Huawei Cloud Developer Alliance creates a tech sharing platform for developers and partners, gathering Huawei Cloud product knowledge, event updates, expert talks, and more. Together we continuously innovate to build the cloud foundation of an intelligent world.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.