1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2015 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#include <stdio.h>
25b8e80941Smrg#include <stdbool.h>
26b8e80941Smrg#include <string.h>
27b8e80941Smrg#include <math.h>
28b8e80941Smrg
29b8e80941Smrg#include "macros.h"
30b8e80941Smrg#include "rounding.h"
31b8e80941Smrg
32b8e80941Smrgint main(int argc, char *argv[])
33b8e80941Smrg{
34b8e80941Smrg   const struct {
35b8e80941Smrg      float input, expected;
36b8e80941Smrg   } float_data[] = {
37b8e80941Smrg      { 0.0,                  0.0 },
38b8e80941Smrg      { nextafterf(0.5, 0.0), 0.0 },
39b8e80941Smrg      { 0.5,                  0.0 },
40b8e80941Smrg      { nextafterf(0.5, 1.0), 1.0 },
41b8e80941Smrg      { 1.0,                  1.0 },
42b8e80941Smrg      { nextafterf(1.5, 1.0), 1.0 },
43b8e80941Smrg      { 1.5,                  2.0 },
44b8e80941Smrg      { nextafterf(1.5, 2.0), 2.0 },
45b8e80941Smrg      { 2.0,                  2.0 },
46b8e80941Smrg      { nextafterf(2.5, 2.0), 2.0 },
47b8e80941Smrg      { 2.5,                  2.0 },
48b8e80941Smrg      { nextafterf(2.5, 3.0), 3.0 },
49b8e80941Smrg   };
50b8e80941Smrg
51b8e80941Smrg   const struct {
52b8e80941Smrg      double input, expected;
53b8e80941Smrg   } double_data[] = {
54b8e80941Smrg      { 0.0,                 0.0 },
55b8e80941Smrg      { nextafter(0.5, 0.0), 0.0 },
56b8e80941Smrg      { 0.5,                 0.0 },
57b8e80941Smrg      { nextafter(0.5, 1.0), 1.0 },
58b8e80941Smrg      { 1.0,                 1.0 },
59b8e80941Smrg      { nextafter(1.5, 1.0), 1.0 },
60b8e80941Smrg      { 1.5,                 2.0 },
61b8e80941Smrg      { nextafter(1.5, 2.0), 2.0 },
62b8e80941Smrg      { 2.0,                 2.0 },
63b8e80941Smrg      { nextafter(2.5, 2.0), 2.0 },
64b8e80941Smrg      { 2.5,                 2.0 },
65b8e80941Smrg      { nextafter(2.5, 3.0), 3.0 },
66b8e80941Smrg   };
67b8e80941Smrg
68b8e80941Smrg   bool failed = false;
69b8e80941Smrg   int i;
70b8e80941Smrg
71b8e80941Smrg   for (i = 0; i < ARRAY_SIZE(float_data); i++) {
72b8e80941Smrg      float output = _mesa_roundevenf(float_data[i].input);
73b8e80941Smrg      if (memcmp(&float_data[i].expected, &output, sizeof(float))) {
74b8e80941Smrg         fprintf(stderr, "%d float: expected %f (%a) from "
75b8e80941Smrg                         "_mesa_roundevenf(%f (%a)) but got %f (%a)\n",
76b8e80941Smrg                 i,
77b8e80941Smrg                 float_data[i].expected,
78b8e80941Smrg                 float_data[i].expected,
79b8e80941Smrg                 float_data[i].input,
80b8e80941Smrg                 float_data[i].input,
81b8e80941Smrg                 output,
82b8e80941Smrg                 output);
83b8e80941Smrg         failed = true;
84b8e80941Smrg      }
85b8e80941Smrg   }
86b8e80941Smrg
87b8e80941Smrg   /* Test negated values */
88b8e80941Smrg   for (i = 0; i < ARRAY_SIZE(float_data); i++) {
89b8e80941Smrg      float output = _mesa_roundevenf(-float_data[i].input);
90b8e80941Smrg      float negated_expected = -float_data[i].expected;
91b8e80941Smrg      if (memcmp(&negated_expected, &output, sizeof(float))) {
92b8e80941Smrg         fprintf(stderr, "%d float: expected %f (%a) from "
93b8e80941Smrg                         "_mesa_roundevenf(%f (%a)) but got %f (%a)\n",
94b8e80941Smrg                 i,
95b8e80941Smrg                 negated_expected,
96b8e80941Smrg                 negated_expected,
97b8e80941Smrg                 -float_data[i].input,
98b8e80941Smrg                 -float_data[i].input,
99b8e80941Smrg                 output,
100b8e80941Smrg                 output);
101b8e80941Smrg         failed = true;
102b8e80941Smrg      }
103b8e80941Smrg   }
104b8e80941Smrg
105b8e80941Smrg   for (i = 0; i < ARRAY_SIZE(double_data); i++) {
106b8e80941Smrg      double output = _mesa_roundeven(double_data[i].input);
107b8e80941Smrg      if (memcmp(&double_data[i].expected, &output, sizeof(double))) {
108b8e80941Smrg         fprintf(stderr, "%d double: expected %f (%a) from "
109b8e80941Smrg                         "_mesa_roundeven(%f (%a)) but got %f (%a)\n",
110b8e80941Smrg                 i,
111b8e80941Smrg                 double_data[i].expected,
112b8e80941Smrg                 double_data[i].expected,
113b8e80941Smrg                 double_data[i].input,
114b8e80941Smrg                 double_data[i].input,
115b8e80941Smrg                 output,
116b8e80941Smrg                 output);
117b8e80941Smrg         failed = true;
118b8e80941Smrg      }
119b8e80941Smrg   }
120b8e80941Smrg
121b8e80941Smrg   /* Test negated values */
122b8e80941Smrg   for (i = 0; i < ARRAY_SIZE(double_data); i++) {
123b8e80941Smrg      double output = _mesa_roundeven(-double_data[i].input);
124b8e80941Smrg      double negated_expected = -double_data[i].expected;
125b8e80941Smrg      if (memcmp(&negated_expected, &output, sizeof(double))) {
126b8e80941Smrg         fprintf(stderr, "%d double: expected %f (%a) from "
127b8e80941Smrg                         "_mesa_roundeven(%f (%a)) but got %f (%a)\n",
128b8e80941Smrg                 i,
129b8e80941Smrg                 negated_expected,
130b8e80941Smrg                 negated_expected,
131b8e80941Smrg                 -double_data[i].input,
132b8e80941Smrg                 -double_data[i].input,
133b8e80941Smrg                 output,
134b8e80941Smrg                 output);
135b8e80941Smrg         failed = true;
136b8e80941Smrg      }
137b8e80941Smrg   }
138b8e80941Smrg
139b8e80941Smrg   return failed;
140b8e80941Smrg}
141