t___sync_and.c revision 1.2 1 /* $NetBSD: t___sync_and.c,v 1.2 2019/02/27 15:32:11 christos Exp $ */
2
3 /*
4 * Copyright (C) 2019 Tetsuya Isaki. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: t___sync_and.c,v 1.2 2019/02/27 15:32:11 christos Exp $");
30
31 #include <atf-c.h>
32 #include <inttypes.h>
33 #include <machine/types.h> // for __HAVE_ATOMIC64_OPS
34
35 /*
36 * These tests don't examine the atomicity.
37 */
38
39 /* XXX
40 * Depending on a combination of arch and compiler, __sync_* is
41 * implemented as compiler's builtin function. In that case, even
42 * if libc exports the function symbol, it is not used. As a result
43 * this tests will examine compiler's builtin functions.
44 * It's better to run only when target is actually in libc.
45 */
46
47 #define DST (0x1122334455667788UL)
48 #define SRC (0xf0f0f0f0f0f0f0f0UL)
49 #define EXPECT (0x1020304050607080UL)
50
51 #define atf_sync_prefetch(NAME, TYPE, FMT) \
52 ATF_TC(NAME); \
53 ATF_TC_HEAD(NAME, tc) \
54 { \
55 atf_tc_set_md_var(tc, "descr", #NAME); \
56 } \
57 extern TYPE NAME(volatile TYPE *, TYPE) __attribute__((__weak__)); \
58 ATF_TC_BODY(NAME, tc) \
59 { \
60 volatile TYPE val; \
61 TYPE src; \
62 TYPE res; \
63 TYPE expval; \
64 TYPE expres; \
65 val = (TYPE)DST; \
66 src = (TYPE)SRC; \
67 expval = (TYPE)EXPECT; \
68 expres = (TYPE)DST; \
69 if (NAME == NULL) \
70 return; \
71 res = NAME(&val, src); \
72 ATF_REQUIRE_MSG(val == expval, \
73 "val expects 0x%" FMT " but 0x%" FMT, expval, val); \
74 ATF_REQUIRE_MSG(res == expres, \
75 "res expects 0x%" FMT " but 0x%" FMT, expres, res); \
76 }
77
78 atf_sync_prefetch(__sync_fetch_and_and_1, uint8_t, PRIx8);
79 atf_sync_prefetch(__sync_fetch_and_and_2, uint16_t, PRIx16);
80 atf_sync_prefetch(__sync_fetch_and_and_4, uint32_t, PRIx32);
81 #if defined(__HAVE_ATOMIC64_OPS)
82 atf_sync_prefetch(__sync_fetch_and_and_8, uint64_t, PRIx64);
83 #endif
84
85 #define atf_sync_postfetch(NAME, TYPE, FMT) \
86 ATF_TC(NAME); \
87 ATF_TC_HEAD(NAME, tc) \
88 { \
89 atf_tc_set_md_var(tc, "descr", #NAME); \
90 } \
91 extern TYPE NAME(volatile TYPE *, TYPE) __attribute__((__weak__)); \
92 ATF_TC_BODY(NAME, tc) \
93 { \
94 volatile TYPE val; \
95 TYPE src; \
96 TYPE res; \
97 TYPE exp; \
98 val = (TYPE)DST; \
99 src = (TYPE)SRC; \
100 exp = (TYPE)EXPECT; \
101 if (NAME == NULL) \
102 return; \
103 res = NAME(&val, src); \
104 ATF_REQUIRE_MSG(val == exp, \
105 "val expects 0x%" FMT " but 0x%" FMT, exp, val); \
106 ATF_REQUIRE_MSG(res == exp, \
107 "res expects 0x%" FMT " but 0x%" FMT, exp, res); \
108 }
109
110 atf_sync_postfetch(__sync_and_and_fetch_1, uint8_t, PRIx8);
111 atf_sync_postfetch(__sync_and_and_fetch_2, uint16_t, PRIx16);
112 atf_sync_postfetch(__sync_and_and_fetch_4, uint32_t, PRIx32);
113 #ifdef __HAVE_ATOMIC64_OPS
114 atf_sync_postfetch(__sync_and_and_fetch_8, uint64_t, PRIx64);
115 #endif
116
117 ATF_TP_ADD_TCS(tp)
118 {
119 ATF_TP_ADD_TC(tp, __sync_fetch_and_and_1);
120 ATF_TP_ADD_TC(tp, __sync_fetch_and_and_2);
121 ATF_TP_ADD_TC(tp, __sync_fetch_and_and_4);
122 #ifdef __HAVE_ATOMIC64_OPS
123 ATF_TP_ADD_TC(tp, __sync_fetch_and_and_8);
124 #endif
125
126 ATF_TP_ADD_TC(tp, __sync_and_and_fetch_1);
127 ATF_TP_ADD_TC(tp, __sync_and_and_fetch_2);
128 ATF_TP_ADD_TC(tp, __sync_and_and_fetch_4);
129 #ifdef __HAVE_ATOMIC64_OPS
130 ATF_TP_ADD_TC(tp, __sync_and_and_fetch_8);
131 #endif
132
133 return atf_no_error();
134 }
135