-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith.cc
executable file
·154 lines (133 loc) · 4.58 KB
/
arith.cc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* arith.cc
*
* Created on: Dec 29, 2008
* Author: root
*/
#include "simlab.h"
#include <stdio.h>
#ifdef JAVA
#include <jni.h>
#else
#ifdef WIN
#define JNIEXPORT __declspec(dllexport)
#else
#define JNIEXPORT extern "C"
#endif
#endif
extern simlab data;
extern c_const<int> iconst;
extern c_const<float> fconst;
template <typename T, typename K> void t_set( T buffer, int length, K value, int vallen ) {
int len = vallen > length ? vallen : length;
for( int i = 0; i < len; i++ ) {
buffer[i] = value[i];
}
}
template <typename T, typename K> void t_add( T buffer, int length, K value, int vallen ) {
int len = vallen > length ? vallen : length;
for( int i = 0; i < len; i++ ) {
buffer[i] += value[i];
}
}
template <typename T, typename K> void t_mul( T t, int tlen, K k, int klen ) {
for( int i = 0; i < (tlen < klen ? tlen : klen); i++ ) {
t[i] *= k[i];
}
}
template <typename T, typename K> void t_sub( T buffer, int length, K value, int vallen ) {
int len = vallen > length ? vallen : length;
for( int i = 0; i < len; i++ ) {
buffer[i] -= value[i];
}
}
template <typename T, typename K> void t_div( T t, int tlen, K k, int klen ) {
for( int i = 0; i < (tlen < klen ? tlen : klen); i++ ) {
t[i] /= k[i];
}
}
template <typename T, typename K> class c_set {
public:
c_set( T tbuf, int tlen, K kbuf, int klen ) {
t_set<T,K>( tbuf, tlen, kbuf, klen );
}
};
template <typename T, typename K> class c_mul {
public:
c_mul( T tbuf, int tlen, K kbuf, int klen ) {
t_mul<T,K>( tbuf, tlen, kbuf, klen );
}
};
template <typename T, typename K> class c_add {
public:
c_add( T tbuf, int tlen, K kbuf, int klen ) {
t_add<T,K>( tbuf, tlen, kbuf, klen );
}
};
template <typename T, typename K> class c_sub {
public:
c_sub( T tbuf, int tlen, K kbuf, int klen ) {
t_sub<T,K>( tbuf, tlen, kbuf, klen );
}
};
template <typename T, typename K> class c_div {
public:
c_div( T tbuf, int tlen, K kbuf, int klen ) {
t_div<T,K>( tbuf, tlen, kbuf, klen );
}
};
template<template<typename T, typename V> class c_func, typename K> void subarith( K kbuf, int klen ) {
if( data.length == -1 ) {
} else if( data.type < 0 ) {
if( data.type == -66 ) c_func< c_simlab<double&>&,K >( *(c_simlab<double&>*)data.buffer, data.length, kbuf, klen );
else if( data.type == -34 ) c_func< c_simlab<float&>&,K >( *(c_simlab<float&>*)data.buffer, data.length, kbuf, klen );
else if( data.type == -32 ) c_func< c_simlab<int&>&,K >( *(c_simlab<int&>*)data.buffer, data.length, kbuf, klen );
else if( data.type == -16 ) c_func< c_simlab<short&>&,K >( *(c_simlab<short&>*)data.buffer, data.length, kbuf, klen );
} else {
if( data.type == 66 ) c_func<double*,K>( (double*)data.buffer, data.length, kbuf, klen );
else if( data.type == 34 ) c_func<float*,K>( (float*)data.buffer, data.length, kbuf, klen );
else if( data.type == 32 ) c_func<int*,K>( (int*)data.buffer, data.length, kbuf, klen );
else if( data.type == 16 ) c_func<short*,K>( (short*)data.buffer, data.length, kbuf, klen );
}
}
template<template<typename T, typename K> class c_func> void arith( simlab & value ) {
if( value.length == 0 ) {
if( value.type == 32 ) {
c_const<int> sl( *(int*)&value.buffer );
subarith< c_func, c_simlab<int>& >( sl, data.length );
} else if( value.type == 34 ) {
c_const<float> sl( *(float*)&value.buffer );
subarith< c_func, c_simlab<float>& >( sl, data.length );
}
} else if( value.length == -1 ) {
if( value.type == -66 ) subarith< c_func, c_simlab<double>& >( *(c_simlab<double>*)value.buffer, data.length );
else if( value.type == -34 ) subarith< c_func, c_simlab<float>& >( *((c_simlab<float>*)value.buffer), data.length );
else if( value.type == -32 ) subarith< c_func, c_simlab<int>& >( *((c_simlab<int>*)value.buffer), data.length );
else if( value.type == -16 ) subarith< c_func, c_simlab<short>& >( *((c_simlab<short>*)value.buffer), data.length );
} else {
if( value.type == 66 ) subarith< c_func, double* >( (double*)value.buffer, data.length );
else if( value.type == 34 ) subarith< c_func, float* >( (float*)value.buffer, data.length );
else if( value.type == 32 ) subarith< c_func, int* >( (int*)value.buffer, data.length );
else if( value.type == 16 ) subarith< c_func, short* >( (short*)value.buffer, data.length );
}
}
JNIEXPORT int set( simlab value ) {
arith< c_set >( value );
return 1;
}
JNIEXPORT int add( simlab value ) {
arith< c_add >( value );
return 1;
}
JNIEXPORT int mul( simlab value ) {
arith< c_mul >( value );
return 1;
}
JNIEXPORT int sub( simlab value ) {
arith< c_sub >( value );
return 1;
}
JNIEXPORT int div( simlab value ) {
arith< c_div >( value );
return 1;
}