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

查看:153
本文介绍了使用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是"nicer")

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指令

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天全站免登陆