-
Notifications
You must be signed in to change notification settings - Fork 21
/
sample2.c
68 lines (66 loc) · 1.65 KB
/
sample2.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
#include <stdio.h>
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include "SFMT.h"
int main(int argc, char* argv[]) {
int i, j, cnt, seed;
double x, y, pi;
const int NUM = 10000;
const int R_SIZE = 2 * NUM;
int size;
uint64_t *array;
sfmt_t sfmt;
if (argc >= 2) {
seed = strtol(argv[1], NULL, 10);
} else {
seed = 12345;
}
size = sfmt_get_min_array_size64(&sfmt);
if (size < R_SIZE) {
size = R_SIZE;
}
#if defined(__APPLE__) || \
(defined(__FreeBSD__) && __FreeBSD__ >= 3 && __FreeBSD__ <= 6)
printf("malloc used\n");
array = malloc(sizeof(double) * size);
if (array == NULL) {
printf("can't allocate memory.\n");
return 1;
}
#elif defined(_POSIX_C_SOURCE)
printf("posix_memalign used\n");
if (posix_memalign((void **)&array, 16, sizeof(double) * size) != 0) {
printf("can't allocate memory.\n");
return 1;
}
#elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
printf("memalign used\n");
array = memalign(16, sizeof(double) * size);
if (array == NULL) {
printf("can't allocate memory.\n");
return 1;
}
#else /* in this case, gcc doesn't suppport SSE2 */
printf("malloc used\n");
array = malloc(sizeof(double) * size);
if (array == NULL) {
printf("can't allocate memory.\n");
return 1;
}
#endif
cnt = 0;
j = 0;
sfmt_init_gen_rand(&sfmt, seed);
sfmt_fill_array64(&sfmt, array, size);
for (i = 0; i < NUM; i++) {
x = sfmt_to_res53(array[j++]);
y = sfmt_to_res53(array[j++]);
if (x * x + y * y < 1.0) {
cnt++;
}
}
free(array);
pi = (double)cnt / NUM * 4;
printf("%lf\n", pi);
return 0;
}