forked from riscv-non-isa/rvv-intrinsic-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
106 lines (92 loc) · 2.54 KB
/
common.h
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
// common.h
// common utilites for the test code under exmaples/
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
void gen_rand_1d(double *a, int n) {
for (int i = 0; i < n; ++i)
a[i] = (double)rand() / (double)RAND_MAX + (double)(rand() % 1000);
}
void gen_string(char *s, int n) {
// char value range: -128 ~ 127
for (int i = 0; i < n - 1; ++i)
s[i] = (char)(rand() % 127) + 1;
s[n - 1] = '\0';
}
void gen_rand_2d(double **ar, int n, int m) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
ar[i][j] = (double)rand() / (double)RAND_MAX + (double)(rand() % 1000);
}
void print_string(const char *a, const char *name) {
printf("const char *%s = \"", name);
int i = 0;
while (a[i] != 0)
putchar(a[i++]);
printf("\"\n");
puts("");
}
void print_array_1d(double *a, int n, const char *type, const char *name) {
printf("%s %s[%d] = {\n", type, name, n);
for (int i = 0; i < n; ++i) {
printf("%06.2f%s", a[i], i != n - 1 ? "," : "};\n");
if (i % 10 == 9)
puts("");
}
puts("");
}
void print_array_2d(double **a, int n, int m, const char *type,
const char *name) {
printf("%s %s[%d][%d] = {\n", type, name, n, m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
printf("%06.2f", a[i][j]);
if (j == m - 1)
puts(i == n - 1 ? "};" : ",");
else
putchar(',');
}
}
puts("");
}
bool double_eq(double golden, double actual, double relErr) {
return (fabs(actual - golden) < relErr);
}
bool compare_1d(double *golden, double *actual, int n) {
for (int i = 0; i < n; ++i)
if (!double_eq(golden[i], actual[i], 1e-6))
return false;
return true;
}
bool compare_string(const char *golden, const char *actual, int n) {
for (int i = 0; i < n; ++i)
if (golden[i] != actual[i])
return false;
return true;
}
bool compare_2d(double **golden, double **actual, int n, int m) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (!double_eq(golden[i][j], actual[i][j], 1e-6))
return false;
return true;
}
double **alloc_array_2d(int n, int m) {
double **ret;
ret = (double **)malloc(sizeof(double *) * n);
for (int i = 0; i < n; ++i)
ret[i] = (double *)malloc(sizeof(double) * m);
return ret;
}
void init_array_one_1d(double *ar, int n) {
for (int i = 0; i < n; ++i)
ar[i] = 1;
}
void init_array_one_2d(double **ar, int n, int m) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
ar[i][j] = 1;
}