Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:33:24

0001 #include <stdlib.h>
0002 #include <stdio.h>
0003 #include "array.h"
0004 
0005 #ifndef NULL
0006 #define NULL 0
0007 #endif
0008 
0009 double **new_Array2D_double(int M, int N) {
0010   int i = 0;
0011   int failed = 0;
0012 
0013   double **A = (double **)malloc(sizeof(double *) * M);
0014   if (A == NULL)
0015     return NULL;
0016 
0017   for (i = 0; i < M; i++) {
0018     A[i] = (double *)malloc(N * sizeof(double));
0019     if (A[i] == NULL) {
0020       failed = 1;
0021       break;
0022     }
0023   }
0024 
0025   /* if we didn't successfully allocate all rows of A      */
0026   /* clean up any allocated memory (i.e. go back and free  */
0027   /* previous rows) and return NULL                        */
0028 
0029   if (failed) {
0030     i--;
0031     for (; i <= 0; i--)
0032       free(A[i]);
0033     free(A);
0034     return NULL;
0035   } else
0036     return A;
0037 }
0038 void Array2D_double_delete(int M, int N, double **A) {
0039   int i;
0040   if (A == NULL)
0041     return;
0042 
0043   for (i = 0; i < M; i++)
0044     free(A[i]);
0045 
0046   free(A);
0047 }
0048 
0049 void Array2D_double_copy(int M, int N, double **B, double **A) {
0050   int remainder = N & 3; /* N mod 4; */
0051   int i = 0;
0052   int j = 0;
0053 
0054   for (i = 0; i < M; i++) {
0055     double *Bi = B[i];
0056     double *Ai = A[i];
0057     for (j = 0; j < remainder; j++)
0058       Bi[j] = Ai[j];
0059     for (j = remainder; j < N; j += 4) {
0060       Bi[j] = Ai[j];
0061       Bi[j + 1] = Ai[j + 1];
0062       Bi[j + 2] = Ai[j + 2];
0063       Bi[j + 3] = Ai[j + 3];
0064     }
0065   }
0066 }