《Intel® 64体系结构内存排序白皮书》文档指出,对于“读取或写入地址对齐为8字节边界的四字(8字节)指令”,无论内存类型如何,内存操作都会执行为单个内存访问。
问题是:是否存在能够保证读取或写入16字节(128位)并对齐到16字节边界的Intel/AMD/x86 CPU能够执行单个内存访问?如果是这样,请说明是哪种特定类型的CPU(Core2 / Atom / K8 / Phenom / ...)?如果您对此问题提供了答案(是/否),请同时指定用于确定答案的方法 - PDF文档查找、暴力测试、数学证明或您用于确定答案的任何其他方法。
此问题涉及到诸如http://research.swtch.com/2010/02/off-to-races.html之类的问题。
更新:
我创建了一个简单的C语言测试程序,您可以在计算机上运行它。请编译并在您拥有的Phenom、Athlon、Bobcat、Core2、Atom、Sandy Bridge或任何支持SSE2指令集的CPU上运行它。谢谢。
// Compile with:
// gcc -o a a.c -pthread -msse2 -std=c99 -Wall -O2
//
// Make sure you have at least two physical CPU cores or hyper-threading.
#include <pthread.h>
#include <emmintrin.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
typedef int v4si __attribute__ ((vector_size (16)));
volatile v4si x;
unsigned n1[16] __attribute__((aligned(64)));
unsigned n2[16] __attribute__((aligned(64)));
void* thread1(void *arg) {
for (int i=0; i<100*1000*1000; i++) {
int mask = _mm_movemask_ps((__m128)x);
n1[mask]++;
x = (v4si){0,0,0,0};
}
return NULL;
}
void* thread2(void *arg) {
for (int i=0; i<100*1000*1000; i++) {
int mask = _mm_movemask_ps((__m128)x);
n2[mask]++;
x = (v4si){-1,-1,-1,-1};
}
return NULL;
}
int main() {
// Check memory alignment
if ( (((uintptr_t)&x) & 0x0f) != 0 )
abort();
memset(n1, 0, sizeof(n1));
memset(n2, 0, sizeof(n2));
pthread_t t1, t2;
pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
for (unsigned i=0; i<16; i++) {
for (int j=3; j>=0; j--)
printf("%d", (i>>j)&1);
printf(" %10u %10u", n1[i], n2[i]);
if(i>0 && i<0x0f) {
if(n1[i] || n2[i])
printf(" Not a single memory access!");
}
printf("\n");
}
return 0;
}
我笔记本电脑上的CPU是Core Duo(不是Core2)。这个特定的CPU未通过测试,它实现的16字节内存读写具有8字节的粒度。输出结果如下:
0000 96905702 10512
0001 0 0
0010 0 0
0011 22 12924 Not a single memory access!
0100 0 0
0101 0 0
0110 0 0
0111 0 0
1000 0 0
1001 0 0
1010 0 0
1011 0 0
1100 3092557 1175 Not a single memory access!
1101 0 0
1110 0 0
1111 1719 99975389