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