cpp_atomic_ops_linkable.cc revision 313498
1/* $NetBSD: cpp_atomic_ops_linkable.cc,v 1.4 2016/02/27 18:50:39 joerg Exp $ */
2
3/*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann <martin@NetBSD.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * This is a simple link-time test to verify all builtin atomic sync
34 * operations for C++ <atomic> are available.
35 */
36
37#include <atomic>
38#include <machine/types.h>	// for __HAVE_ATOMIC64_OPS
39
40template <class T>
41class ATest {
42public:
43  ATest() : m_val(0)
44  {
45    m_val.exchange(std::atomic<T>(8));
46    m_val--;
47    m_val++;
48    m_val ^= 0x0f;
49    m_val &= 0x0f;
50    m_val |= 2;
51
52    T tval(1), other(42);
53    m_val.compare_exchange_weak(tval, other,
54      std::memory_order_release, std::memory_order_relaxed);
55  }
56
57private:
58  volatile std::atomic<T> m_val;
59};
60
61#if defined(__clang__) && defined(__sparc64__)
62#define NO_SHORT_ATOMICS
63#endif
64
65int main(int argc, char **argv)
66{
67#ifndef NO_SHORT_ATOMICS
68  ATest<char>();
69  ATest<signed char>();
70  ATest<unsigned char>();
71  ATest<short>();
72  ATest<unsigned short>();
73#endif
74  ATest<int>();
75  ATest<unsigned int>();
76  ATest<long>();
77  ATest<unsigned long>();
78#ifdef __HAVE_ATOMIC64_OPS
79  ATest<long long>();
80  ATest<unsigned long long>();
81#endif
82#ifndef NO_SHORT_ATOMICS
83  ATest<char16_t>();
84#endif
85  ATest<char32_t>();
86  ATest<wchar_t>();
87#ifndef NO_SHORT_ATOMICS
88  ATest<int_least8_t>();
89  ATest<uint_least8_t>();
90  ATest<int_least16_t>();
91  ATest<uint_least16_t>();
92#endif
93  ATest<int_least32_t>();
94  ATest<uint_least32_t>();
95#ifdef __HAVE_ATOMIC64_OPS
96  ATest<int_least64_t>();
97  ATest<uint_least64_t>();
98#endif
99#ifndef NO_SHORT_ATOMICS
100  ATest<int_fast8_t>();
101  ATest<uint_fast8_t>();
102  ATest<int_fast16_t>();
103  ATest<uint_fast16_t>();
104#endif
105  ATest<int_fast32_t>();
106  ATest<uint_fast32_t>();
107#ifdef __HAVE_ATOMIC64_OPS
108  ATest<int_fast64_t>();
109  ATest<uint_fast64_t>();
110#endif
111  ATest<intptr_t>();
112  ATest<uintptr_t>();
113  ATest<std::size_t>();
114  ATest<std::ptrdiff_t>();
115#ifdef __HAVE_ATOMIC64_OPS
116  ATest<intmax_t>();
117  ATest<uintmax_t>();
118#endif /* NO_SHORT_ATOMICS */
119}
120