1*0a6a1f1dSLionel Sambuc /* $NetBSD: all_sync_ops_linkable.c,v 1.4 2014/02/21 10:26:25 martin Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc * by Martin Husemann <martin@NetBSD.org>.
9*0a6a1f1dSLionel Sambuc *
10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc * are met:
13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc *
19*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc */
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc /*
33*0a6a1f1dSLionel Sambuc * This is a simple link-time test to verify all builtin atomic sync
34*0a6a1f1dSLionel Sambuc * operations are available. Depending on the exact cpu/arch code generator
35*0a6a1f1dSLionel Sambuc * options, some of these need support functions (which on NetBSD we
36*0a6a1f1dSLionel Sambuc * typically provide in src/common/lib/libc/atomic).
37*0a6a1f1dSLionel Sambuc *
38*0a6a1f1dSLionel Sambuc * The list of operations has been extracted from sync-builtins.def file
39*0a6a1f1dSLionel Sambuc * in the gcc distribution (as of gcc 4.8.2).
40*0a6a1f1dSLionel Sambuc */
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc #include <machine/types.h>
43*0a6a1f1dSLionel Sambuc #include <sys/inttypes.h>
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc volatile uint8_t u8 = 0;
46*0a6a1f1dSLionel Sambuc volatile uint16_t u16 = 0;
47*0a6a1f1dSLionel Sambuc volatile uint32_t u32 = 0;
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
50*0a6a1f1dSLionel Sambuc volatile uint64_t u64 = 0;
51*0a6a1f1dSLionel Sambuc #endif
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc int
main(int argc,char ** argv)54*0a6a1f1dSLionel Sambuc main(int argc, char **argv)
55*0a6a1f1dSLionel Sambuc {
56*0a6a1f1dSLionel Sambuc __sync_synchronize();
57*0a6a1f1dSLionel Sambuc __sync_add_and_fetch(&u8, 1);
58*0a6a1f1dSLionel Sambuc __sync_add_and_fetch_1(&u8, 1);
59*0a6a1f1dSLionel Sambuc __sync_add_and_fetch_2(&u16, 1);
60*0a6a1f1dSLionel Sambuc __sync_add_and_fetch_4(&u32, 1);
61*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
62*0a6a1f1dSLionel Sambuc __sync_add_and_fetch_8(&u64, 1);
63*0a6a1f1dSLionel Sambuc #endif
64*0a6a1f1dSLionel Sambuc __sync_bool_compare_and_swap(&u8, 1, 2);
65*0a6a1f1dSLionel Sambuc __sync_bool_compare_and_swap_1(&u8, 1, 2);
66*0a6a1f1dSLionel Sambuc __sync_bool_compare_and_swap_2(&u16, 1, 2);
67*0a6a1f1dSLionel Sambuc __sync_bool_compare_and_swap_4(&u32, 1, 2);
68*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
69*0a6a1f1dSLionel Sambuc __sync_bool_compare_and_swap_8(&u64, 1, 2);
70*0a6a1f1dSLionel Sambuc #endif
71*0a6a1f1dSLionel Sambuc __sync_fetch_and_add(&u8, 1);
72*0a6a1f1dSLionel Sambuc __sync_fetch_and_add_1(&u8, 1);
73*0a6a1f1dSLionel Sambuc __sync_fetch_and_add_2(&u16, 1);
74*0a6a1f1dSLionel Sambuc __sync_fetch_and_add_4(&u32, 1);
75*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
76*0a6a1f1dSLionel Sambuc __sync_fetch_and_add_8(&u64, 1);
77*0a6a1f1dSLionel Sambuc #endif
78*0a6a1f1dSLionel Sambuc __sync_fetch_and_and(&u8, 0x80);
79*0a6a1f1dSLionel Sambuc __sync_fetch_and_and_1(&u8, 0x80);
80*0a6a1f1dSLionel Sambuc __sync_fetch_and_and_2(&u16, 0x80);
81*0a6a1f1dSLionel Sambuc __sync_fetch_and_and_4(&u32, 0x80);
82*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
83*0a6a1f1dSLionel Sambuc __sync_fetch_and_and_8(&u64, 0x80);
84*0a6a1f1dSLionel Sambuc #endif
85*0a6a1f1dSLionel Sambuc #ifndef __clang__
86*0a6a1f1dSLionel Sambuc __sync_fetch_and_nand(&u8, 0x80);
87*0a6a1f1dSLionel Sambuc __sync_fetch_and_nand_1(&u8, 0x80);
88*0a6a1f1dSLionel Sambuc __sync_fetch_and_nand_2(&u16, 0x80);
89*0a6a1f1dSLionel Sambuc __sync_fetch_and_nand_4(&u32, 0x80);
90*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
91*0a6a1f1dSLionel Sambuc __sync_fetch_and_nand_8(&u64, 0x80);
92*0a6a1f1dSLionel Sambuc #endif
93*0a6a1f1dSLionel Sambuc #endif
94*0a6a1f1dSLionel Sambuc __sync_fetch_and_or(&u8, 0x80);
95*0a6a1f1dSLionel Sambuc __sync_fetch_and_or_1(&u8, 0x80);
96*0a6a1f1dSLionel Sambuc __sync_fetch_and_or_2(&u16, 0x80);
97*0a6a1f1dSLionel Sambuc __sync_fetch_and_or_4(&u32, 0x80);
98*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
99*0a6a1f1dSLionel Sambuc __sync_fetch_and_or_8(&u64, 0x80);
100*0a6a1f1dSLionel Sambuc #endif
101*0a6a1f1dSLionel Sambuc __sync_fetch_and_sub(&u8, 0x80);
102*0a6a1f1dSLionel Sambuc __sync_fetch_and_sub_1(&u8, 0x80);
103*0a6a1f1dSLionel Sambuc __sync_fetch_and_sub_2(&u16, 0x80);
104*0a6a1f1dSLionel Sambuc __sync_fetch_and_sub_4(&u32, 0x80);
105*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
106*0a6a1f1dSLionel Sambuc __sync_fetch_and_sub_8(&u64, 0x80);
107*0a6a1f1dSLionel Sambuc #endif
108*0a6a1f1dSLionel Sambuc __sync_fetch_and_xor(&u8, 0x80);
109*0a6a1f1dSLionel Sambuc __sync_fetch_and_xor_1(&u8, 0x80);
110*0a6a1f1dSLionel Sambuc __sync_fetch_and_xor_2(&u16, 0x80);
111*0a6a1f1dSLionel Sambuc __sync_fetch_and_xor_4(&u32, 0x80);
112*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
113*0a6a1f1dSLionel Sambuc __sync_fetch_and_xor_8(&u64, 0x80);
114*0a6a1f1dSLionel Sambuc #endif
115*0a6a1f1dSLionel Sambuc __sync_lock_release(&u8);
116*0a6a1f1dSLionel Sambuc __sync_lock_release_1(&u8);
117*0a6a1f1dSLionel Sambuc __sync_lock_release_2(&u16);
118*0a6a1f1dSLionel Sambuc __sync_lock_release_4(&u32);
119*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
120*0a6a1f1dSLionel Sambuc __sync_lock_release_8(&u64);
121*0a6a1f1dSLionel Sambuc #endif
122*0a6a1f1dSLionel Sambuc __sync_lock_test_and_set(&u8, 5);
123*0a6a1f1dSLionel Sambuc __sync_lock_test_and_set_1(&u8, 5);
124*0a6a1f1dSLionel Sambuc __sync_lock_test_and_set_2(&u16, 5);
125*0a6a1f1dSLionel Sambuc __sync_lock_test_and_set_4(&u32, 5);
126*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
127*0a6a1f1dSLionel Sambuc __sync_lock_test_and_set_8(&u64, 5);
128*0a6a1f1dSLionel Sambuc #endif
129*0a6a1f1dSLionel Sambuc #ifndef __clang__
130*0a6a1f1dSLionel Sambuc __sync_nand_and_fetch(&u8, 5);
131*0a6a1f1dSLionel Sambuc __sync_nand_and_fetch_1(&u8, 5);
132*0a6a1f1dSLionel Sambuc __sync_nand_and_fetch_2(&u16, 5);
133*0a6a1f1dSLionel Sambuc __sync_nand_and_fetch_4(&u32, 5);
134*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
135*0a6a1f1dSLionel Sambuc __sync_nand_and_fetch_8(&u64, 5);
136*0a6a1f1dSLionel Sambuc #endif
137*0a6a1f1dSLionel Sambuc #endif
138*0a6a1f1dSLionel Sambuc __sync_or_and_fetch(&u8, 5);
139*0a6a1f1dSLionel Sambuc __sync_or_and_fetch_1(&u8, 5);
140*0a6a1f1dSLionel Sambuc __sync_or_and_fetch_2(&u16, 5);
141*0a6a1f1dSLionel Sambuc __sync_or_and_fetch_4(&u32, 5);
142*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
143*0a6a1f1dSLionel Sambuc __sync_or_and_fetch_8(&u64, 5);
144*0a6a1f1dSLionel Sambuc #endif
145*0a6a1f1dSLionel Sambuc __sync_sub_and_fetch(&u8, 5);
146*0a6a1f1dSLionel Sambuc __sync_sub_and_fetch_1(&u8, 5);
147*0a6a1f1dSLionel Sambuc __sync_sub_and_fetch_2(&u16, 5);
148*0a6a1f1dSLionel Sambuc __sync_sub_and_fetch_4(&u32, 5);
149*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
150*0a6a1f1dSLionel Sambuc __sync_sub_and_fetch_8(&u64, 5);
151*0a6a1f1dSLionel Sambuc #endif
152*0a6a1f1dSLionel Sambuc __sync_val_compare_and_swap(&u8, 5, 9);
153*0a6a1f1dSLionel Sambuc __sync_val_compare_and_swap_1(&u8, 5, 9);
154*0a6a1f1dSLionel Sambuc __sync_val_compare_and_swap_2(&u16, 5, 9);
155*0a6a1f1dSLionel Sambuc __sync_val_compare_and_swap_4(&u32, 5, 9);
156*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
157*0a6a1f1dSLionel Sambuc __sync_val_compare_and_swap_8(&u64, 5, 9);
158*0a6a1f1dSLionel Sambuc #endif
159*0a6a1f1dSLionel Sambuc __sync_xor_and_fetch(&u8, 5);
160*0a6a1f1dSLionel Sambuc __sync_xor_and_fetch_1(&u8, 5);
161*0a6a1f1dSLionel Sambuc __sync_xor_and_fetch_2(&u16, 5);
162*0a6a1f1dSLionel Sambuc __sync_xor_and_fetch_4(&u32, 5);
163*0a6a1f1dSLionel Sambuc #ifdef __HAVE_ATOMIC64_OPS
164*0a6a1f1dSLionel Sambuc __sync_xor_and_fetch_8(&u64, 5);
165*0a6a1f1dSLionel Sambuc #endif
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc return 0;
168*0a6a1f1dSLionel Sambuc }
169