static void do_hi (void) { if (__sync_fetch_and_add(AL+4, 1) != 0) abort (); if (__sync_fetch_and_add(AL+5, 4) != 0) abort (); if (__sync_fetch_and_add(AL+6, 22) != 0) abort (); if (__sync_fetch_and_sub(AL+7, 12) != 0) abort (); if (__sync_fetch_and_and(AL+8, 7) != -1) abort (); if (__sync_fetch_and_or(AL+9, 8) != 0) abort (); if (__sync_fetch_and_xor(AL+10, 9) != 0) abort (); if (__sync_fetch_and_nand(AL+11, 7) != -1) abort (); if (__sync_add_and_fetch(AL+12, 1) != 1) abort (); if (__sync_sub_and_fetch(AL+13, 12) != -12) abort (); if (__sync_and_and_fetch(AL+14, 7) != 7) abort (); if (__sync_or_and_fetch(AL+15, 8) != 8) abort (); if (__sync_xor_and_fetch(AL+16, 9) != 9) abort (); if (__sync_nand_and_fetch(AL+17, 7) != ~7) abort (); }
static void do_qi (void) { if (__sync_fetch_and_add(AI+4, 1) != 0) abort (); if (__sync_fetch_and_add(AI+5, 4) != 0) abort (); if (__sync_fetch_and_add(AI+6, 22) != 0) abort (); if (__sync_fetch_and_sub(AI+7, 12) != 0) abort (); if (__sync_fetch_and_and(AI+8, 7) != (char)-1) abort (); if (__sync_fetch_and_or(AI+9, 8) != 0) abort (); if (__sync_fetch_and_xor(AI+10, 9) != 0) abort (); if (__sync_fetch_and_nand(AI+11, 7) != (char)-1) abort (); if (__sync_add_and_fetch(AI+12, 1) != 1) abort (); if (__sync_sub_and_fetch(AI+13, 12) != (char)-12) abort (); if (__sync_and_and_fetch(AI+14, 7) != 7) abort (); if (__sync_or_and_fetch(AI+15, 8) != 8) abort (); if (__sync_xor_and_fetch(AI+16, 9) != 9) abort (); if (__sync_nand_and_fetch(AI+17, 7) != (char)~7) abort (); }
void test_fetch_and_op (void) { sc = __sync_fetch_and_add (&sc, 11); uc = __sync_fetch_and_add (&uc, 11); ss = __sync_fetch_and_add (&ss, 11); us = __sync_fetch_and_add (&us, 11); si = __sync_fetch_and_add (&si, 11); ui = __sync_fetch_and_add (&ui, 11); sll = __sync_fetch_and_add (&sll, 11); ull = __sync_fetch_and_add (&ull, 11); sc = __sync_fetch_and_sub (&sc, 11); uc = __sync_fetch_and_sub (&uc, 11); ss = __sync_fetch_and_sub (&ss, 11); us = __sync_fetch_and_sub (&us, 11); si = __sync_fetch_and_sub (&si, 11); ui = __sync_fetch_and_sub (&ui, 11); sll = __sync_fetch_and_sub (&sll, 11); ull = __sync_fetch_and_sub (&ull, 11); sc = __sync_fetch_and_or (&sc, 11); uc = __sync_fetch_and_or (&uc, 11); ss = __sync_fetch_and_or (&ss, 11); us = __sync_fetch_and_or (&us, 11); si = __sync_fetch_and_or (&si, 11); ui = __sync_fetch_and_or (&ui, 11); sll = __sync_fetch_and_or (&sll, 11); ull = __sync_fetch_and_or (&ull, 11); sc = __sync_fetch_and_xor (&sc, 11); uc = __sync_fetch_and_xor (&uc, 11); ss = __sync_fetch_and_xor (&ss, 11); us = __sync_fetch_and_xor (&us, 11); si = __sync_fetch_and_xor (&si, 11); ui = __sync_fetch_and_xor (&ui, 11); sll = __sync_fetch_and_xor (&sll, 11); ull = __sync_fetch_and_xor (&ull, 11); sc = __sync_fetch_and_and (&sc, 11); uc = __sync_fetch_and_and (&uc, 11); ss = __sync_fetch_and_and (&ss, 11); us = __sync_fetch_and_and (&us, 11); si = __sync_fetch_and_and (&si, 11); ui = __sync_fetch_and_and (&ui, 11); sll = __sync_fetch_and_and (&sll, 11); ull = __sync_fetch_and_and (&ull, 11); sc = __sync_fetch_and_nand (&sc, 11); uc = __sync_fetch_and_nand (&uc, 11); ss = __sync_fetch_and_nand (&ss, 11); us = __sync_fetch_and_nand (&us, 11); si = __sync_fetch_and_nand (&si, 11); ui = __sync_fetch_and_nand (&ui, 11); sll = __sync_fetch_and_nand (&sll, 11); ull = __sync_fetch_and_nand (&ull, 11); }
void test_op_ignore (void) { (void) __sync_fetch_and_add (&sc, 1); (void) __sync_fetch_and_add (&uc, 1); (void) __sync_fetch_and_add (&ss, 1); (void) __sync_fetch_and_add (&us, 1); (void) __sync_fetch_and_add (&si, 1); (void) __sync_fetch_and_add (&ui, 1); (void) __sync_fetch_and_add (&sll, 1); (void) __sync_fetch_and_add (&ull, 1); (void) __sync_fetch_and_sub (&sc, 1); (void) __sync_fetch_and_sub (&uc, 1); (void) __sync_fetch_and_sub (&ss, 1); (void) __sync_fetch_and_sub (&us, 1); (void) __sync_fetch_and_sub (&si, 1); (void) __sync_fetch_and_sub (&ui, 1); (void) __sync_fetch_and_sub (&sll, 1); (void) __sync_fetch_and_sub (&ull, 1); (void) __sync_fetch_and_or (&sc, 1); (void) __sync_fetch_and_or (&uc, 1); (void) __sync_fetch_and_or (&ss, 1); (void) __sync_fetch_and_or (&us, 1); (void) __sync_fetch_and_or (&si, 1); (void) __sync_fetch_and_or (&ui, 1); (void) __sync_fetch_and_or (&sll, 1); (void) __sync_fetch_and_or (&ull, 1); (void) __sync_fetch_and_xor (&sc, 1); (void) __sync_fetch_and_xor (&uc, 1); (void) __sync_fetch_and_xor (&ss, 1); (void) __sync_fetch_and_xor (&us, 1); (void) __sync_fetch_and_xor (&si, 1); (void) __sync_fetch_and_xor (&ui, 1); (void) __sync_fetch_and_xor (&sll, 1); (void) __sync_fetch_and_xor (&ull, 1); (void) __sync_fetch_and_and (&sc, 1); (void) __sync_fetch_and_and (&uc, 1); (void) __sync_fetch_and_and (&ss, 1); (void) __sync_fetch_and_and (&us, 1); (void) __sync_fetch_and_and (&si, 1); (void) __sync_fetch_and_and (&ui, 1); (void) __sync_fetch_and_and (&sll, 1); (void) __sync_fetch_and_and (&ull, 1); (void) __sync_fetch_and_nand (&sc, 1); (void) __sync_fetch_and_nand (&uc, 1); (void) __sync_fetch_and_nand (&ss, 1); (void) __sync_fetch_and_nand (&us, 1); (void) __sync_fetch_and_nand (&si, 1); (void) __sync_fetch_and_nand (&ui, 1); (void) __sync_fetch_and_nand (&sll, 1); (void) __sync_fetch_and_nand (&ull, 1); }
/* Now check return values. */ static void do_ret_di (void) { if (__sync_val_compare_and_swap (AL+0, 0x100000002ll, 0x1234567890ll) != 0x100000002ll) abort (); if (__sync_bool_compare_and_swap (AL+1, 0x200000003ll, 0x1234567890ll) != 1) abort (); if (__sync_lock_test_and_set (AL+2, 1) != 0) abort (); __sync_lock_release (AL+3); /* no return value, but keep to match results. */ /* The following tests should not change the value since the original does NOT match. */ if (__sync_val_compare_and_swap (AL+4, 0x000000002ll, 0x1234567890ll) != 0x100000002ll) abort (); if (__sync_val_compare_and_swap (AL+5, 0x100000000ll, 0x1234567890ll) != 0x100000002ll) abort (); if (__sync_bool_compare_and_swap (AL+6, 0x000000002ll, 0x1234567890ll) != 0) abort (); if (__sync_bool_compare_and_swap (AL+7, 0x100000000ll, 0x1234567890ll) != 0) abort (); if (__sync_fetch_and_add (AL+8, 1) != 0) abort (); if (__sync_fetch_and_add (AL+9, 0xb000e0000000ll) != 0x1000e0de0000ll) abort (); if (__sync_fetch_and_sub (AL+10, 22) != 42) abort (); if (__sync_fetch_and_sub (AL+11, 0xb000e0000000ll) != 0xc001c0de0000ll) abort (); if (__sync_fetch_and_and (AL+12, 0x300000007ll) != -1ll) abort (); if (__sync_fetch_and_or (AL+13, 0x500000009ll) != 0) abort (); if (__sync_fetch_and_xor (AL+14, 0xe00000001ll) != 0xff00ff0000ll) abort (); if (__sync_fetch_and_nand (AL+15, 0xa00000007ll) != -1ll) abort (); /* These should be the same as the fetch_and_* cases except for return value. */ if (__sync_add_and_fetch (AL+16, 1) != 1) abort (); if (__sync_add_and_fetch (AL+17, 0xb000e0000000ll) != 0xc001c0de0000ll) abort (); if (__sync_sub_and_fetch (AL+18, 22) != 20) abort (); if (__sync_sub_and_fetch (AL+19, 0xb000e0000000ll) != 0x1000e0de0000ll) abort (); if (__sync_and_and_fetch (AL+20, 0x300000007ll) != 0x300000007ll) abort (); if (__sync_or_and_fetch (AL+21, 0x500000009ll) != 0x500000009ll) abort (); if (__sync_xor_and_fetch (AL+22, 0xe00000001ll) != 0xf100ff0001ll) abort (); if (__sync_nand_and_fetch (AL+23, 0xa00000007ll) != ~0xa00000007ll) abort (); }
static void do_noret_di (void) { __sync_val_compare_and_swap(AL+0, 0, 1); __sync_bool_compare_and_swap(AL+1, 0, 1); __sync_lock_test_and_set(AL+2, 1); __sync_lock_release(AL+3); __sync_fetch_and_add(AL+4, 1); __sync_fetch_and_add(AL+5, 4); __sync_fetch_and_add(AL+6, 22); __sync_fetch_and_sub(AL+7, 12); __sync_fetch_and_and(AL+8, 7); __sync_fetch_and_or(AL+9, 8); __sync_fetch_and_xor(AL+10, 9); __sync_fetch_and_nand(AL+11, 7); }
static void do_di (void) { if (__sync_val_compare_and_swap(AL+0, 0, 1) != 0) abort (); if (__sync_val_compare_and_swap(AL+0, 0, 1) != 1) abort (); if (__sync_bool_compare_and_swap(AL+1, 0, 1) != 1) abort (); if (__sync_bool_compare_and_swap(AL+1, 0, 1) != 0) abort (); if (__sync_lock_test_and_set(AL+2, 1) != 0) abort (); if (__sync_fetch_and_add(AL+4, 1) != 0) abort (); if (__sync_fetch_and_add(AL+5, 4) != 0) abort (); if (__sync_fetch_and_add(AL+6, 22) != 0) abort (); if (__sync_fetch_and_sub(AL+7, 12) != 0) abort (); if (__sync_fetch_and_and(AL+8, 7) != -1) abort (); if (__sync_fetch_and_or(AL+9, 8) != 0) abort (); if (__sync_fetch_and_xor(AL+10, 9) != 0) abort (); if (__sync_fetch_and_nand(AL+11, 7) != 0) abort (); if (__sync_add_and_fetch(AL+12, 1) != 1) abort (); if (__sync_sub_and_fetch(AL+13, 12) != -12) abort (); if (__sync_and_and_fetch(AL+14, 7) != 7) abort (); if (__sync_or_and_fetch(AL+15, 8) != 8) abort (); if (__sync_xor_and_fetch(AL+16, 9) != 9) abort (); if (__sync_nand_and_fetch(AL+17, 7) != 7) abort (); }
/* First check they work in terms of what they do to memory. */ static void do_noret_di (void) { __sync_val_compare_and_swap (AL+0, 0x100000002ll, 0x1234567890ll); __sync_bool_compare_and_swap (AL+1, 0x200000003ll, 0x1234567890ll); __sync_lock_test_and_set (AL+2, 1); __sync_lock_release (AL+3); /* The following tests should not change the value since the original does NOT match. */ __sync_val_compare_and_swap (AL+4, 0x000000002ll, 0x1234567890ll); __sync_val_compare_and_swap (AL+5, 0x100000000ll, 0x1234567890ll); __sync_bool_compare_and_swap (AL+6, 0x000000002ll, 0x1234567890ll); __sync_bool_compare_and_swap (AL+7, 0x100000000ll, 0x1234567890ll); __sync_fetch_and_add (AL+8, 1); __sync_fetch_and_add (AL+9, 0xb000e0000000ll); /* + to both halves & carry. */ __sync_fetch_and_sub (AL+10, 22); __sync_fetch_and_sub (AL+11, 0xb000e0000000ll); __sync_fetch_and_and (AL+12, 0x300000007ll); __sync_fetch_and_or (AL+13, 0x500000009ll); __sync_fetch_and_xor (AL+14, 0xe00000001ll); __sync_fetch_and_nand (AL+15, 0xa00000007ll); /* These should be the same as the fetch_and_* cases except for return value. */ __sync_add_and_fetch (AL+16, 1); /* add to both halves & carry. */ __sync_add_and_fetch (AL+17, 0xb000e0000000ll); __sync_sub_and_fetch (AL+18, 22); __sync_sub_and_fetch (AL+19, 0xb000e0000000ll); __sync_and_and_fetch (AL+20, 0x300000007ll); __sync_or_and_fetch (AL+21, 0x500000009ll); __sync_xor_and_fetch (AL+22, 0xe00000001ll); __sync_nand_and_fetch (AL+23, 0xa00000007ll); }
int main() { { T x = HILO(5, 3); T y = __sync_fetch_and_add(&x, DUP(1)); assert(y == HILO(5, 3)); assert(x == HILO(6, 4)); volatile T n = HILO(2, 1); if (emscripten_has_threading_support()) { for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_add, (void*)&n); for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); printf("n: %llx\n", n); assert(n == HILO(NUM_THREADS*10000ULL+2ULL, NUM_THREADS*10000ULL+1ULL)); } } { T x = HILO(15, 13); T y = __sync_fetch_and_sub(&x, HILO(10, 10)); assert(y == HILO(15, 13)); assert(x == HILO(5, 3)); volatile T n = HILO(NUM_THREADS*10000ULL+5ULL, NUM_THREADS*10000ULL+3ULL); if (emscripten_has_threading_support()) { for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_sub, (void*)&n); for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); printf("n: %llx\n", n); assert(n == HILO(5,3)); } } { T x = HILO(32768 + 5, 5); T y = __sync_fetch_and_or(&x, HILO(65536 + 9, 9)); assert(y == HILO(32768 + 5, 5)); assert(x == HILO(32768 + 65536 + 13, 13)); if (emscripten_has_threading_support()) { for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. { fetch_and_or_data = HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS); for(int i = 0; i < NUM_THREADS; ++i) { threadArg[i] = DUP(1 << i); pthread_create(&thread[i], NULL, thread_fetch_and_or, (void*)&threadArg[i]); } for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); assert(fetch_and_or_data == HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1)); } } } { T x = HILO(32768 + 5, 5); T y = __sync_fetch_and_and(&x, HILO(32768 + 9, 9)); assert(y == HILO(32768 + 5, 5)); assert(x == HILO(32768 + 1, 1)); if (emscripten_has_threading_support()) { for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. { fetch_and_and_data = HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1); for(int i = 0; i < NUM_THREADS; ++i) { threadArg[i] = DUP(~(1UL<<i)); pthread_create(&thread[i], NULL, thread_fetch_and_and, (void*)&threadArg[i]); } for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); assert(fetch_and_and_data == HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS)); } } } { T x = HILO(32768 + 5, 5); T y = __sync_fetch_and_xor(&x, HILO(16384 + 9, 9)); assert(y == HILO(32768 + 5, 5)); assert(x == HILO(32768 + 16384 + 12, 12)); if (emscripten_has_threading_support()) { for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. { fetch_and_xor_data = HILO(32768 + (1<<NUM_THREADS), 1<<NUM_THREADS); for(int i = 0; i < NUM_THREADS; ++i) { threadArg[i] = DUP(~(1UL<<i)); pthread_create(&thread[i], NULL, thread_fetch_and_xor, (void*)&threadArg[i]); } for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL); assert(fetch_and_xor_data == HILO(32768 + ((1<<(NUM_THREADS+1))-1), (1<<(NUM_THREADS+1))-1)); } } } // XXX NAND support does not exist in Atomics API. #if 0 { T x = 5; T y = __sync_fetch_and_nand(&x, 9); assert(y == 5); assert(x == -2); const int oddNThreads = NUM_THREADS-1; for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived. { fetch_and_nand_data = 0; for(int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_nand, (void*)-1); for(int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL); assert(fetch_and_nand_data == -1); } } #endif #ifdef REPORT_RESULT int result = 0; REPORT_RESULT(); #endif }
void *thread_fetch_and_nand(void *arg) { for(int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out. __sync_fetch_and_nand((int*)&fetch_and_nand_data, (int)arg); pthread_exit(0); }
void test9_4(volatile int* ptr, int val) { // expected-warning@+1 {{the semantics of this intrinsic changed with GCC version 4.4 - the newer semantics are provided here}} __sync_fetch_and_nand(ptr, val); }
NOMIPS16 int f1 (int *z) { return __sync_fetch_and_nand (z, 42); }
NOMIPS16 char f3 (char *z) { return __sync_fetch_and_nand (z, 42); }
NOMIPS16 short f2 (short *z) { return __sync_fetch_and_nand (z, 42); }
T fetchAndNand(T value) { return __sync_fetch_and_nand(&_value, value); }