Currently TiDB use below code to in LogicalNot:
goldbolt it:
package not
func Not(i64s []int64) {
n := len(i64s)
for i := 0; i < n; i++ {
if i64s[i] == 0 {
i64s[i] = 1
} else {
i64s[i] = 0
}
}
}
Not_pc14:
INCQ DX
Not_pc17:
CMPQ AX, DX
JLE Not_pc51
MOVQ (CX)(DX*8), BX
TESTQ BX, BX
JNE Not_pc41
MOVQ $1, (CX)(DX*8)
JMP Not_pc14
Not_pc41:
MOVQ $0, (CX)(DX*8)
JMP Not_pc14
Not_pc51:
RET
A C++ version with -O3
C++ can convert bool to int64_t, and have defined behaviour.
#include <vector>
#include <cstdint>
int Not(std::vector<int64_t>& i64s) {
int n = i64s.size();
for (int i = 0; i < n; i++) {
i64s[i] = static_cast<int64_t>((i64s[i] == 0));
}
}
Not(std::vector<long, std::allocator<long> >&):
mov rax, QWORD PTR [rdi]
.L2:
xor edx, edx
cmp QWORD PTR [rax], 0
sete dl
add rax, 8
mov QWORD PTR [rax-8], rdx
jmp .L2