iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔖

Accessing Arm64 (AArch64) FPCR from C

に公開
2024/11/21

To change behavior related to floating-point numbers in Arm64 (AArch64), you access the FPCR (Floating-Point Control Register). Typical items controlled by the FPCR include rounding modes, but there are various others, such as trap control, enabling flush-to-zero for denormal numbers, and changing how NaN payloads are propagated.

In the era of 32-bit Arm (AArch32), status flags for floating-point exceptions were also managed in the same register, called FPSCR (Floating-Point Status and Control Register). However, with the transition to 64-bit, the status flags were separated into a different register called FPSR.

While the Arm64 FPCR is 64 bits wide, the parts currently used are contained within the lower 32 bits as of the time of writing, with the upper 32 bits being reserved.

ACLE (Arm C Language Extensions)

In environments supporting ACLE (Arm's C extensions), you can access special registers including the FPCR using intrinsics like __arm_rsr64 and __arm_wsr64. The register name is provided as a string argument.

As of the time of writing, Clang supports these intrinsics. GCC supports them from version 14 onwards. The status of MSVC is unknown.

Usage example:

#include <arm_acle.h>

__attribute__((always_inline))
uint64_t get_fpcr(void)
{
    return __arm_rsr64("fpcr");
}
__attribute__((always_inline))
void set_fpcr(uint64_t x)
{
    __arm_wsr64("fpcr", x);
}

FPSR can also be accessed in the same way.

[Addendum] Upon re-reading ACLE, it seems it doesn't explicitly state that specifying the name "fpcr" is allowed. It might be a case of "it works in Clang, so it's fine!", but I'm not sure how it should be interpreted.

GCC

Up to GCC 13, ACLE support was partial, and specifically, __arm_rsr64 and __arm_wsr64 were not supported. Instead, GCC provides built-in functions for accessing FPCR and FPSR.

__builtin_aarch64_{get,set}_fpcr64, which allows access to the full 64-bit width, was added in GCC 11. Before that, only the lower 32 bits of the FPCR were accessible via __builtin_aarch64_{get,set}_fpcr. __builtin_aarch64_{get,set}_fpcr was implemented in GCC 5.

By the way, since GCC 10 and later support __has_builtin, you can use it to detect __builtin_aarch64_{get,set}_fpcr64.

Usage example:

__attribute__((always_inline))
uint64_t get_fpcr(void)
{
#if __GNUC__ >= 11
    return __builtin_aarch64_get_fpcr64();
#elif __GNUC__ >= 5
    return (uint64_t)__builtin_aarch64_get_fpcr();
#else
#error unsupported compiler
#endif
}
__attribute__((always_inline))
void set_fpcr(uint64_t x)
{
#if __GNUC__ >= 11
    __builtin_aarch64_set_fpcr64(x);
#elif __GNUC__ >= 5
    __builtin_aarch64_set_fpcr((unsigned long)x);
#else
#error unsupported compiler
#endif
}

Inline Assembly

Inline assembly is a method that can be used with both GCC and Clang. Since I used this for Clang before I learned about the existence of __arm_rsr64 and __arm_wsr64, I will leave it here as a memorial.

__attribute__((always_inline))
uint64_t get_fpcr(void)
{
    uint64_t fpcr;
    asm volatile("mrs %0, fpcr" : "=r"(fpcr));
    return fpcr;
}
__attribute__((always_inline))
void set_fpcr(uint64_t x)
{
    asm volatile("msr fpcr, %0" : : "r"(x));
}

It is quite a hassle when there is no common method across compilers. Someone should really do something about this.

Discussion