about summary refs log tree commit diff
path: root/benchtests/bench-math-inlines.c
blob: 9042608fde6011033bb3cd0f87cdffcfefd5b231 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Measure math inline functions.
   Copyright (C) 2015-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#define SIZE 1024
#define TEST_MAIN
#define TEST_NAME "math-inlines"
#define TEST_FUNCTION test_main ()
#include "bench-timing.h"
#include "json-lib.h"
#include "bench-util.h"

#include <stdlib.h>
#include <math.h>
#include <stdint.h>

#define BOOLTEST(func)					  \
static int __attribute__((noinline))			  \
func ## _f (double d, int i)				  \
{							  \
  if (func (d))						  \
    return (int) d + i;					  \
  else							  \
    return 5;						  \
}							  \
static int						  \
func ## _t (volatile double *p, size_t n, size_t iters)   \
{							  \
  int i, j;						  \
  int res = 0;						  \
  for (j = 0; j < iters; j++)				  \
    for (i = 0; i < n; i++)				  \
      if (func ## _f (p[i] * 2.0, i) != 0)		  \
	res += 5;					  \
  return res;						  \
}

#define VALUETEST(func)					  \
static int __attribute__((noinline))			  \
func ## _f (double d)					  \
{							  \
  return func (d);					  \
}							  \
static int						  \
func ## _t (volatile double *p, size_t n, size_t iters)	  \
{							  \
  int i, j;						  \
  int res = 0;						  \
  for (j = 0; j < iters; j++)				  \
    for (i = 0; i < n; i++)				  \
      res += func ## _f (p[i] * 2.0);			  \
  return res;						  \
}

typedef union
{
  double value;
  uint64_t word;
} ieee_double_shape_type;

#define EXTRACT_WORDS64(i,d)				  \
do {							  \
  ieee_double_shape_type gh_u;				  \
  gh_u.value = (d);					  \
  (i) = gh_u.word;					  \
} while (0)

/* Inlines similar to existing math_private.h versions.  */

static __always_inline int
__isnan_inl (double d)
{
  uint64_t di;
  EXTRACT_WORDS64 (di, d);
  return (di & 0x7fffffffffffffffull) > 0x7ff0000000000000ull;
}

static __always_inline int
__isinf_ns2 (double d)
{
  uint64_t di;
  EXTRACT_WORDS64 (di, d);
  return (di & 0x7fffffffffffffffull) == 0x7ff0000000000000ull;
}

static __always_inline int
__finite_inl (double d)
{
  uint64_t di;
  EXTRACT_WORDS64 (di, d);
  return (di & 0x7fffffffffffffffull) < 0x7ff0000000000000ull;
}

#define __isnormal_inl(X) (__fpclassify (X) == FP_NORMAL)

/* Inlines for the builtin functions.  */

#define __isnan_builtin(X) __builtin_isnan (X)
#define __isinf_ns_builtin(X) __builtin_isinf (X)
#define __isinf_builtin(X) __builtin_isinf_sign (X)
#define __isfinite_builtin(X) __builtin_isfinite (X)
#define __isnormal_builtin(X) __builtin_isnormal (X)
#define __fpclassify_builtin(X) __builtin_fpclassify (FP_NAN, FP_INFINITE,  \
				  FP_NORMAL, FP_SUBNORMAL, FP_ZERO, (X))

static double __attribute ((noinline))
kernel_standard (double x, double y, int z)
{
  return x * y + z;
}

volatile double rem1 = 2.5;

static __always_inline double
remainder_test1 (double x)
{
  double y = rem1;
  if (((__builtin_expect (y == 0.0, 0) && !__isnan_inl (x))
	|| (__builtin_expect (__isinf_ns2 (x), 0) && !__isnan_inl (y))))
    return kernel_standard (x, y, 10);

  return remainder (x, y);
}

static __always_inline double
remainder_test2 (double x)
{
  double y = rem1;
  if (((__builtin_expect (y == 0.0, 0) && !__builtin_isnan (x))
	|| (__builtin_expect (__builtin_isinf (x), 0) && !__builtin_isnan (y))))
    return kernel_standard (x, y, 10);

  return remainder (x, y);
}

