1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: test1.c
8**
9** Purpose: Tests that scalbnf returns correct values.
10**
11**===================================================================*/
12
13#include <palsuite.h>
14
15// binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this
16// is slightly too accurate when writing tests meant to run against libm implementations
17// for various platforms. 2^-21 (approx. 4.76e-07) seems to be as accurate as we can get.
18//
19// The tests themselves will take PAL_EPSILON and adjust it according to the expected result
20// so that the delta used for comparison will compare the most significant digits and ignore
21// any digits that are outside the double precision range (6-9 digits).
22
23// For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON
24// for the variance, while an expected result in the format of 0.0xxxxxxxxx will use
25// PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10.
26#define PAL_EPSILON 4.76837158e-07
27
28#define PAL_NAN sqrtf(-1.0f)
29#define PAL_POSINF -logf(0.0f)
30#define PAL_NEGINF logf(0.0f)
31
32/**
33 * Helper test structure
34 */
35struct test
36{
37 float value; /* value to test the function with */
38 int exponent; /* exponent to test the function with */
39 float expected; /* expected result */
40 float variance; /* maximum delta between the expected and actual result */
41};
42
43/**
44 * validate
45 *
46 * test validation function
47 */
48void __cdecl validate(float value, int exponent, float expected, float variance)
49{
50 float result = scalbnf(value, exponent);
51
52 /*
53 * The test is valid when the difference between result
54 * and expected is less than or equal to variance
55 */
56 float delta = fabsf(result - expected);
57
58 if (delta > variance)
59 {
60 Fail("scalbnf(%g, %g) returned %10.9g when it should have returned %10.9g",
61 value, exponent, result, expected);
62 }
63}
64
65/**
66 * validate
67 *
68 * test validation function for values returning NaN
69 */
70void __cdecl validate_isnan(float value, int exponent)
71{
72 float result = scalbnf(value, exponent);
73
74 if (!_isnanf(result))
75 {
76 Fail("scalbnf(%g, %g) returned %10.9g when it should have returned %10.9g",
77 value, exponent, result, PAL_NAN);
78 }
79}
80
81/**
82 * main
83 *
84 * executable entry point
85 */
86int __cdecl main(int argc, char **argv)
87{
88 struct test tests[] =
89 {
90 /* value exponent expected variance */
91 { PAL_NEGINF, 0x80000000, PAL_NEGINF, 0 },
92 { 0, 0x80000000, 0, 0 },
93 { 0.113314732f, -3, 0.0141643415f, PAL_EPSILON / 10 },
94 { 0.151955223f, -2, 0.0379888058f, PAL_EPSILON / 10 },
95 { 0.202699566f, -2, 0.0506748916f, PAL_EPSILON / 10 },
96 { 0.336622537f, -1, 0.168311268f, PAL_EPSILON },
97 { 0.367879441f, -1, 0.183939721f, PAL_EPSILON },
98 { 0.375214227f, -1, 0.187607114f, PAL_EPSILON },
99 { 0.457429347f, -1, 0.228714674f, PAL_EPSILON },
100 { 0.5f, -1, 0.25f, PAL_EPSILON },
101 { 0.580191810f, 0, 0.580191810f, PAL_EPSILON },
102 { 0.612547327f, 0, 0.612547327f, PAL_EPSILON },
103 { 0.618503138f, 0, 0.618503138f, PAL_EPSILON },
104 { 0.643218242f, 0, 0.643218242f, PAL_EPSILON },
105 { 0.740055574f, 0, 0.740055574f, PAL_EPSILON },
106 { 0.802008879f, 0, 0.802008879f, PAL_EPSILON },
107 { 1, 0, 1, PAL_EPSILON * 10 },
108 { 1.24686899f, 0, 1.24686899f, PAL_EPSILON * 10 },
109 { 1.35124987f, 0, 1.35124987f, PAL_EPSILON * 10 },
110 { 1.55468228f, 0, 1.55468228f, PAL_EPSILON * 10 },
111 { 1.61680667f, 0, 1.61680667f, PAL_EPSILON * 10 },
112 { 1.63252692f, 0, 1.63252692f, PAL_EPSILON * 10 },
113 { 1.72356793f, 0, 1.72356793f, PAL_EPSILON * 10 },
114 { 2, 1, 4, PAL_EPSILON * 10 },
115 { 2.18612996f, 1, 4.37225992f, PAL_EPSILON * 10 },
116 { 2.66514414f, 1, 5.33028829f, PAL_EPSILON * 10 },
117 { 2.71828183f, 1, 5.43656366f, PAL_EPSILON * 10 },
118 { 2.97068642f, 1, 5.94137285f, PAL_EPSILON * 10 },
119 { 4.93340967f, 2, 19.7336387f, PAL_EPSILON * 100 },
120 { 6.58088599f, 2, 26.3235440f, PAL_EPSILON * 100 },
121 { 8.82497783f, 3, 70.5998226f, PAL_EPSILON * 100 },
122 { PAL_POSINF, 0x80000000, PAL_POSINF, 0 },
123 };
124
125 if (PAL_Initialize(argc, argv) != 0)
126 {
127 return FAIL;
128 }
129
130 for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
131 {
132 validate(tests[i].value, tests[i].exponent, tests[i].expected, tests[i].variance);
133 }
134
135 validate_isnan(PAL_NAN, 2147483647);
136
137 PAL_Terminate();
138 return PASS;
139}
140