gpt4 book ai didi

javascript - 为什么原子操作没有逻辑否定?

转载 作者:行者123 更新时间:2023-12-04 14:13:07 25 4
gpt4 key购买 nike

JavaScript has no Atomics.not,但它确实有像Atomics.and这样的操作。

Rust also has no fetch_not,但它确实有像fetch_or这样的操作。

为什么这些语言没有原子逻辑否定运算符,而它们有其他常见操作?

最佳答案

fetch_or 的 x86 程序集如下所示(丢弃值时):

lock
or byte ptr [rsp], 51

这实际上是一条指令:lock 是修改下一条指令的指令前缀。具体来说,lock 使指令以原子方式执行。 Intel's x86 manual指定哪些指令支持 lock 前缀。 Bitwise not 支持lock前缀

This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically.

那么如果有 x86 指令,为什么没有原子操作呢? LLVM没有内在的。在 Rust 中,Atomic* 函数都直接转发到 LLVM 内在函数,因此只有具有相应 LLVM 内在函数的操作才能获得函数。 JavaScript 原子的内存模型 "takes significant inspiration from LLVM" ,因此猜测 Atomics 函数只是从 LLVM 拥有的函数复制而来似乎并不难。

LLVM supports 6 intrinsic operations : add, sub, and, or, xor, and 与非。它支持那些因为 GCC has intrinsic operations with the same names (类型除外)。原子 andsubandorxor 操作 were added to GCC ( nand was added later ) 作为在 C++11 中添加对 std::atomic 支持的一部分。

C++11 在 std::atomic 中有 5 个按位原子操作:add、sub、and、or、xor。 (但不是 nand,它在 GCC 中获得了内在特性,但在 std::atomic 中没有实际支持)。为什么?因为这就是 2007 paper 中的建议最初在 C++ 中提出原子,它定义了操作“fetch-and-{add,sub,and,or,xor}”。他们没有提供仅具有这 5 个操作的原因,但我的猜测是他们希望最大程度地减少编译器实现者的实现工作量,因为您可以通过对所有操作进行异或运算来获得 NOT 操作。或者也许他们只是没有考虑过。

关于javascript - 为什么原子操作没有逻辑否定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62738528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com