1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2021 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 <math.h> 25bf215546Sopenharmony_ci#include <gtest/gtest.h> 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "util/half_float.h" 28bf215546Sopenharmony_ci#include "util/u_math.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci/* math.h has some defines for these, but they have some compiler dependencies 31bf215546Sopenharmony_ci * and can potentially raise exceptions. 32bf215546Sopenharmony_ci */ 33bf215546Sopenharmony_ci#define TEST_POS_INF (uif(0x7f800000)) 34bf215546Sopenharmony_ci#define TEST_NEG_INF (uif(0xff800000)) 35bf215546Sopenharmony_ci#define TEST_NAN (uif(0x7fc00000)) 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#define HALF_POS_INF 0x7c00 38bf215546Sopenharmony_ci#define HALF_NEG_INF 0xfc00 39bf215546Sopenharmony_ci#define HALF_NAN 0x7e00 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci#ifndef HAVE_ISSIGNALING 42bf215546Sopenharmony_cistatic bool issignaling(float x) 43bf215546Sopenharmony_ci{ 44bf215546Sopenharmony_ci uint32_t ui = fui(x); 45bf215546Sopenharmony_ci return (((ui >> 23) & 0xff) == 0xff) && !(ui & (1 << 22)); 46bf215546Sopenharmony_ci} 47bf215546Sopenharmony_ci#endif 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci/* Sanity test our test values */ 50bf215546Sopenharmony_ciTEST(half_to_float_test, nan_test) 51bf215546Sopenharmony_ci{ 52bf215546Sopenharmony_ci EXPECT_TRUE(isinf(TEST_POS_INF)); 53bf215546Sopenharmony_ci EXPECT_TRUE(isinf(TEST_NEG_INF)); 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci EXPECT_TRUE(isnan(TEST_NAN)); 56bf215546Sopenharmony_ci /* Make sure that our 32-bit float nan test value we're using is a 57bf215546Sopenharmony_ci * non-signaling NaN. The sign of the bit for signaling was apparently 58bf215546Sopenharmony_ci * different on some old processors (PA-RISC, MIPS?). This test value should 59bf215546Sopenharmony_ci * cover Intel, ARM, and PPC, for sure. 60bf215546Sopenharmony_ci */ 61bf215546Sopenharmony_ci EXPECT_FALSE(issignaling(TEST_NAN)); 62bf215546Sopenharmony_ci} 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_cistatic void 65bf215546Sopenharmony_citest_half_to_float_limits(float (*func)(uint16_t)) 66bf215546Sopenharmony_ci{ 67bf215546Sopenharmony_ci /* Positive and negative 0. */ 68bf215546Sopenharmony_ci EXPECT_EQ(func(0), 0.0f); 69bf215546Sopenharmony_ci EXPECT_EQ(fui(func(0x8000)), fui(-0.0f)); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci /* Max normal number */ 72bf215546Sopenharmony_ci EXPECT_EQ(func(0x7bff), 65504.0f); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci float nan = func(HALF_NAN); 75bf215546Sopenharmony_ci EXPECT_TRUE(isnan(nan)); 76bf215546Sopenharmony_ci EXPECT_FALSE(issignaling(nan)); 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci /* inf */ 79bf215546Sopenharmony_ci EXPECT_EQ(func(HALF_POS_INF), TEST_POS_INF); 80bf215546Sopenharmony_ci /* -inf */ 81bf215546Sopenharmony_ci EXPECT_EQ(func(HALF_NEG_INF), TEST_NEG_INF); 82bf215546Sopenharmony_ci} 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci/* Test the optionally HW instruction-using path. */ 85bf215546Sopenharmony_ciTEST(half_to_float_test, half_to_float_test) 86bf215546Sopenharmony_ci{ 87bf215546Sopenharmony_ci test_half_to_float_limits(_mesa_half_to_float); 88bf215546Sopenharmony_ci} 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ciTEST(half_to_float_test, half_to_float_slow_test) 91bf215546Sopenharmony_ci{ 92bf215546Sopenharmony_ci test_half_to_float_limits(_mesa_half_to_float_slow); 93bf215546Sopenharmony_ci} 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_cistatic void 96bf215546Sopenharmony_citest_float_to_half_limits(uint16_t (*func)(float)) 97bf215546Sopenharmony_ci{ 98bf215546Sopenharmony_ci /* Positive and negative 0. */ 99bf215546Sopenharmony_ci EXPECT_EQ(func(0.0f), 0); 100bf215546Sopenharmony_ci EXPECT_EQ(func(-0.0f), 0x8000); 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci /* Max normal number */ 103bf215546Sopenharmony_ci EXPECT_EQ(func(65504.0f), 0x7bff); 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci uint16_t nan = func(TEST_NAN); 106bf215546Sopenharmony_ci EXPECT_EQ((nan & 0xfc00), 0x7c00); /* exponent is all 1s */ 107bf215546Sopenharmony_ci EXPECT_TRUE(nan & (1 << 9)); /* mantissa is quiet nan */ 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci EXPECT_EQ(func(TEST_POS_INF), HALF_POS_INF); 110bf215546Sopenharmony_ci EXPECT_EQ(func(TEST_NEG_INF), HALF_NEG_INF); 111bf215546Sopenharmony_ci} 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ciTEST(float_to_half_test, float_to_half_test) 114bf215546Sopenharmony_ci{ 115bf215546Sopenharmony_ci test_float_to_half_limits(_mesa_float_to_half); 116bf215546Sopenharmony_ci} 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ciTEST(float_to_float16_rtne_test, float_to_float16_rtne_test) 119bf215546Sopenharmony_ci{ 120bf215546Sopenharmony_ci test_float_to_half_limits(_mesa_float_to_float16_rtne); 121bf215546Sopenharmony_ci} 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci/* no rtne_slow variant -- rtne is just _mesa_float_to_half(). */ 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ciTEST(float_to_float16_rtz_test, float_to_float16_rtz_test) 126bf215546Sopenharmony_ci{ 127bf215546Sopenharmony_ci test_float_to_half_limits(_mesa_float_to_float16_rtz); 128bf215546Sopenharmony_ci} 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ciTEST(float_to_float16_rtz_slow_test, float_to_float16_rtz_test) 131bf215546Sopenharmony_ci{ 132bf215546Sopenharmony_ci test_float_to_half_limits(_mesa_float_to_float16_rtz_slow); 133bf215546Sopenharmony_ci} 134