1122222Ssimokawa/* $NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $ */
2122222Ssimokawa
3122222Ssimokawa/*-
4122222Ssimokawa * Copyright (c) 2011 The NetBSD Foundation, Inc.
5122222Ssimokawa * All rights reserved.
6122222Ssimokawa *
7122222Ssimokawa * This code is derived from software contributed to The NetBSD Foundation
8122222Ssimokawa * by Christos Zoulas.
9122222Ssimokawa *
10122222Ssimokawa * Redistribution and use in source and binary forms, with or without
11122222Ssimokawa * modification, are permitted provided that the following conditions
12122222Ssimokawa * are met:
13122222Ssimokawa * 1. Redistributions of source code must retain the above copyright
14122222Ssimokawa *    notice, this list of conditions and the following disclaimer.
15122222Ssimokawa * 2. Redistributions in binary form must reproduce the above copyright
16122222Ssimokawa *    notice, this list of conditions and the following disclaimer in the
17122222Ssimokawa *    documentation and/or other materials provided with the distribution.
18122222Ssimokawa * 3. All advertising materials mentioning features or use of this software
19122222Ssimokawa *    must display the following acknowledgement:
20122222Ssimokawa *        This product includes software developed by the NetBSD
21122222Ssimokawa *        Foundation, Inc. and its contributors.
22122222Ssimokawa * 4. Neither the name of The NetBSD Foundation nor the names of its
23122222Ssimokawa *    contributors may be used to endorse or promote products derived
24122222Ssimokawa *    from this software without specific prior written permission.
25122222Ssimokawa *
26122222Ssimokawa * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27122222Ssimokawa * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28122222Ssimokawa * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29122222Ssimokawa * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30122222Ssimokawa * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31122222Ssimokawa * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32122222Ssimokawa * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33122222Ssimokawa * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34131468Sru * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35122222Ssimokawa * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36122222Ssimokawa * POSSIBILITY OF SUCH DAMAGE.
37122222Ssimokawa */
38122222Ssimokawa#include <sys/cdefs.h>
39122222Ssimokawa__RCSID("$NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $");
40122222Ssimokawa
41198125Sbrueffer#include <float.h>
42198125Sbrueffer#include <math.h>
43198125Sbrueffer#include <stdlib.h>
44198125Sbrueffer#include <string.h>
45122222Ssimokawa#include <stdio.h>
46122222Ssimokawa
47122222Ssimokawa#include <atf-c.h>
48122222Ssimokawa
49198125SbruefferATF_TC(fpsetround_basic);
50198125SbruefferATF_TC_HEAD(fpsetround_basic, tc)
51198125Sbrueffer{
52198125Sbrueffer
53198125Sbrueffer	atf_tc_set_md_var(tc, "descr",
54198125Sbrueffer	    "Minimal testing of fpgetround(3) and fpsetround(3)");
55198125Sbrueffer}
56198125Sbrueffer
57198125Sbrueffer#ifdef _FLOAT_IEEE754
58198125Sbrueffer#include <ieeefp.h>
59122222Ssimokawa
60122222Ssimokawastatic const struct {
61122222Ssimokawa	const char *n;
62122222Ssimokawa	int rm;
63131802Sru	int rf;
64131802Sru} rnd[] = {
65131802Sru	{ "RN", FP_RN, 1 },
66131802Sru	{ "RP", FP_RP, 2 },
67131802Sru	{ "RM", FP_RM, 3 },
68131802Sru	{ "RZ", FP_RZ, 0 },
69131802Sru
70131802Sru};
71131802Sru
72131802Srustatic const struct {
73131802Sru	const char *n;
74122222Ssimokawa	int v[4];
75122222Ssimokawa} tst[] = {	/*  RN  RP  RM  RZ */
76122222Ssimokawa	{  "1.1", {  1,  1,  2,  1 } },
77122222Ssimokawa	{  "1.5", {  1,  2,  2,  1 } },
78122222Ssimokawa	{  "1.9", {  1,  2,  2,  1 } },
79122222Ssimokawa	{  "2.1", {  2,  2,  3,  2 } },
80122222Ssimokawa	{  "2.5", {  2,  2,  3,  2 } },
81122222Ssimokawa	{  "2.9", {  2,  3,  3,  2 } },
82122222Ssimokawa	{ "-1.1", { -1, -1, -1, -2 } },
83122222Ssimokawa	{ "-1.5", { -1, -2, -1, -2 } },
84122222Ssimokawa	{ "-1.9", { -1, -2, -1, -2 } },
85122222Ssimokawa	{ "-2.1", { -2, -2, -2, -3 } },
86122222Ssimokawa	{ "-2.5", { -2, -2, -2, -3 } },
87131594Sru	{ "-2.9", { -2, -3, -2, -3 } },
88122222Ssimokawa};
89122222Ssimokawa
90122222Ssimokawastatic const char *
91122222Ssimokawagetname(int r)
92122222Ssimokawa{
93122222Ssimokawa	for (size_t i = 0; i < __arraycount(rnd); i++)
94122222Ssimokawa		if (rnd[i].rm == r)
95131802Sru			return rnd[i].n;
96208596Suqs	return "*unknown*";
97131802Sru}
98131802Sru
99static void
100test(int r)
101{
102	int did = 0;
103	for (size_t i = 0; i < __arraycount(tst); i++) {
104		double d = strtod(tst[i].n, NULL);
105		int g = (int)rint(d);
106		int e = tst[i].v[r];
107		ATF_CHECK_EQ(g, e);
108		if (g != e) {
109			if (!did) {
110				fprintf(stderr, "Mode Value Result Expected\n");
111				did = 1;
112			}
113			fprintf(stderr, "%4.4s %-5.5s %6d %8d\n", rnd[r].n,
114			    tst[i].n, (int)rint(d), tst[i].v[r]);
115		}
116	}
117}
118#endif
119
120
121ATF_TC_BODY(fpsetround_basic, tc)
122{
123
124#ifndef _FLOAT_IEEE754
125	atf_tc_skip("Test not applicable on this architecture.");
126#else
127	int r;
128
129	ATF_CHECK_EQ(r = fpgetround(), FP_RN);
130	if (FP_RN != r)
131		fprintf(stderr, "default expected=%s got=%s\n", getname(FP_RN),
132		    getname(r));
133	ATF_CHECK_EQ(FLT_ROUNDS, 1);
134
135	for (size_t i = 0; i < __arraycount(rnd); i++) {
136		const size_t j = (i + 1) & 3;
137		const int o = rnd[i].rm;
138		const int n = rnd[j].rm;
139
140		ATF_CHECK_EQ(r = fpsetround(n), o);
141		if (o != r)
142			fprintf(stderr, "set %s expected=%s got=%s\n",
143			    getname(n), getname(o), getname(r));
144		ATF_CHECK_EQ(r = fpgetround(), n);
145		if (n != r)
146			fprintf(stderr, "get expected=%s got=%s\n", getname(n),
147			    getname(r));
148		ATF_CHECK_EQ(r = FLT_ROUNDS, rnd[j].rf);
149		if (r != rnd[j].rf)
150			fprintf(stderr, "rounds expected=%x got=%x\n",
151			    rnd[j].rf, r);
152		test(r);
153	}
154#endif /* _FLOAT_IEEE754 */
155}
156
157ATF_TP_ADD_TCS(tp)
158{
159
160	ATF_TP_ADD_TC(tp, fpsetround_basic);
161
162	return atf_no_error();
163}
164