1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <time.h>
30 #include <assert.h>
31 #include "wayland-private.h"
32
33 volatile double global_d;
34
35 static void
noop_conversion(void)36 noop_conversion(void)
37 {
38 wl_fixed_t f;
39 union {
40 int64_t i;
41 double d;
42 } u;
43
44 for (f = 0; f < INT32_MAX; f++) {
45 u.i = f;
46 global_d = u.d;
47 }
48 }
49
50 static void
magic_conversion(void)51 magic_conversion(void)
52 {
53 wl_fixed_t f;
54
55 for (f = 0; f < INT32_MAX; f++)
56 global_d = wl_fixed_to_double(f);
57 }
58
59 static void
mul_conversion(void)60 mul_conversion(void)
61 {
62 wl_fixed_t f;
63
64 /* This will get optimized into multiplication by 1/256 */
65 for (f = 0; f < INT32_MAX; f++)
66 global_d = f / 256.0;
67 }
68
69 double factor = 256.0;
70
71 static void
div_conversion(void)72 div_conversion(void)
73 {
74 wl_fixed_t f;
75
76 for (f = 0; f < INT32_MAX; f++)
77 global_d = f / factor;
78 }
79
80 static void
benchmark(const char * s,void (* f)(void))81 benchmark(const char *s, void (*f)(void))
82 {
83 struct timespec start, stop, elapsed;
84
85 clock_gettime(CLOCK_MONOTONIC, &start);
86 f();
87 clock_gettime(CLOCK_MONOTONIC, &stop);
88
89 elapsed.tv_sec = stop.tv_sec - start.tv_sec;
90 elapsed.tv_nsec = stop.tv_nsec - start.tv_nsec;
91 if (elapsed.tv_nsec < 0) {
92 elapsed.tv_nsec += 1000000000;
93 elapsed.tv_sec--;
94 }
95 printf("benchmarked %s:\t%ld.%09lds\n",
96 s, elapsed.tv_sec, elapsed.tv_nsec);
97 }
98
main(void)99 int main(void)
100 {
101 benchmark("noop", noop_conversion);
102 benchmark("magic", magic_conversion);
103 benchmark("div", div_conversion);
104 benchmark("mul", mul_conversion);
105
106 return 0;
107 }
108