forked from sysprog21/rv32emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
63 lines (54 loc) · 1.37 KB
/
array.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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "array.h"
double **new_Array2D_double(int M, int N)
{
int i = 0;
bool failed = false;
double **A = (double **) malloc(sizeof(double *) * M);
if (A == NULL)
return NULL;
for (i = 0; i < M; i++) {
A[i] = (double *) malloc(N * sizeof(double));
if (A[i] == NULL) {
failed = true;
break;
}
}
/* if we didn't successfully allocate all rows of A */
/* clean up any allocated memory (i.e. go back and free */
/* previous rows) and return NULL */
if (failed) {
i--;
for (; i <= 0; i--)
free(A[i]);
free(A);
return NULL;
}
return A;
}
void Array2D_double_delete(int M, int N, double **A)
{
if (A == NULL)
return;
for (int i = 0; i < M; i++)
free(A[i]);
free(A);
}
void Array2D_double_copy(int M, int N, double **B, double **A)
{
int remainder = N & 3; /* N mod 4; */
for (int i = 0; i < M; i++) {
double *Bi = B[i];
double *Ai = A[i];
for (int j = 0; j < remainder; j++)
Bi[j] = Ai[j];
for (int j = remainder; j < N; j += 4) {
Bi[j] = Ai[j];
Bi[j + 1] = Ai[j + 1];
Bi[j + 2] = Ai[j + 2];
Bi[j + 3] = Ai[j + 3];
}
}
}