lib/Faster/Maths.xs

filter: Statementstmt 87.3% 186 / 213 Branchbran 81.6% 49 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.0% 235 / 273 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Faster/Maths.pm ↑ Summary
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2021 -- leonerd@leonerd.org.uk
5 */
6 #define PERL_NO_GET_CONTEXT
7  
8 #include "EXTERN.h"
9 #include "perl.h"
10 #include "XSUB.h"
11  
12 #define HAVE_PERL_VERSION(R, V, S) \
13 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
14  
15 #if HAVE_PERL_VERSION(5,28,0)
16 # define GET_UNOP_AUX_item_pv(aux) ((aux).pv)
17 # define SET_UNOP_AUX_item_pv(aux,v) ((aux).pv = (v))
18 #else
19 # define GET_UNOP_AUX_item_pv(aux) INT2PTR(char *, (aux).uv)
20 # define SET_UNOP_AUX_item_pv(aux,v) ((aux).uv = PTR2UV(v))
21 #endif
22  
23 static XOP xop_multimath;
24 3174007 static OP *pp_multimath(pTHX)
25 {
26 3174007 dSP;
27 3174007 dTARGET;
28 3174007 UNOP_AUX_item *aux = cUNOP_AUX->op_aux;
29  
30 3174007 char *prog = GET_UNOP_AUX_item_pv(aux[0]) + 1; /* skip leading '(' */
31 3174007 U32 ntmps = PL_op->op_private;
32  
33 3174007 U32 tmpi = 1;
34 NV accum;
35  
36 3174007 U32 auxi = 1 + ntmps;
37  
38 3174007 switch(*(prog++)) {
39 2118006 case 'p':
40 2118006 accum = SvNV_nomg(PAD_SV(aux[auxi].pad_offset));
41 2118006 auxi++;
42 2118006 break;
43  
44 1056001 case 'c':
45 1056001 accum = SvNV_nomg(aux[auxi].sv);
46 1056001 auxi++;
47 1056001 break;
48  
49 0 default:
50 0 croak("ARGH: initial prog instruction");
51 }
52  
53 16926027 ▶ while(*prog) {
Branch: 1 branch
BranchTrueFalseTotal
while(*prog) { 13752020 3174007 16926027
54 13752020 char op = *(prog++);
55 13752020 switch(op) {
56 10578017 case '+':
57 case '-':
58 case '*':
59 case '/':
60 {
61 NV rhs;
62 10578017 switch(*(prog++)) {
63 7404014 case 'p':
64 7404014 rhs = SvNV_nomg(PAD_SV(aux[auxi].pad_offset));
65 7404014 auxi++;
66 7404014 break;
67 0 case 'c':
68 0 rhs = SvNV_nomg(aux[auxi].sv);
69 0 auxi++;
70 0 break;
71 3174003 case ')':
72 3174003 rhs = accum;
73 3174003 tmpi--;
74 3174003 accum = SvNV_nomg(PAD_SV(aux[tmpi].pad_offset));
75 3174003 break;
76 0 default:
77 0 croak("ARGH MULTIMATH arg %c\n", prog[-1]);
78 }
79  
80 10578017 switch(op) {
81 3174006 case '+':
82 3174006 accum += rhs;
83 3174006 break;
84 1056003 case '-':
85 1056003 accum -= rhs;
86 1056003 break;
87 6348008 case '*':
88 6348008 accum *= rhs;
89 6348008 break;
90 0 case '/':
91 0 accum /= rhs;
92 0 break;
93 }
94 10578017 break;
95 }
96  
97 3174003 case '(':
98 {
99 NV val;
100 3174003 switch(*(prog++)) {
101 3174003 case 'p':
102 3174003 val = SvNV_nomg(PAD_SV(aux[auxi].pad_offset));
103 3174003 auxi++;
104 3174003 break;
105 0 case 'c':
106 0 val = SvNV_nomg(aux[auxi].sv);
107 0 auxi++;
108 0 break;
109 }
110 3174003 sv_setnv(PAD_SV(aux[tmpi].pad_offset), accum);
111 3174003 tmpi++;
112 3174003 accum = val;
113 3174003 break;
114 }
115  
116 0 default:
117 0 croak("TODO: MULTIMATH %c\n", op);
118 }
119 }
120  
121 3174007 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 3174007 3174007
122 3174007 ▶ PUSHn(accum);
Branch: 1 branch
BranchTrueFalseTotal
PUSHn(accum); 2111998 1062009 3174007
123 3174007 RETURN;
124 }
125  
126 #define optimize_maths(start, final) MY_optimize_maths(aTHX_ start, final)
127 10 static OP *MY_optimize_maths(pTHX_ OP *start, OP *final)
128 {
129 OP *o;
130  
131 /* Phase 1: just count the number of aux items we need
132 * We'll need one for every constant or padix
133 * Also count the maximum stack height
134 */
135 10 U32 nitems = 1; /* aux[0] is the program */
136 10 U32 height = 0, maxheight = 0;
137  
138 64 ▶ for(o = start; o; o = o->op_next) {
Branch: 1 branch
BranchTrueFalseTotal
for(o = start; o; o = o->op_next) { 64 0 64
139 64 switch(o->op_type) {
140 37 case OP_CONST:
141 case OP_PADSV:
142 37 nitems++;
143 37 height++;
144 37 break;
145  
146 27 case OP_ADD:
147 case OP_SUBTRACT:
148 case OP_MULTIPLY:
149 case OP_DIVIDE:
150 27 height--;
151 27 break;
152 }
153  
154 64 ▶ if(height > maxheight)
Branch: 1 branch
BranchTrueFalseTotal
if(height > maxheight) 26 38 64
155 26 maxheight = height;
156  
157 64 ▶ if(o == final)
Branch: 1 branch
BranchTrueFalseTotal
if(o == final) 10 54 64
158 10 break;
159 }
160  
161 10 U32 ntmps = maxheight - 1;
162  
163 UNOP_AUX_item *aux;
164 10 Newx(aux, nitems + ntmps, UNOP_AUX_item);
165  
166 10 SV *prog = newSV(0);
167 10 sv_setpvs(prog, "");
168  
169 10 U32 tmpi = 1;
170 10 U32 auxi = 1 + ntmps;
171  
172 /* Phase 2: collect up the constants and padices, build the program string */
173 10 char lastarg = ')';
174 char operator;
175 64 ▶ for(o = start; o; o = o->op_next) {
Branch: 1 branch
BranchTrueFalseTotal
for(o = start; o; o = o->op_next) { 64 0 64
176 64 switch(o->op_type) {
177 2 case OP_CONST:
178 2 ▶ if(lastarg != ')')
Branch: 1 branch
BranchTrueFalseTotal
if(lastarg != ')') 0 2 2
179 0 sv_catpvf(prog, "(%c", lastarg);
180 2 lastarg = 'c';
181 2 aux[auxi++].sv = SvREFCNT_inc(cSVOPo->op_sv);
182 2 break;
183  
184 35 case OP_PADSV:
185 35 ▶ if(lastarg != ')')
Branch: 1 branch
BranchTrueFalseTotal
if(lastarg != ')') 16 19 35
186 16 sv_catpvf(prog, "(%c", lastarg);
187 35 lastarg = 'p';
188 35 aux[auxi++].pad_offset = o->op_targ;
189 35 break;
190  
191 9 case OP_ADD:
192 9 operator = '+';
193 9 goto do_BINOP;
194 4 case OP_SUBTRACT:
195 4 operator = '-';
196 4 goto do_BINOP;
197 14 case OP_MULTIPLY:
198 14 operator = '*';
199 14 goto do_BINOP;
200 0 case OP_DIVIDE:
201 0 operator = '/';
202 0 goto do_BINOP;
203  
204 27 do_BINOP:
205 27 sv_catpvf(prog, "%c%c", operator, lastarg);
206 27 lastarg = ')';
207  
208 /* Steal a padtmp because that won't be using it */
209 27 ▶ if(tmpi <= ntmps)
Branch: 1 branch
BranchTrueFalseTotal
if(tmpi <= ntmps) 16 11 27
210 16 aux[tmpi++].pad_offset = o->op_targ;
211 27 break;
212  
213  
214 0 default:
215 0 croak("ARGH unsure how to optimize this op\n");
216 }
217  
218 64 ▶ if(o == final)
Branch: 1 branch
BranchTrueFalseTotal
if(o == final) 10 54 64
219 10 break;
220 }
221  
222 10 ▶ if(SvPVX(prog)[0] != '(')
Branch: 1 branch
BranchTrueFalseTotal
if(SvPVX(prog)[0] != '(') 0 10 10
223 0 croak("ARGH: expected prog to begin (");
224  
225 /* Steal the buffer */
226 10 SET_UNOP_AUX_item_pv(aux[0], SvPVX(prog)); SvLEN(prog) = 0;
227 10 SvREFCNT_dec(prog);
228  
229 10 OP *retop = newUNOP_AUX(OP_CUSTOM, 0, NULL, aux);
230 10 retop->op_ppaddr = &pp_multimath;
231 10 retop->op_private = ntmps;
232 10 retop->op_targ = final->op_targ;
233  
234 10 return retop;
235 }
236  
237 static void rpeep_for_fastermaths(pTHX_ OP *o, bool init_enabled);
238 3853 static void rpeep_for_fastermaths(pTHX_ OP *o, bool init_enabled)
239 {
240 3853 bool enabled = init_enabled;
241  
242 3853 OP *prevo = NULL;
243  
244 /* In some cases (e.g. while(1) { ... } ) the ->op_next chain actually
245 * forms a closed loop. In order to detect this loop and break out we'll
246 * advance the `slowo` pointer half as fast as o. If o is ever equal to
247 * slowo then we have reached a cycle and should stop
248 */
249 3853 OP *slowo = NULL;
250 3853 int slowotick = 0;
251  
252 114319 ▶ while(o && o != slowo) {
Branch: 2 branches
BranchTrueFalseTotal
while(o && o != slowo) { 110476 3843 114319
while(o && o != slowo) { 110466 10 110476
253 110466 ▶ if(o->op_type == OP_NEXTSTATE) {
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_type == OP_NEXTSTATE) { 13794 96672 110466
254 13794 SV *sv = cop_hints_fetch_pvs(cCOPo, "Faster::Maths/faster", 0);
255 13794 ▶ enabled = sv && sv != &PL_sv_placeholder && SvTRUE(sv);
Branch: 3 branches
BranchTrueFalseTotal
enabled = sv && sv != &PL_sv_placeholder && SvTRUE(sv); 13794 0 13794
enabled = sv && sv != &PL_sv_placeholder && SvTRUE(sv); 33 13761 13794
enabled = sv && sv != &PL_sv_placeholder && SvTRUE(sv); 33 0 33
256 13794 goto next_o;
257 }
258 96672 ▶ if(!enabled)
Branch: 1 branch
BranchTrueFalseTotal
if(!enabled) 96531 141 96672
259 96531 goto next_o;
260  
261 /* There are no BASEOP mathsy ops, so any optimizable sequence necessarily
262 * starts with an argument
263 */
264 141 switch(o->op_type) {
265 51 case OP_CONST:
266 case OP_PADSV:
267 51 break;
268  
269 7 case OP_OR:
270 case OP_AND:
271 case OP_DOR:
272 #if HAVE_PERL_VERSION(5,32,0)
273 case OP_CMPCHAIN_AND:
274 #endif
275 case OP_COND_EXPR:
276 case OP_MAPWHILE:
277 case OP_ANDASSIGN:
278 case OP_ORASSIGN:
279 case OP_DORASSIGN:
280 case OP_RANGE:
281 case OP_ONCE:
282 #if HAVE_PERL_VERSION(5,26,0)
283 case OP_ARGDEFELEM:
284 #endif
285 /* Optimize on the righthand side of `or` / `and` operators and other.
286 * similar cases. This might catch more things than perl's own
287 * recursion inside because simple expressions don't begin with an
288 * OP_NEXTSTATE
289 */
290 7 ▶ if(cLOGOPo->op_other && cLOGOPo->op_other->op_type != OP_NEXTSTATE)
Branch: 2 branches
BranchTrueFalseTotal
if(cLOGOPo->op_other && cLOGOPo->op_other->op_type != OP_NEXTSTATE) 7 0 7
if(cLOGOPo->op_other && cLOGOPo->op_other->op_type != OP_NEXTSTATE) 4 3 7
291 4 rpeep_for_fastermaths(aTHX_ cLOGOPo->op_other, enabled);
292 7 goto next_o;
293  
294 83 default:
295 83 goto next_o;
296 }
297  
298 /* Find a sequence of mathsy args/ops we can optimize */
299 51 OP *final = NULL; /* the final op in the optimizable chain */
300 51 U32 final_opcount = 0; /* total number of operations we found until final */
301 {
302 51 U32 opcount = 0;
303 51 U32 height = 0; /* running height of the stack */
304  
305 OP *scout;
306 189 ▶ for(scout = o; scout; scout = scout->op_next) {
Branch: 1 branch
BranchTrueFalseTotal
for(scout = o; scout; scout = scout->op_next) { 189 0 189
307 189 switch(scout->op_type) {
308 /* OPs that push 1 argument */
309 105 case OP_CONST:
310 case OP_PADSV:
311 105 height++;
312 105 break;
313  
314 /* BINOPs that consume 2, push 1 */
315 33 case OP_ADD:
316 case OP_SUBTRACT:
317 case OP_MULTIPLY:
318 case OP_DIVIDE:
319 33 ▶ if(height < 2)
Branch: 1 branch
BranchTrueFalseTotal
if(height < 2) 0 33 33
320 /* We never had enough arguments, meaning any of the initial
321 * ones for this op must have been of a kind we don't recognise
322 * Abort and go to the next outer loop; we'll pick up an
323 * optimizable inner sub-chain again later
324 */
325 0 goto next_o;
326  
327 33 opcount++;
328 33 height--;
329 33 ▶ if(height == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(height == 1) { 21 12 33
330 21 final = scout;
331 21 final_opcount = opcount;
332 }
333 33 break;
334  
335 51 default:
336 51 ▶ if(!final)
Branch: 1 branch
BranchTrueFalseTotal
if(!final) 41 10 51
337 /* We never finished on an op that would give us a height of 1,
338 * which means we probably took too many initial argument ops.
339 * Abort now and go to the next outer loop; we'll pick up an
340 * optimizable inner sub-chain again later
341 */
342 41 goto next_o;
343  
344 10 goto done_scout;
345 }
346 }
347 0 done_scout:
348 ;
349 }
350  
351 /* If we found fewer than 2 operations there's no point optimizing them */
352 10 ▶ if(final_opcount < 2)
Branch: 1 branch
BranchTrueFalseTotal
if(final_opcount < 2) 0 10 10
353 0 goto next_o;
354  
355 /* At this point we now know that the sequence o to final consists of
356 * optimizable ops yielding a single final scalar answer.
357 */
358 10 OP *newo = optimize_maths(o, final);
359  
360 10 newo->op_next = final->op_next;
361 10 ▶ if(prevo)
Branch: 1 branch
BranchTrueFalseTotal
if(prevo) 9 1 10
362 9 prevo->op_next = o = newo;
363 else {
364 /* we optimized starting at the very first op in this chain */
365 1 o->op_targ = o->op_type;
366 1 o->op_type = OP_NULL;
367 1 o->op_next = newo;
368  
369 1 o = newo;
370 }
371  
372 110466 next_o:
373 110466 ▶ if(!slowo)
Branch: 1 branch
BranchTrueFalseTotal
if(!slowo) 3853 106613 110466
374 3853 slowo = o;
375 106613 ▶ else if((slowotick++) % 2)
Branch: 1 branch
BranchTrueFalseTotal
else if((slowotick++) % 2) 52314 54299 106613
376 52314 slowo = slowo->op_next;
377  
378 110466 prevo = o;
379 110466 o = o->op_next;
380 }
381 3853 }
382  
383 static void (*next_rpeepp)(pTHX_ OP *o);
384  
385 static void
386 3903 my_rpeepp(pTHX_ OP *o)
387 {
388 3903 ▶ if(!o)
Branch: 1 branch
BranchTrueFalseTotal
if(!o) 54 3849 3903
389 54 return;
390  
391 3849 (*next_rpeepp)(aTHX_ o);
392  
393 3849 rpeep_for_fastermaths(aTHX_ o, FALSE);
394 }
395  
396 MODULE = Faster::Maths PACKAGE = Faster::Maths
397  
398 BOOT:
399 /* TODO: find the correct wrapper function for this */
400 3 next_rpeepp = PL_rpeepp;
401 3 PL_rpeepp = &my_rpeepp;
402  
403 3 XopENTRY_set(&xop_multimath, xop_name, "multimath");
404 3 XopENTRY_set(&xop_multimath, xop_desc,
405 "combined maths operations");
406 3 XopENTRY_set(&xop_multimath, xop_class, OA_UNOP_AUX);
407 3 Perl_custom_op_register(aTHX_ &pp_multimath, &xop_multimath);
« blib/lib/Faster/Maths.pm ↑ Summary