File: tensor.c

package info (click to toggle)
fitsh 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,768 kB
  • ctags: 4,050
  • sloc: ansic: 53,352; makefile: 1,120; sh: 25
file content (84 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download
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
/*****************************************************************************/
/* tensor.c 								     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Standalone library for allocating tensors with arbitrary rank and type.   */
/* (c) 2004, 2005; Pal, A. (apal@szofi.elte.hu).	 		     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* See function prototypes and the usage of the functions in tensor.h	     */
/*****************************************************************************/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include "tensor.h"

/*****************************************************************************/

void *tensor_alloc_arr(int typesize,int rank,int *arr)
{
 int	tsize,psize,bsize,cd,cb,snext,pnext;
 int	i,j;
 void	*ret,**pret;

 if ( rank<=0 )
	return(NULL);

 psize=0;bsize=1;
 for ( i=rank-1 ; i>=1 ; i-- )
  {	bsize=bsize*arr[i];
	psize+=bsize;
  }
 psize=psize*sizeof(void *);
 tsize=psize+typesize*bsize*arr[0];
 pret=(void **)malloc(tsize);
 if ( pret==NULL )	return(NULL);

 psize=0;bsize=1;
 for ( i=rank-1 ; i>=1 ; i-- )
  {	cd=arr[i];
	if ( i>1 )	snext=sizeof(void *);
	else		snext=typesize;
	cb=cd*bsize;
	pnext=psize+cb;
	for ( j=0 ; j<cb ; j++ )
		pret[psize+j]=(void *)( ((char *)(&pret[pnext]))+j*snext*arr[i-1] );
	psize=pnext;
	bsize=cb;
  }
 ret=(void *)pret;
 return(ret);
}
void *tensor_alloc(int typesize,int rank,...)
{
 int		ip_static[16],*ip_dynamic,*ip,i;
 void		*ret;
 va_list	ap;

 if ( rank<=16 )
  {	ip=ip_static;
	ip_dynamic=NULL;
  }
 else
  {	ip_dynamic=(int *)malloc(sizeof(int)*rank);
	ip=ip_dynamic;
  }
 va_start(ap,rank);
 for ( i=0 ; i<rank ; i++ )
  {	ip[i]=va_arg(ap,int);		}
 va_end(ap);
 ret=tensor_alloc_arr(typesize,rank,ip);
 if ( ip_dynamic != NULL )	free(ip_dynamic);
 return(ret);
}

int tensor_free(void *tensor)
{
 free(tensor);
 return(0);
}

/*****************************************************************************/