cf2e9e02ec808c59975cee1cdb0d1138607e3899
[platform/upstream/glibc.git] / sysdeps / x86_64 / fpu / bench-libmvec-skeleton.c
1 /* Skeleton for libmvec benchmark programs.
2    Copyright (C) 2021-2023 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <time.h>
24 #include <inttypes.h>
25 #include <bench-timing.h>
26 #include <json-lib.h>
27 #include <bench-util.h>
28 #include <math-tests-arch.h>
29
30 #include <bench-util.c>
31 #define D_ITERS 10000
32
33 int
34 main (int argc, char **argv)
35 {
36   unsigned long i, k;
37   timing_t start, end;
38   json_ctx_t json_ctx;
39
40 #if defined REQUIRE_AVX
41   if (!CPU_FEATURE_ACTIVE (AVX))
42     {
43       printf ("AVX not supported.");
44       return 77;
45     }
46 #elif defined REQUIRE_AVX2
47   if (!CPU_FEATURE_ACTIVE (AVX2))
48     {
49       printf ("AVX2 not supported.");
50       return 77;
51     }
52 #elif defined REQUIRE_AVX512F
53   if (!CPU_FEATURE_ACTIVE (AVX512F))
54     {
55       printf ("AVX512F not supported.");
56       return 77;
57     }
58 #endif
59
60   bench_start ();
61
62 #ifdef BENCH_INIT
63   BENCH_INIT ();
64 #endif
65
66   json_init (&json_ctx, 2, stdout);
67
68   /* Begin function.  */
69   json_attr_object_begin (&json_ctx, FUNCNAME);
70
71   for (int v = 0; v < NUM_VARIANTS; v++)
72     {
73       double d_total_time = 0;
74       timing_t cur;
75       for (k = 0; k < D_ITERS; k++)
76         {
77           TIMING_NOW (start);
78           for (i = 0; i < NUM_SAMPLES (v); i++)
79             BENCH_FUNC (v, i);
80           TIMING_NOW (end);
81
82           TIMING_DIFF (cur, start, end);
83
84           TIMING_ACCUM (d_total_time, cur);
85         }
86       double d_total_data_set = D_ITERS * NUM_SAMPLES (v) * STRIDE;
87
88       /* Begin variant.  */
89       json_attr_object_begin (&json_ctx, VARIANT (v));
90
91       json_attr_double (&json_ctx, "duration", d_total_time);
92       json_attr_double (&json_ctx, "iterations", d_total_data_set);
93       json_attr_double (&json_ctx, "mean", d_total_time / d_total_data_set);
94
95       /* End variant.  */
96       json_attr_object_end (&json_ctx);
97     }
98
99   /* End function.  */
100   json_attr_object_end (&json_ctx);
101
102   return 0;
103 }