-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide.c
103 lines (87 loc) · 2.81 KB
/
divide.c
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
// gcc -O3 -o divide divide.c && ./divide
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "benchmark.hpp"
__attribute__((noinline)) uint64_t int_div64(uint64_t a, uint64_t b)
{
return a / b;
}
__attribute__((noinline)) double float_div64(double a, double b)
{
return a / b;
}
__attribute__((noinline)) uint32_t int_div32(uint32_t a, uint32_t b)
{
return a / b;
}
__attribute__((noinline)) double float_div32(float a, float b)
{
return a / b;
}
__attribute__((noinline)) void populate_int64(uint64_t num, uint64_t *array, uint64_t size)
{
for (uint64_t i = 1; i <= size; i++)
{
array[i] = int_div64(num, i);
}
}
__attribute__((noinline)) void populate_float64(uint64_t num, double *array, uint64_t size)
{
for (uint64_t i = 1; i <= size; i++)
{
array[i] = float_div64(num, i);
}
}
__attribute__((noinline)) void populate_int32divbyconstant(uint32_t *array, uint32_t size)
{
for (uint32_t i = 1; i <= size; i++)
{
array[i] = i / 17;
}
}
__attribute__((noinline)) void populate_int32(uint32_t num, uint32_t *array, uint32_t size)
{
for (uint32_t i = 1; i <= size; i++)
{
array[i] = int_div32(num, i);
}
}
__attribute__((noinline)) void populate_int32viafloat64(uint32_t num, uint32_t *array, uint32_t size)
{
for (uint32_t i = 1; i <= size; i++)
{
array[i] = (uint32_t)float_div64(num, i);
}
}
__attribute__((noinline)) void populate_float32(uint32_t num, float *array, uint32_t size)
{
for (uint32_t i = 1; i <= size; i++)
{
array[i] = float_div32(num, i);
}
}
void demo(size_t N)
{
double *farray64 = (double *)malloc(N * sizeof(double));
uint64_t *iarray64 = (uint64_t *)malloc(N * sizeof(uint64_t));
float *farray32 = (float *)malloc(N * sizeof(float));
uint32_t *iarray32 = (uint32_t *)malloc(N * sizeof(uint32_t));
int repeat = 5;
uint64_t inum64 = 1000000;
BEST_TIME_NOCHECK(populate_int32divbyconstant(iarray32, N), memset(iarray32, 0, N * sizeof(uint32_t)), repeat, N, true);
BEST_TIME_NOCHECK(populate_int64(inum64, iarray64, N), memset(iarray64, 0, N * sizeof(uint64_t)), repeat, N, true);
BEST_TIME_NOCHECK(populate_float64(inum64, farray64, N), memset(farray64, 0, N * sizeof(double)), repeat, N, true);
BEST_TIME_NOCHECK(populate_int32(inum64, iarray32, N), memset(iarray32, 0, N * sizeof(uint32_t)), repeat, N, true);
BEST_TIME_NOCHECK(populate_int32viafloat64(inum64, iarray32, N), memset(iarray32, 0, N * sizeof(uint32_t)), repeat, N, true);
BEST_TIME_NOCHECK(populate_float32(inum64, farray32, N), memset(farray32, 0, N * sizeof(float)), repeat, N, true);
}
int main(int argc, char **argv)
{
printf("clocks per s = %ld \n", CLOCKS_PER_SEC);
demo(1000);
return 0;
}