forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_assembly.cpp
More file actions
148 lines (136 loc) · 5.22 KB
/
test_assembly.cpp
File metadata and controls
148 lines (136 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2005-2014 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks. Threading Building Blocks is free software;
you can redistribute it and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation. Threading Building Blocks is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details. You should have received a copy of
the GNU General Public License along with Threading Building Blocks; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software library without
restriction. Specifically, if other files instantiate templates or use macros or inline
functions from this file, or you compile this file and link it with other files to produce
an executable, this file does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however invalidate any other
reasons why the executable file might be covered by the GNU General Public License.
*/
// Program for basic correctness testing of assembly-language routines.
#include "harness_defs.h"
//for ICC builtins mode the test will be skipped as
//macro __TBB_GCC_BUILTIN_ATOMICS_PRESENT used to define __TBB_TEST_SKIP_GCC_BUILTINS_MODE
//will not be defined (it is explicitly disabled for ICC)
#if __TBB_TEST_SKIP_GCC_BUILTINS_MODE
#include "harness.h"
int TestMain() {
REPORT("Known issue: GCC builtins aren't available\n");
return Harness::Skipped;
}
#else
#include "tbb/task.h"
#include <new>
#include "harness.h"
using tbb::internal::reference_count;
//TODO: remove this function when atomic function __TBB_XXX are dropped
//! Test __TBB_CompareAndSwapW
static void TestCompareExchange() {
ASSERT( intptr_t(-10)<10, "intptr_t not a signed integral type?" );
REMARK("testing __TBB_CompareAndSwapW\n");
for( intptr_t a=-10; a<10; ++a )
for( intptr_t b=-10; b<10; ++b )
for( intptr_t c=-10; c<10; ++c ) {
// Workaround for a bug in GCC 4.3.0; and one more is below.
#if __TBB_GCC_OPTIMIZER_ORDERING_BROKEN
intptr_t x;
__TBB_store_with_release( x, a );
#else
intptr_t x = a;
#endif
intptr_t y = __TBB_CompareAndSwapW(&x,b,c);
ASSERT( y==a, NULL );
if( a==c )
ASSERT( x==b, NULL );
else
ASSERT( x==a, NULL );
}
}
//TODO: remove this function when atomic function __TBB_XXX are dropped
//! Test __TBB___TBB_FetchAndIncrement and __TBB___TBB_FetchAndDecrement
static void TestAtomicCounter() {
// "canary" is a value used to detect illegal overwrites.
const reference_count canary = ~(uintptr_t)0/3;
REMARK("testing __TBB_FetchAndIncrement\n");
struct {
reference_count prefix, i, suffix;
} x;
x.prefix = canary;
x.i = 0;
x.suffix = canary;
for( int k=0; k<10; ++k ) {
reference_count j = __TBB_FetchAndIncrementWacquire((volatile void *)&x.i);
ASSERT( x.prefix==canary, NULL );
ASSERT( x.suffix==canary, NULL );
ASSERT( x.i==k+1, NULL );
ASSERT( j==k, NULL );
}
REMARK("testing __TBB_FetchAndDecrement\n");
x.i = 10;
for( int k=10; k>0; --k ) {
reference_count j = __TBB_FetchAndDecrementWrelease((volatile void *)&x.i);
ASSERT( j==k, NULL );
ASSERT( x.i==k-1, NULL );
ASSERT( x.prefix==canary, NULL );
ASSERT( x.suffix==canary, NULL );
}
}
static void TestTinyLock() {
REMARK("testing __TBB_LockByte\n");
__TBB_atomic_flag flags[16];
for( unsigned int i=0; i<16; ++i )
flags[i] = (__TBB_Flag)i;
#if __TBB_GCC_OPTIMIZER_ORDERING_BROKEN
__TBB_store_with_release( flags[8], 0 );
#else
flags[8] = 0;
#endif
__TBB_LockByte(flags[8]);
for( unsigned int i=0; i<16; ++i )
#ifdef __sparc
ASSERT( flags[i]==(i==8?0xff:i), NULL );
#else
ASSERT( flags[i]==(i==8?1:i), NULL );
#endif
__TBB_UnlockByte(flags[8]);
for( unsigned int i=0; i<16; ++i )
ASSERT( flags[i] == (i==8?0:i), NULL );
}
static void TestLog2() {
REMARK("testing __TBB_Log2\n");
for( uintptr_t i=1; i; i<<=1 ) {
for( uintptr_t j=1; j<1<<16; ++j ) {
if( uintptr_t k = i*j ) {
uintptr_t actual = __TBB_Log2(k);
const uintptr_t ONE = 1; // warning suppression again
ASSERT( k >= ONE<<actual, NULL );
ASSERT( k>>1 < ONE<<actual, NULL );
}
}
}
}
static void TestPause() {
REMARK("testing __TBB_Pause\n");
__TBB_Pause(1);
}
int TestMain () {
__TBB_TRY {
TestLog2();
TestTinyLock();
TestCompareExchange();
TestAtomicCounter();
TestPause();
} __TBB_CATCH(...) {
ASSERT(0,"unexpected exception");
}
return Harness::Done;
}
#endif // __TBB_TEST_SKIP_BUILTINS_MODE