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