http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47357
Summary: volatile member function is not treated as volatile Product: gcc Version: 4.5.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: yot...@yahoo.co.jp When I execute the following test code with '-O2 -msse2' compiler options, it outputs the wrong value. I'm expecting that pC[1] will be 2. operator[](int) is defined as volatile. However v is not treated as volatile. And I tried (volatile const u &) instead of (const u &) when the casting, but it's not affected. -- begin test case -- //g++ -O2 -msse2 test.cpp #include <stdio.h> #include <stdint.h> #include <emmintrin.h> #define NELEM 1 struct int4 { __m128i v; int4(__m128i a) : v(a) {} int4(int a0, int a1, int a2, int a3) : v(_mm_setr_epi32(a0, a1, a2, a3)) {} operator __m128i() const { return v; } int operator [](int i) volatile const { union u { __m128i m128; int i32[4]; }; return ((const u &)v).i32[i]; } }; inline int4 operator <<(const int4 & a, const int4 & b) { return int4(a[0]<<b[0], a[1]<<b[1], a[2]<<b[2], a[3]<<b[3]); } __attribute__((noinline)) void shift_left( int n, const __m128i a[], const __m128i b[], __m128i c[]) { for ( int j = 0; j < n; ++j ) { c[j] = int4(a[j]) << int4(b[j]); } } int main( int argc, char * argv[] ) { __m128i * a = new __m128i[NELEM+1]; __m128i * b = new __m128i[NELEM+1]; __m128i * c = new __m128i[NELEM+1]; a = (__m128i *)(-sizeof(*a) & (intptr_t)&a[1]); b = (__m128i *)(-sizeof(*b) & (intptr_t)&b[1]); c = (__m128i *)(-sizeof(*c) & (intptr_t)&c[1]); for ( int i = 0; i < NELEM*4; ++i ) { ((int *)a)[i] = i; ((int *)b)[i] = i & 31; ((int *)c)[i] = 0; } shift_left(NELEM, a, b, c); const int * pA = (const int *)a; const int * pB = (const int *)b; const int * pC = (const int *)c; for ( int i = 0; i < NELEM*4; ++i ) { printf("%08x << %08x = %08x\n", pA[i], pB[i], pC[i]); } } -- end test case -- -- begin output -- 00000000 << 00000000 = 58a00000 00000001 << 00000001 = e9000000 00000002 << 00000002 = b5f40000 00000003 << 00000003 = 00000000 -- end output --