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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include <stdatomic.h>
/**
** @brief A simple accumulation in a loop
**
** Any decent optimizer should be able to figure out that the return
** value is @c 100 and that the only side effect is to store the
** value @c 10 into @c *count.
**/
unsigned accu(unsigned*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 9) {
tmp = (*count)++;
ret += 2*tmp +1;
}
return ret;
}
/**
** @brief @c volatile qualification
**
** @c volatile forces all stores to be effected. So this has side
** effects of storing @f$0, \ldots, 10@f$ into @c *count.
**/
unsigned accuv(unsigned volatile*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 9) {
tmp = (*count)++;
ret += 2*tmp +1;
}
return ret;
}
/**
** @brief @c _Atomic specification
**
** Here, because it is not @c volatile, none of the side effects
** would have to be made visible before returning. Only there is an
** ambiguous phrase in the C standard:
**
** <quote>
** Atomic read-modify-write operations shall always read the last
** value (in the modification order) stored before the write
** associated with the read-modify-write operation.
** </quote>
**
** This can be read that for read-modify-write operations the read
** and write operations *must* be effected.
**/
unsigned accua(_Atomic(unsigned)*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 9) {
tmp = (*count)++;
ret += 2*tmp +1;
}
return ret;
}
unsigned accuav(_Atomic(unsigned) volatile*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 9) {
tmp = (*count)++;
ret += 2*tmp +1;
}
return ret;
}
unsigned accuar(_Atomic(unsigned)*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
atomic_store_explicit(count, 0, memory_order_relaxed);
while (tmp < 9) {
tmp = atomic_fetch_add_explicit(count, 1u, memory_order_relaxed);
ret += 2*tmp +1;
}
return ret;
}
unsigned accuavr(_Atomic(unsigned) volatile*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
atomic_store_explicit(count, 0, memory_order_relaxed);
while (tmp < 9) {
tmp = atomic_fetch_add_explicit(count, 1u, memory_order_relaxed);
ret += 2*tmp +1;
}
return ret;
}
unsigned accut(unsigned*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 10) {
ret += 2*tmp +1;
(*count) = ++tmp;
}
return ret;
}
unsigned accutv(unsigned volatile*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 10) {
ret += 2*tmp +1;
(*count) = ++tmp;
}
return ret;
}
unsigned accuta(_Atomic(unsigned)*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 10) {
ret += 2*tmp +1;
(*count) = ++tmp;
}
return ret;
}
unsigned accutav(_Atomic(unsigned) volatile*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
(*count) = 0;
while (tmp < 10) {
ret += 2*tmp +1;
(*count) = ++tmp;
}
return ret;
}
unsigned accutar(_Atomic(unsigned)*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
atomic_store_explicit(count, 0, memory_order_relaxed);
while (tmp < 10) {
ret += 2*tmp +1;
atomic_store_explicit(count, ++tmp, memory_order_relaxed);
}
return ret;
}
unsigned accutavr(_Atomic(unsigned) volatile*restrict count) {
unsigned ret = 0;
unsigned tmp = 0;
atomic_store_explicit(count, 0, memory_order_relaxed);
while (tmp < 10) {
ret += 2*tmp +1;
atomic_store_explicit(count, ++tmp, memory_order_relaxed);
}
return ret;
}
|