在 aarch64/arm64 上使用环绕将 float 转换为 int [英] Casting float to int with wrap-around on aarch64/arm64

查看:63
本文介绍了在 aarch64/arm64 上使用环绕将 float 转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 x86_64 和 aarch64/arm64 上运行的应用程序之间完全匹配行为.但是,当浮点数超出可能的整数范围时,它们的不同之处在于它们如何将浮点数转换为整数.

I'm trying to match the behavior exactly between an application running on both x86_64 and aarch64/arm64. However, they differ in how they cast a floating point number to an integer when it's outside of the possible range of integers.

考虑以下示例:

#include <stdio.h>
#include <cstdint>

void cast(float value) {
  printf("uint32_t(%.2f) = %u\n", value, uint32_t(value));
}

int main() {
  cast(4294967808.);
}

# output on x86_64:  uint32_t(4294967808.00) = 512
# output on aarch64: uint32_t(4294967808.00) = 4294967295

x86_64 版本使用 cvttss2si 进行转换,它包含了答案,尽管文档对此非常不清楚.Aarch64 正在使用 fcvtzu 饱和了.

The x86_64 version is using cvttss2si for the conversion, which wraps-around the answer, although the documentation is quite unclear on this. Aarch64 is using fcvtzu which is saturating.

任何将两者对齐的解决方案都会很有趣,但理想情况下,我想在 clang 上设置一个编译器标志,以使 aarch64 版本的行为类似于 x86_64 版本(即使 aarch64更好")

Any solution to align the two would be interesting, but ideally I'd like to set a compiler flag on clang to have the aarch64 version behave like the x86_64 one (even though the aarch64 is "nicer")

推荐答案

使用 CPU 指令 fjcvtzs(或内在的 __builtin_arm_jcvt)来获取 x86 在 aarch64 上的行为.

Use the CPU instruction fjcvtzs (or the intrinsic __builtin_arm_jcvt) to get behavior of x86 on aarch64.

(感谢@EOF 在评论中提供足够的信息让我找到答案)

(Thanks to @EOF for providing enough information in a comment for me to find the answer)

这篇关于在 aarch64/arm64 上使用环绕将 float 转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