numru-narray 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,212 @@
1
+ $type_codes = %w(n B I L G F D X C O)
2
+ $data_types =
3
+ %w(none u_int8_t int16_t int32_t int64_t float double scomplex dcomplex VALUE)
4
+ $real_types =
5
+ %w(none u_int8_t int16_t int32_t int64_t float double float double VALUE)
6
+ $int_types =
7
+ %w(none u_int8_t int16_t int32_t int64_t int32_t int32_t scomplex dcomplex VALUE)
8
+ $comp_types =
9
+ %w(none scomplex scomplex scomplex dcomplex scomplex dcomplex scomplex dcomplex VALUE)
10
+ $swap_types =
11
+ %w(none u_int8_t na_size16_t na_size32_t na_size64_t na_size32_t na_size64_t na_size64_t na_size128_t VALUE)
12
+ $upcast = [
13
+ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
14
+ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
15
+ [ 0, 2, 2, 3, 4, 5, 6, 7, 8, 9],
16
+ [ 0, 3, 3, 3, 4, 5, 6, 7, 8, 9],
17
+ [ 0, 4, 4, 4, 4, 5, 6, 7, 8, 9],
18
+ [ 0, 5, 5, 5, 5, 5, 6, 7, 8, 9],
19
+ [ 0, 6, 6, 6, 6, 6, 6, 8, 8, 9],
20
+ [ 0, 7, 7, 7, 7, 7, 8, 7, 8, 9],
21
+ [ 0, 8, 8, 8, 8, 8, 8, 8, 8, 9],
22
+ [ 0, 9, 9, 9, 9, 9, 9, 9, 9, 9] ]
23
+ $data_obj = [
24
+ [/[O]/,/[O]/, "
25
+ *p1 = rb_funcall(*p1,#id,1,*p2);"],
26
+ [/[O]/,/[BIL]/,"
27
+ *p1 = rb_funcall(*p1,#id,1,INT2FIX(*p2));"],
28
+ [/[O]/,/[G]/,"
29
+ *p1 = rb_funcall(*p1,#id,1,LL2NUM(*p2));"],
30
+ [/[O]/,/[FD]/, "
31
+ *p1 = rb_funcall(*p1,#id,1,rb_float_new(*p2));"],
32
+ [/[O]/,/[XC]/, "
33
+ *p1 = rb_funcall(*p1,#id,1,rb_complex_new(p2->r,p2->i));"],
34
+ [/[BIL]/,/[O]/,"
35
+ *p1 = NUM2INT(rb_funcall(INT2FIX(*p1),#id,1,*p2));"],
36
+ [/[G]/,/[O]/,"
37
+ *p1 = NUM2LL(rb_funcall(LL2NUM(*p1),#id,1,*p2));"],
38
+ [/[FD]/,/[O]/, "
39
+ *p1 = NUM2DBL(rb_funcall(rb_float_new(*p1),#id,1,*p2));"],
40
+ [/[XC]/,/[O]/, "VALUE v=rb_funcall(rb_complex_new(p1->r,p1->i),#id,1,*p2);
41
+ p1->r = NUM2REAL(v); p1->i = NUM2IMAG(v);"] ]
42
+
43
+
44
+ def mksetfuncs(name,op,id,funcs)
45
+
46
+ print "
47
+ /* ------------------------- #{name} --------------------------- */\n"
48
+ c = $type_codes
49
+ n = $type_codes.size
50
+ td = $data_types
51
+ tr = $real_types
52
+
53
+ # Function Definition
54
+
55
+ for i in 0...n
56
+ for j in 0...n
57
+ funcs.each do |k|
58
+ if c[i]=~k[0] && c[j]=~k[1]
59
+ #if i==j
60
+ # f = "memcpy(p1,p1,sizeof(typed));"
61
+ #else
62
+ f = k[2]
63
+ #end
64
+ f = f.
65
+ gsub(/p1\./,"((#{td[i]}*)(p1+i*i1))->").
66
+ gsub(/p2\./,"((#{td[j]}*)(p2+i*i2))->").
67
+ gsub(/\*p1/,"*((#{td[i]}*)(p1+i*i1))").
68
+ gsub(/\*p2/,"*((#{td[j]}*)(p2+i*i2))").
69
+ gsub(/ = /," = (#{tr[i]})").
70
+ gsub(/#id/,id).
71
+ gsub(/#op/,op).
72
+ gsub(/typed/,td[i]).
73
+ gsub(/typef/,tr[i])
74
+ puts $func_body.
75
+ gsub(/typed/,td[i]).
76
+ gsub(/typef/,tr[i]).
77
+ gsub(/#name/,name).
78
+ sub(/OPERATION/,f).
79
+ gsub(/#CC/,c[i]+c[j])
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ # function pointer array
86
+ print "\nna_setfunc_t "+name+"Funcs = {\n"
87
+ m = []
88
+ for i in 0...n
89
+ l = []
90
+ for j in 0...n
91
+ f = true
92
+ for k in funcs
93
+ if c[i]=~k[0] && c[j]=~k[1]
94
+ l += [name+c[i]+c[j]]
95
+ f = false
96
+ break
97
+ end
98
+ end
99
+ if f
100
+ l += ['TpErr']
101
+ end
102
+ end
103
+ m += [' { '+l.join(', ')+' }']
104
+ end
105
+ print m.join(",\n")+"\n};\n"
106
+
107
+ end
108
+
109
+
110
+
111
+ def mkfuncs(name,t1,t2,func,idx=["i"]*3)
112
+
113
+ print "
114
+ /* ------------------------- #{name} --------------------------- */\n"
115
+ c = $type_codes
116
+ td = $data_types
117
+ tr = $real_types
118
+
119
+ for i in 0...c.size
120
+ if func[i] != nil && func[i] != "copy"
121
+ if /Insp/ =~ name
122
+ f = func[i].
123
+ gsub(/p2\./,"(*((#{t2[i]}*)p2)).").
124
+ gsub(/\*p1/,"*((#{t1[i]}*)p1)").
125
+ gsub(/\*p2/,"*((#{t2[i]}*)p2)")
126
+ else
127
+ f = func[i].
128
+ gsub(/p1\./,"(*((#{t1[i]}*)(p1+#{idx[0]}*i1))).").
129
+ gsub(/p2\./,"(*((#{t2[i]}*)(p2+#{idx[1]}*i2))).").
130
+ gsub(/p3\./,"(*((#{t2[i]}*)(p3+#{idx[2]}*i3))).").
131
+ gsub(/\*p1/,"*((#{t1[i]}*)(p1+#{idx[0]}*i1))").
132
+ gsub(/\*p2/,"*((#{t2[i]}*)(p2+#{idx[1]}*i2))").
133
+ gsub(/\*p3/,"*((#{t2[i]}*)(p3+#{idx[2]}*i3))").
134
+ gsub(/\(u_int8_t\*\)p3/,"(u_int8_t*)(p3+#{idx[2]}*i3)").
135
+ gsub(/p2;/,'p2+i*i2;')
136
+ end
137
+ f = f.
138
+ gsub(/type1/,td[i]).
139
+ gsub(/typec/,t1[i]).
140
+ gsub(/typef/,tr[i])
141
+ puts $func_body.
142
+ gsub(/type1/,td[i]).
143
+ gsub(/typec/,t1[i]).
144
+ gsub(/typef/,tr[i]).
145
+ gsub(/#name/,name).
146
+ gsub(/OPERATION/,f).
147
+ gsub(/#C/,c[i])
148
+ end
149
+ end
150
+
151
+ # Function Array
152
+
153
+ print "\nna_func_t #{name}Funcs =\n{ "
154
+ m = []
155
+ for i in 0...c.size
156
+ if func[i] == nil
157
+ m += ['TpErr']
158
+ elsif func[i]=='copy'
159
+ m += ['Set'+c[$data_types.index(t1[i])]+c[i]]
160
+ else
161
+ m += [name+c[i]]
162
+ end
163
+ end
164
+ print m.join(", ")+" };\n"
165
+
166
+ end
167
+
168
+
169
+
170
+ def mksortfuncs(bsname,t1,t2,func)
171
+
172
+ print "
173
+ /* ------------------------- #{bsname} --------------------------- */\n"
174
+ c = $type_codes
175
+ tf = $real_types
176
+ name = bsname
177
+
178
+ # Function Definition
179
+ head = "static int #{name}#code(const void *p1, const void *p2)"
180
+ for i in 0...c.size
181
+ if func[i] != nil && func[i]=~/^\{/
182
+ f = func[i].
183
+ gsub(/p1->/,"((#{t1[i]}*)p1)->").
184
+ gsub(/p2->/,"((#{t2[i]}*)p2)->").
185
+ gsub(/\*\*p1/,"**(#{t1[i]}**)p1").
186
+ gsub(/\*\*p2/,"**(#{t2[i]}**)p2").
187
+ gsub(/\*p1/,"*(#{t1[i]}*)p1").
188
+ gsub(/\*p2/,"*(#{t2[i]}*)p2").
189
+ gsub(/typef/,tf[i])
190
+ puts( (head+f).gsub(/#code/,c[i]) )
191
+ end
192
+ end
193
+
194
+ # Function Array
195
+
196
+ print "\nna_sortfunc_t #{name}Funcs =\n{ "
197
+ m = []
198
+ for i in 0...c.size
199
+ if func[i] == nil
200
+ m += ['(int (*)(const void *, const void *))TpErrI']
201
+ elsif func[i]=='copy'
202
+ m += ['Set'+c[i]*2]
203
+ elsif !( func[i] =~ /^\{/ )
204
+ m += [func[i]]
205
+ else
206
+ m += [name+c[i]]
207
+ end
208
+ end
209
+ print m.join(", ")+" };\n"
210
+
211
+ end
212
+