1272343Sngie/* $NetBSD: t_bswap.c,v 1.1 2011/05/05 05:39:11 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Jukka Ruohonen.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie#include <sys/cdefs.h>
32272343Sngie__RCSID("$NetBSD: t_bswap.c,v 1.1 2011/05/05 05:39:11 jruoho Exp $");
33272343Sngie
34272343Sngie#include <sys/types.h>
35272343Sngie#include <machine/bswap.h>
36272343Sngie
37272343Sngie#include <atf-c.h>
38272343Sngie
39272343Sngiestatic uint16_t x16;
40272343Sngiestatic uint32_t x32;
41272343Sngiestatic uint64_t x64;
42272343Sngie
43272343Sngiestatic uint16_t	unconst16(uint16_t);
44272343Sngiestatic uint32_t	unconst32(uint32_t);
45272343Sngiestatic uint64_t	unconst64(uint64_t);
46272343Sngie
47272343Sngie/*
48272343Sngie * Given the use of __builtin_constant_p(3),
49272343Sngie * these functions try to avoid gcc(1) from
50272343Sngie * treating the arguments as constants.
51272343Sngie */
52272343Sngiestatic uint16_t
53272343Sngieunconst16(uint16_t val)
54272343Sngie{
55272343Sngie	return val + x16;
56272343Sngie}
57272343Sngie
58272343Sngiestatic uint32_t
59272343Sngieunconst32(uint32_t val)
60272343Sngie{
61272343Sngie	return val + x32;
62272343Sngie}
63272343Sngie
64272343Sngiestatic uint64_t
65272343Sngieunconst64(uint64_t val)
66272343Sngie{
67272343Sngie	return val + x64;
68272343Sngie}
69272343Sngie
70272343SngieATF_TC(bswap16_basic);
71272343SngieATF_TC_HEAD(bswap16_basic, tc)
72272343Sngie{
73272343Sngie	atf_tc_set_md_var(tc, "descr", "A naive test of bswap16(3), #1");
74272343Sngie}
75272343Sngie
76272343SngieATF_TC_BODY(bswap16_basic, tc)
77272343Sngie{
78272343Sngie	ATF_REQUIRE_EQ(bswap16(0x0000), 0x0000);
79272343Sngie	ATF_REQUIRE_EQ(bswap16(0xff00), 0x00ff);
80272343Sngie	ATF_REQUIRE_EQ(bswap16(0xffff), 0xffff);
81272343Sngie	ATF_REQUIRE_EQ(bswap16(0x1234), 0x3412);
82272343Sngie}
83272343Sngie
84272343SngieATF_TC(bswap16_unconst);
85272343SngieATF_TC_HEAD(bswap16_unconst, tc)
86272343Sngie{
87272343Sngie	atf_tc_set_md_var(tc, "descr", "A naive test of bswap16(3), #2");
88272343Sngie}
89272343Sngie
90272343SngieATF_TC_BODY(bswap16_unconst, tc)
91272343Sngie{
92272343Sngie	x16 = 0;
93272343Sngie
94272343Sngie	ATF_REQUIRE_EQ(bswap16(unconst16(0x0000)), 0x0000);
95272343Sngie	ATF_REQUIRE_EQ(bswap16(unconst16(0xff00)), 0x00ff);
96272343Sngie	ATF_REQUIRE_EQ(bswap16(unconst16(0xffff)), 0xffff);
97272343Sngie	ATF_REQUIRE_EQ(bswap16(unconst16(0x1234)), 0x3412);
98272343Sngie}
99272343Sngie
100272343SngieATF_TC(bswap32_basic);
101272343SngieATF_TC_HEAD(bswap32_basic, tc)
102272343Sngie{
103272343Sngie	atf_tc_set_md_var(tc, "descr", "A naive test of bswap32(3), #1");
104272343Sngie}
105272343Sngie
106272343SngieATF_TC_BODY(bswap32_basic, tc)
107272343Sngie{
108272343Sngie	ATF_REQUIRE_EQ(bswap32(0x00000000), 0x00000000);
109272343Sngie	ATF_REQUIRE_EQ(bswap32(0xffff0000), 0x0000ffff);
110272343Sngie	ATF_REQUIRE_EQ(bswap32(0xffffffff), 0xffffffff);
111272343Sngie	ATF_REQUIRE_EQ(bswap32(0x12345678), 0x78563412);
112272343Sngie}
113272343Sngie
114272343SngieATF_TC(bswap32_unconst);
115272343SngieATF_TC_HEAD(bswap32_unconst, tc)
116272343Sngie{
117272343Sngie	atf_tc_set_md_var(tc, "descr", "A naive test of bswap32(3), #2");
118272343Sngie}
119272343Sngie
120272343SngieATF_TC_BODY(bswap32_unconst, tc)
121272343Sngie{
122272343Sngie	x32 = 0;
123272343Sngie
124272343Sngie	ATF_REQUIRE_EQ(bswap32(unconst32(0x00000000)), 0x00000000);
125272343Sngie	ATF_REQUIRE_EQ(bswap32(unconst32(0xffff0000)), 0x0000ffff);
126272343Sngie	ATF_REQUIRE_EQ(bswap32(unconst32(0xffffffff)), 0xffffffff);
127272343Sngie	ATF_REQUIRE_EQ(bswap32(unconst32(0x12345678)), 0x78563412);
128272343Sngie}
129272343Sngie
130272343SngieATF_TC(bswap64_basic);
131272343SngieATF_TC_HEAD(bswap64_basic, tc)
132272343Sngie{
133272343Sngie	atf_tc_set_md_var(tc, "descr", "A naive test of bswap64(3), #1");
134272343Sngie}
135272343Sngie
136272343SngieATF_TC_BODY(bswap64_basic, tc)
137272343Sngie{
138272343Sngie	ATF_REQUIRE_EQ(bswap64(0x0000000000000000), 0x0000000000000000);
139272343Sngie	ATF_REQUIRE_EQ(bswap64(0xffffffff00000000), 0x00000000ffffffff);
140272343Sngie	ATF_REQUIRE_EQ(bswap64(0xffffffffffffffff), 0xffffffffffffffff);
141272343Sngie	ATF_REQUIRE_EQ(bswap64(0x123456789abcdeff), 0xffdebc9a78563412);
142272343Sngie}
143272343Sngie
144272343SngieATF_TC(bswap64_unconst);
145272343SngieATF_TC_HEAD(bswap64_unconst, tc)
146272343Sngie{
147272343Sngie	atf_tc_set_md_var(tc, "descr", "A naive test of bswap64(3), #2");
148272343Sngie}
149272343Sngie
150272343SngieATF_TC_BODY(bswap64_unconst, tc)
151272343Sngie{
152272343Sngie	x64 = 0;
153272343Sngie
154272343Sngie	ATF_REQUIRE_EQ(bswap64(unconst64(0x0000000000000000)),
155272343Sngie	    0x0000000000000000);
156272343Sngie
157272343Sngie	ATF_REQUIRE_EQ(bswap64(unconst64(0xffffffff00000000)),
158272343Sngie	    0x00000000ffffffff);
159272343Sngie
160272343Sngie	ATF_REQUIRE_EQ(bswap64(unconst64(0xffffffffffffffff)),
161272343Sngie	    0xffffffffffffffff);
162272343Sngie
163272343Sngie	ATF_REQUIRE_EQ(bswap64(unconst64(0x123456789abcdeff)),
164272343Sngie	    0xffdebc9a78563412);
165272343Sngie}
166272343Sngie
167272343SngieATF_TP_ADD_TCS(tp)
168272343Sngie{
169272343Sngie
170272343Sngie	ATF_TP_ADD_TC(tp, bswap16_basic);
171272343Sngie	ATF_TP_ADD_TC(tp, bswap16_unconst);
172272343Sngie	ATF_TP_ADD_TC(tp, bswap32_basic);
173272343Sngie	ATF_TP_ADD_TC(tp, bswap32_unconst);
174272343Sngie	ATF_TP_ADD_TC(tp, bswap64_basic);
175272343Sngie	ATF_TP_ADD_TC(tp, bswap64_unconst);
176272343Sngie
177272343Sngie	return atf_no_error();
178272343Sngie}
179