/* Create test functions for each possibility.  */

BOOLTEST (__isnan)
BOOLTEST (__isnan_inl)
BOOLTEST (__isnan_builtin)
BOOLTEST (isnan)

BOOLTEST (__isinf)
BOOLTEST (__isinf_builtin)
BOOLTEST (__isinf_ns2)
BOOLTEST (__isinf_ns_builtin)
BOOLTEST (isinf)

BOOLTEST (__finite)
BOOLTEST (__finite_inl)
BOOLTEST (__isfinite_builtin)
BOOLTEST (isfinite)

BOOLTEST (__isnormal_inl)
BOOLTEST (__isnormal_builtin)
BOOLTEST (isnormal)

VALUETEST (__fpclassify)
VALUETEST (__fpclassify_builtin)
VALUETEST (fpclassify)

VALUETEST (remainder_test1)
VALUETEST (remainder_test2)

typedef int (*proto_t) (volatile double *p, size_t n, size_t iters);

typedef struct
{
  const char *name;
  proto_t fn;
} impl_t;

#define IMPL(name) { #name, name ## _t }

static impl_t test_list[] =
{
  IMPL (__isnan),
  IMPL (__isnan_inl),
  IMPL (__isnan_builtin),
  IMPL (isnan),

  IMPL (__isinf),
  IMPL (__isinf_ns2),
  IMPL (__isinf_ns_builtin),
  IMPL (__isinf_builtin),
  IMPL (isinf),

  IMPL (__finite),
  IMPL (__finite_inl),
  IMPL (__isfinite_builtin),
  IMPL (isfinite),

  IMPL (__isnormal_inl),
  IMPL (__isnormal_builtin),
  IMPL (isnormal),

  IMPL (__fpclassify),
  IMPL (__fpclassify_builtin),
  IMPL (fpclassify),

  IMPL (remainder_test1),
  IMPL (remainder_test2)
};

static void
do_one_test (json_ctx_t *json_ctx, proto_t test_fn, volatile double *arr,
	     size_t len, const char *testname)
{
  size_t iters = 2048;
  timing_t start, stop, cur;

  json_attr_object_begin (json_ctx, testname);

  TIMING_NOW (start);
  test_fn (arr, len, iters);
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  json_attr_double (json_ctx, "duration", cur);
  json_attr_double (json_ctx, "iterations", iters);
  json_attr_double (json_ctx, "mean", cur / iters);
  json_attr_object_end (json_ctx);
}

static volatile double arr1[SIZE];
static volatile double arr2[SIZE];

int
test_main (void)
{
  json_ctx_t json_ctx;
  size_t i;

  bench_start ();

  json_init (&json_ctx, 2, stdout);
  json_attr_object_begin (&json_ctx, TEST_NAME);

  /* Create 2 test arrays, one with 10% zeroes, 10% negative values,
     79% positive values and 1% infinity/NaN.  The other contains
     50% inf, 50% NaN.  This relies on rand behaving correctly.  */

  for (i = 0; i < SIZE; i++)
    {
      int x = rand () & 255;
      arr1[i] = (x < 25) ? 0.0 : ((x < 50) ? -1 : 100);
      if (x == 255) arr1[i] = __builtin_inf ();
      if (x == 254) arr1[i] = __builtin_nan ("0");
      arr2[i] = (x < 128) ? __builtin_inf () : __builtin_nan ("0");
    }

  for (i = 0; i < sizeof (test_list) / sizeof (test_list[0]); i++)
    {
      json_attr_object_begin (&json_ctx, test_list[i].name);
      do_one_test (&json_ctx, test_list[i].fn, arr2, SIZE, "inf/nan");
      json_attr_object_end (&json_ctx);
    }

  for (i = 0; i < sizeof (test_list) / sizeof (test_list[0]); i++)
    {
      json_attr_object_begin (&json_ctx, test_list[i].name);
      do_one_test (&json_ctx, test_list[i].fn, arr1, SIZE, "normal");
      json_attr_object_end (&json_ctx);
    }

  json_attr_object_end (&json_ctx);
  return 0;
}

#include "bench-util.c"
#include "../test-skeleton.c"