Compiler
x86 vs x86-64 vs x64
https://www.ithome.com.tw/tech/56880 x86-64 = x64 -> 64bits x86 names from Intel 8086, refer to old 32bits processors
string_view analysis
https://quuxplusone.github.io/blog/2021/11/09/pass-string-view-by-value/ Analysis by asm
Godbolt: compiler explorer
https://godbolt.org/ A tool to observe how asm generate from code over different platform
Intel vs AT&T
AT&T is even more obscure than Intel https://staffwww.fullcoll.edu/aclifton/courses/cs241/syntax.html
i386
Intel 80386: introduced the 32-bit x86 architecture; its wide adoption shaped later x86 processors.
SIMD Instruction Sets
- MMX (MultiMedia eXtensions)
- SSE (Streaming SIMD Extensions)
- AVX (Advanced Vector Extensions)
AVX2 example: LeetCode 1822. Sign of the Product of an Array. Compare 8 ints with 0 at once, and XOR them to get the sign bit.
cpp
#include <immintrin.h>
__attribute__((target("avx2"))) int work(const int* arr, size_t len) {
if (len == 0) {
return 1;
}
const __m256i* simd_arr = (const __m256i*) arr;
__m256i zero = _mm256_setzero_si256();
__m256i s = _mm256_setzero_si256();
for (size_t i = 0; i < (len>>3); ++i) {
__m256i vec = _mm256_loadu_si256(simd_arr + i);
__m256i y = _mm256_cmpeq_epi32(vec, zero);
if (_mm256_movemask_epi8(y)) {
return 0;
}
s = _mm256_xor_si256(s, vec);
}
alignas(32) int val[8];
_mm256_store_si256((__m256i*) val, s);
int val2 = val[0]^val[1]^val[2]^val[3]^val[4]^val[5]^val[6]^val[7];
return (val2 >= 0) ? 1:-1;
}
class Solution {
public:
int arraySign(vector<int>& nums) {
size_t len = (nums.size()>>3)<<3;
int v = work(nums.data(), nums.size());
if (v == 0) {
return 0;
}
// 剩下不足 8 個的尾巴
for (size_t i = len; i < nums.size(); ++i) {
if (nums[i] == 0) {
return 0;
} else if (nums[i] < 0) {
v = -v;
}
}
return v;
}
};Reference
http://cs.brown.edu/courses/cs033/docs/guides/x64_cheatsheet.pdfhttps://www.cnblogs.com/lsgxeva/p/11176000.html