简体   繁体   English

新 Apple 芯片 (arm64) 与 x86_64 上 double 和 int64_t 转换的不同行为

[英]Different behavior of double and int64_t conversion on new Apple silicon (arm64) vs. x86_64

env: docker.环境:docker。 gcc10 in arm64 debian. arm64 debian 中的 gcc10。 gcc7 in x86_64 debian. x86_64 debian 中的 gcc7。

#include <cstdio>
#include <limits>
#include <cstdint>

int main(int args, char *argv[]) {
        double d = std::numeric_limits<int64_t>::max();
        int64_t t = static_cast<int64_t>(d);
        printf("%lld\n", t);
        return 0;
}

Output: Output:

  • arm64: 9223372036854775807 arm64:9223372036854775807
  • x86_64: -9223372036854775808 x86_64:-9223372036854775808

Can someone help me understand why there is a difference?有人可以帮我理解为什么会有区别吗?

The answer is the different behavior of the ARM architecture for overflows in the conversion (to int64_t in this case).答案是 ARM 架构在转换中溢出的不同行为(在本例中为 int64_t)。 It is documented here (for ARMv7): https://developer.arm.com/documentation/ddi0403/d/Application-Level-Architecture/Application-Level-Programmers--Model/The-optional-Floating-point-extension/Floating-point-data-types-and-arithmetic?lang=en它记录在这里(对于 ARMv7): https://developer.arm.com/documentation/ddi0403/d/Application-Level-Architecture/Application-Level-Programmers--Model/The-optional-Floating-point-extension/浮点数据类型和算术?lang=en

TL:DR: The maximum representable value is used on ARM (9223372036854775807 for int64_t). TL:DR:最大可表示值用于 ARM(9223372036854775807 用于 int64_t)。 (as @PeterCordes guessed) (正如@PeterCordes 猜测的那样)

EDIT: For x86 / x64 the overflow will result in an integer with just the MSB set (which equals -9223372036854775808 in case of int64_t).编辑:对于 x86 / x64,溢出将导致 integer 仅具有 MSB 集(在 int64_t 的情况下等于 -9223372036854775808)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM