3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * ...they shuffled back towards the rear of the line. 'No, not at the
13 * rear!' the slave-driver shouted. 'Three files up. And stay there...
15 * [p.931 of _The Lord of the Rings_, VI/ii: "The Land of Shadow"]
18 /* This file contains pp ("push/pop") functions that
19 * execute the opcodes that make up a perl program. A typical pp function
20 * expects to find its arguments on the stack, and usually pushes its
21 * results onto the stack, hence the 'pp' terminology. Each OP structure
22 * contains a pointer to the relevant pp_foo() function.
24 * This particular file just contains pp_sort(), which is complex
25 * enough to merit its own file! See the other pp*.c files for the rest of
30 #define PERL_IN_PP_SORT_C
34 #define SMALLSORT (200)
38 * The mergesort implementation is by Peter M. Mcilroy <pmcilroy@lucent.com>.
40 * The original code was written in conjunction with BSD Computer Software
41 * Research Group at University of California, Berkeley.
43 * See also: "Optimistic Sorting and Information Theoretic Complexity"
45 * SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms),
46 * pp 467-474, Austin, Texas, 25-27 January 1993.
48 * The integration to Perl is by John P. Linderman <jpl.jpl@gmail.com>.
50 * The code can be distributed under the same terms as Perl itself.
55 typedef char * aptr; /* pointer for arithmetic on sizes */
56 typedef SV * gptr; /* pointers in our lists */
58 /* Binary merge internal sort, with a few special mods
59 ** for the special perl environment it now finds itself in.
61 ** Things that were once options have been hotwired
62 ** to values suitable for this use. In particular, we'll always
63 ** initialize looking for natural runs, we'll always produce stable
64 ** output, and we'll always do Peter McIlroy's binary merge.
67 /* Pointer types for arithmetic and storage and convenience casts */
69 #define APTR(P) ((aptr)(P))
70 #define GPTP(P) ((gptr *)(P))
71 #define GPPP(P) ((gptr **)(P))
74 /* byte offset from pointer P to (larger) pointer Q */
75 #define BYTEOFF(P, Q) (APTR(Q) - APTR(P))
77 #define PSIZE sizeof(gptr)
79 /* If PSIZE is power of 2, make PSHIFT that power, if that helps */
82 #define PNELEM(P, Q) (BYTEOFF(P,Q) >> (PSHIFT))
83 #define PNBYTE(N) ((N) << (PSHIFT))
84 #define PINDEX(P, N) (GPTP(APTR(P) + PNBYTE(N)))
86 /* Leave optimization to compiler */
87 #define PNELEM(P, Q) (GPTP(Q) - GPTP(P))
88 #define PNBYTE(N) ((N) * (PSIZE))
89 #define PINDEX(P, N) (GPTP(P) + (N))
92 /* Pointer into other corresponding to pointer into this */
93 #define POTHER(P, THIS, OTHER) GPTP(APTR(OTHER) + BYTEOFF(THIS,P))
95 #define FROMTOUPTO(src, dst, lim) do *dst++ = *src++; while(src<lim)
98 /* Runs are identified by a pointer in the auxiliary list.
99 ** The pointer is at the start of the list,
100 ** and it points to the start of the next list.
101 ** NEXT is used as an lvalue, too.
104 #define NEXT(P) (*GPPP(P))
107 /* PTHRESH is the minimum number of pairs with the same sense to justify
108 ** checking for a run and extending it. Note that PTHRESH counts PAIRS,
109 ** not just elements, so PTHRESH == 8 means a run of 16.
114 /* RTHRESH is the number of elements in a run that must compare low
115 ** to the low element from the opposing run before we justify
116 ** doing a binary rampup instead of single stepping.
117 ** In random input, N in a row low should only happen with
118 ** probability 2^(1-N), so we can risk that we are dealing
119 ** with orderly input without paying much when we aren't.
126 ** Overview of algorithm and variables.
127 ** The array of elements at list1 will be organized into runs of length 2,
128 ** or runs of length >= 2 * PTHRESH. We only try to form long runs when
129 ** PTHRESH adjacent pairs compare in the same way, suggesting overall order.
131 ** Unless otherwise specified, pair pointers address the first of two elements.
133 ** b and b+1 are a pair that compare with sense "sense".
134 ** b is the "bottom" of adjacent pairs that might form a longer run.
136 ** p2 parallels b in the list2 array, where runs are defined by
139 ** t represents the "top" of the adjacent pairs that might extend
140 ** the run beginning at b. Usually, t addresses a pair
141 ** that compares with opposite sense from (b,b+1).
142 ** However, it may also address a singleton element at the end of list1,
143 ** or it may be equal to "last", the first element beyond list1.
145 ** r addresses the Nth pair following b. If this would be beyond t,
146 ** we back it off to t. Only when r is less than t do we consider the
147 ** run long enough to consider checking.
149 ** q addresses a pair such that the pairs at b through q already form a run.
150 ** Often, q will equal b, indicating we only are sure of the pair itself.
151 ** However, a search on the previous cycle may have revealed a longer run,
152 ** so q may be greater than b.
154 ** p is used to work back from a candidate r, trying to reach q,
155 ** which would mean b through r would be a run. If we discover such a run,
156 ** we start q at r and try to push it further towards t.
157 ** If b through r is NOT a run, we detect the wrong order at (p-1,p).
158 ** In any event, after the check (if any), we have two main cases.
160 ** 1) Short run. b <= q < p <= r <= t.
161 ** b through q is a run (perhaps trivial)
162 ** q through p are uninteresting pairs
163 ** p through r is a run
165 ** 2) Long run. b < r <= q < t.
166 ** b through q is a run (of length >= 2 * PTHRESH)
168 ** Note that degenerate cases are not only possible, but likely.
169 ** For example, if the pair following b compares with opposite sense,
170 ** then b == q < p == r == t.
174 PERL_STATIC_FORCE_INLINE IV __attribute__always_inline__
175 dynprep(pTHX_ gptr *list1, gptr *list2, size_t nmemb, const SVCOMPARE_t cmp)
178 gptr *b, *p, *q, *t, *p2;
183 last = PINDEX(b, nmemb);
184 sense = (cmp(aTHX_ *b, *(b+1)) > 0);
185 for (p2 = list2; b < last; ) {
186 /* We just started, or just reversed sense.
187 ** Set t at end of pairs with the prevailing sense.
189 for (p = b+2, t = p; ++p < last; t = ++p) {
190 if ((cmp(aTHX_ *t, *p) > 0) != sense) break;
193 /* Having laid out the playing field, look for long runs */
195 p = r = b + (2 * PTHRESH);
196 if (r >= t) p = r = t; /* too short to care about */
198 while (((cmp(aTHX_ *(p-1), *p) > 0) == sense) &&
201 /* b through r is a (long) run.
202 ** Extend it as far as possible.
205 while (((p += 2) < t) &&
206 ((cmp(aTHX_ *(p-1), *p) > 0) == sense)) q = p;
207 r = p = q + 2; /* no simple pairs, no after-run */
210 if (q > b) { /* run of greater than 2 at b */
214 /* pick up singleton, if possible */
217 ((cmp(aTHX_ *(p-1), *p) > 0) == sense))
218 savep = r = p = q = last;
219 p2 = NEXT(p2) = p2 + (p - b); ++runs;
228 while (q < p) { /* simple pairs */
229 p2 = NEXT(p2) = p2 + 2; ++runs;
236 if (((b = p) == t) && ((t+1) == last)) {
237 NEXT(p2) = p2 + 1; ++runs;
248 /* The original merge sort, in use since 5.7, was as fast as, or faster than,
249 * qsort on many platforms, but slower than qsort, conspicuously so,
250 * on others. The most likely explanation was platform-specific
251 * differences in cache sizes and relative speeds.
253 * The quicksort divide-and-conquer algorithm guarantees that, as the
254 * problem is subdivided into smaller and smaller parts, the parts
255 * fit into smaller (and faster) caches. So it doesn't matter how
256 * many levels of cache exist, quicksort will "find" them, and,
257 * as long as smaller is faster, take advantage of them.
259 * By contrast, consider how the original mergesort algorithm worked.
260 * Suppose we have five runs (each typically of length 2 after dynprep).
269 * Adjacent pairs are merged in "grand sweeps" through the input.
270 * This means, on pass 1, the records in runs 1 and 2 aren't revisited until
271 * runs 3 and 4 are merged and the runs from run 5 have been copied.
272 * The only cache that matters is one large enough to hold *all* the input.
273 * On some platforms, this may be many times slower than smaller caches.
275 * The following pseudo-code uses the same basic merge algorithm,
276 * but in a divide-and-conquer way.
278 * # merge $runs runs at offset $offset of list $list1 into $list2.
279 * # all unmerged runs ($runs == 1) originate in list $base.
281 * my ($offset, $runs, $base, $list1, $list2) = @_;
284 * if ($list1 is $base) copy run to $list2
285 * return offset of end of list (or copy)
287 * $off2 = mgsort2($offset, $runs-($runs/2), $base, $list2, $list1)
288 * mgsort2($off2, $runs/2, $base, $list2, $list1)
289 * merge the adjacent runs at $offset of $list1 into $list2
290 * return the offset of the end of the merged runs
293 * mgsort2(0, $runs, $base, $aux, $base);
295 * For our 5 runs, the tree of calls looks like
304 * and the corresponding activity looks like
306 * copy runs 1 and 2 from base to aux
307 * merge runs 1 and 2 from aux to base
308 * (run 3 is where it belongs, no copy needed)
309 * merge runs 12 and 3 from base to aux
310 * (runs 4 and 5 are where they belong, no copy needed)
311 * merge runs 4 and 5 from base to aux
312 * merge runs 123 and 45 from aux to base
314 * Note that we merge runs 1 and 2 immediately after copying them,
315 * while they are still likely to be in fast cache. Similarly,
316 * run 3 is merged with run 12 while it still may be lingering in cache.
317 * This implementation should therefore enjoy much of the cache-friendly
318 * behavior that quicksort does. In addition, it does less copying
319 * than the original mergesort implementation (only runs 1 and 2 are copied)
320 * and the "balancing" of merges is better (merged runs comprise more nearly
321 * equal numbers of original runs).
323 * The actual cache-friendly implementation will use a pseudo-stack
324 * to avoid recursion, and will unroll processing of runs of length 2,
325 * but it is otherwise similar to the recursive implementation.
329 IV offset; /* offset of 1st of 2 runs at this level */
330 IV runs; /* how many runs must be combined into 1 */
331 } off_runs; /* pseudo-stack element */
333 PERL_STATIC_FORCE_INLINE void
334 S_sortsv_flags_impl(pTHX_ gptr *base, size_t nmemb, SVCOMPARE_t cmp, U32 flags)
338 gptr *f1, *f2, *t, *b, *p;
342 gptr small[SMALLSORT];
344 off_runs stack[60], *stackp;
346 PERL_UNUSED_ARG(flags);
347 PERL_ARGS_ASSERT_SORTSV_FLAGS_IMPL;
348 if (nmemb <= 1) return; /* sorted trivially */
350 if (nmemb <= SMALLSORT) aux = small; /* use stack for aux array */
351 else { Newx(aux,nmemb,gptr); } /* allocate auxiliary array */
354 stackp->runs = dynprep(aTHX_ base, aux, nmemb, cmp);
355 stackp->offset = offset = 0;
356 which[0] = which[2] = base;
359 /* On levels where both runs have be constructed (stackp->runs == 0),
360 * merge them, and note the offset of their end, in case the offset
361 * is needed at the next level up. Hop up a level, and,
362 * as long as stackp->runs is 0, keep merging.
364 IV runs = stackp->runs;
368 list1 = which[iwhich]; /* area where runs are now */
369 list2 = which[++iwhich]; /* area for merged runs */
372 offset = stackp->offset;
373 f1 = p1 = list1 + offset; /* start of first run */
374 p = tp2 = list2 + offset; /* where merged run will go */
375 t = NEXT(p); /* where first run ends */
376 f2 = l1 = POTHER(t, list2, list1); /* ... on the other side */
377 t = NEXT(t); /* where second runs ends */
378 l2 = POTHER(t, list2, list1); /* ... on the other side */
379 offset = PNELEM(list2, t);
380 while (f1 < l1 && f2 < l2) {
381 /* If head 1 is larger than head 2, find ALL the elements
382 ** in list 2 strictly less than head1, write them all,
383 ** then head 1. Then compare the new heads, and repeat,
384 ** until one or both lists are exhausted.
386 ** In all comparisons (after establishing
387 ** which head to merge) the item to merge
388 ** (at pointer q) is the first operand of
389 ** the comparison. When we want to know
390 ** if "q is strictly less than the other",
393 ** because stability demands that we treat equality
394 ** as high when q comes from l2, and as low when
395 ** q was from l1. So we ask the question by doing
396 ** cmp(q, other) <= sense
397 ** and make sense == 0 when equality should look low,
398 ** and -1 when equality should look high.
402 if (cmp(aTHX_ *f1, *f2) <= 0) {
403 q = f2; b = f1; t = l1;
406 q = f1; b = f2; t = l2;
413 ** Leave t at something strictly
414 ** greater than q (or at the end of the list),
415 ** and b at something strictly less than q.
417 for (i = 1, run = 0 ;;) {
418 if ((p = PINDEX(b, i)) >= t) {
420 if (((p = PINDEX(t, -1)) > b) &&
421 (cmp(aTHX_ *q, *p) <= sense))
425 } else if (cmp(aTHX_ *q, *p) <= sense) {
429 if (++run >= RTHRESH) i += i;
433 /* q is known to follow b and must be inserted before t.
434 ** Increment b, so the range of possibilities is [b,t).
435 ** Round binary split down, to favor early appearance.
436 ** Adjust b and t until q belongs just before t.
441 p = PINDEX(b, (PNELEM(b, t) - 1) / 2);
442 if (cmp(aTHX_ *q, *p) <= sense) {
448 /* Copy all the strictly low elements */
451 FROMTOUPTO(f2, tp2, t);
454 FROMTOUPTO(f1, tp2, t);
460 /* Run out remaining list */
462 if (f2 < l2) FROMTOUPTO(f2, tp2, l2);
463 } else FROMTOUPTO(f1, tp2, l1);
464 p1 = NEXT(p1) = POTHER(tp2, list2, list1);
466 if (--level == 0) goto done;
468 t = list1; list1 = list2; list2 = t; /* swap lists */
469 } while ((runs = stackp->runs) == 0);
473 stackp->runs = 0; /* current run will finish level */
474 /* While there are more than 2 runs remaining,
475 * turn them into exactly 2 runs (at the "other" level),
476 * each made up of approximately half the runs.
477 * Stack the second half for later processing,
478 * and set about producing the first half now.
483 stackp->offset = offset;
484 runs -= stackp->runs = runs / 2;
486 /* We must construct a single run from 1 or 2 runs.
487 * All the original runs are in which[0] == base.
488 * The run we construct must end up in which[level&1].
492 /* Constructing a single run from a single run.
493 * If it's where it belongs already, there's nothing to do.
494 * Otherwise, copy it to where it belongs.
495 * A run of 1 is either a singleton at level 0,
496 * or the second half of a split 3. In neither event
497 * is it necessary to set offset. It will be set by the merge
498 * that immediately follows.
500 if (iwhich) { /* Belongs in aux, currently in base */
501 f1 = b = PINDEX(base, offset); /* where list starts */
502 f2 = PINDEX(aux, offset); /* where list goes */
503 t = NEXT(f2); /* where list will end */
504 offset = PNELEM(aux, t); /* offset thereof */
505 t = PINDEX(base, offset); /* where it currently ends */
506 FROMTOUPTO(f1, f2, t); /* copy */
507 NEXT(b) = t; /* set up parallel pointer */
508 } else if (level == 0) goto done; /* single run at level 0 */
510 /* Constructing a single run from two runs.
511 * The merge code at the top will do that.
512 * We need only make sure the two runs are in the "other" array,
513 * so they'll end up in the correct array after the merge.
517 stackp->offset = offset;
518 stackp->runs = 0; /* take care of both runs, trigger merge */
519 if (!iwhich) { /* Merged runs belong in aux, copy 1st */
520 f1 = b = PINDEX(base, offset); /* where first run starts */
521 f2 = PINDEX(aux, offset); /* where it will be copied */
522 t = NEXT(f2); /* where first run will end */
523 offset = PNELEM(aux, t); /* offset thereof */
524 p = PINDEX(base, offset); /* end of first run */
525 t = NEXT(t); /* where second run will end */
526 t = PINDEX(base, PNELEM(aux, t)); /* where it now ends */
527 FROMTOUPTO(f1, f2, t); /* copy both runs */
528 NEXT(b) = p; /* paralleled pointer for 1st */
529 NEXT(p) = t; /* ... and for second */
534 if (aux != small) Safefree(aux); /* free iff allocated */
540 =for apidoc sortsv_flags
542 In-place sort an array of SV pointers with the given comparison routine,
543 with various SORTf_* flag options.
548 Perl_sortsv_flags(pTHX_ gptr *base, size_t nmemb, SVCOMPARE_t cmp, U32 flags)
550 PERL_ARGS_ASSERT_SORTSV_FLAGS;
552 sortsv_flags_impl(base, nmemb, cmp, flags);
556 * Each of sortsv_* functions contains an inlined copy of
557 * sortsv_flags_impl() with an inlined comparator. Basically, we are
558 * emulating C++ templates by using __attribute__((always_inline)).
560 * The purpose of that is to avoid the function call overhead inside
561 * the sorting routine, which calls the comparison function multiple
562 * times per sorted item.
566 sortsv_amagic_i_ncmp(pTHX_ gptr *base, size_t nmemb, U32 flags)
568 sortsv_flags_impl(base, nmemb, S_amagic_i_ncmp, flags);
572 sortsv_amagic_i_ncmp_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
574 sortsv_flags_impl(base, nmemb, S_amagic_i_ncmp_desc, flags);
578 sortsv_i_ncmp(pTHX_ gptr *base, size_t nmemb, U32 flags)
580 sortsv_flags_impl(base, nmemb, S_sv_i_ncmp, flags);
584 sortsv_i_ncmp_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
586 sortsv_flags_impl(base, nmemb, S_sv_i_ncmp_desc, flags);
590 sortsv_amagic_ncmp(pTHX_ gptr *base, size_t nmemb, U32 flags)
592 sortsv_flags_impl(base, nmemb, S_amagic_ncmp, flags);
596 sortsv_amagic_ncmp_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
598 sortsv_flags_impl(base, nmemb, S_amagic_ncmp_desc, flags);
602 sortsv_ncmp(pTHX_ gptr *base, size_t nmemb, U32 flags)
604 sortsv_flags_impl(base, nmemb, S_sv_ncmp, flags);
608 sortsv_ncmp_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
610 sortsv_flags_impl(base, nmemb, S_sv_ncmp_desc, flags);
614 sortsv_amagic_cmp(pTHX_ gptr *base, size_t nmemb, U32 flags)
616 sortsv_flags_impl(base, nmemb, S_amagic_cmp, flags);
620 sortsv_amagic_cmp_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
622 sortsv_flags_impl(base, nmemb, S_amagic_cmp_desc, flags);
626 sortsv_cmp(pTHX_ gptr *base, size_t nmemb, U32 flags)
628 sortsv_flags_impl(base, nmemb, Perl_sv_cmp, flags);
632 sortsv_cmp_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
634 sortsv_flags_impl(base, nmemb, S_cmp_desc, flags);
637 #ifdef USE_LOCALE_COLLATE
640 sortsv_amagic_cmp_locale(pTHX_ gptr *base, size_t nmemb, U32 flags)
642 sortsv_flags_impl(base, nmemb, S_amagic_cmp_locale, flags);
646 sortsv_amagic_cmp_locale_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
648 sortsv_flags_impl(base, nmemb, S_amagic_cmp_locale_desc, flags);
652 sortsv_cmp_locale(pTHX_ gptr *base, size_t nmemb, U32 flags)
654 sortsv_flags_impl(base, nmemb, Perl_sv_cmp_locale, flags);
658 sortsv_cmp_locale_desc(pTHX_ gptr *base, size_t nmemb, U32 flags)
660 sortsv_flags_impl(base, nmemb, S_cmp_locale_desc, flags);
669 In-place sort an array of SV pointers with the given comparison routine.
671 Currently this always uses mergesort. See C<L</sortsv_flags>> for a more
678 Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp)
680 PERL_ARGS_ASSERT_SORTSV;
682 sortsv_flags(array, nmemb, cmp, 0);
685 #define SvNSIOK(sv) ((SvFLAGS(sv) & SVf_NOK) || ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) == SVf_IOK))
686 #define SvSIOK(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) == SVf_IOK)
687 #define SvNSIV(sv) ( SvNOK(sv) ? SvNVX(sv) : ( SvSIOK(sv) ? SvIVX(sv) : sv_2nv(sv) ) )
691 dSP; dMARK; dORIGMARK;
692 SV **p1 = ORIGMARK+1, **p2;
698 OP* const nextop = PL_op->op_next;
700 bool hasargs = FALSE;
703 const U8 priv = PL_op->op_private;
704 const U8 flags = PL_op->op_flags;
706 I32 all_SIVs = 1, descending = 0;
708 if ((priv & OPpSORT_DESCEND) != 0)
711 if (gimme != G_LIST) {
718 SAVEVPTR(PL_sortcop);
720 /* Important flag meanings:
722 * OPf_STACKED sort <function_name> args
725 * |OPf_SPECIAL) sort { <block> } args
727 * ---- standard block; e.g. sort { $a <=> $b } args
730 * OPpSORT_NUMERIC { $a <=> $b } (as opposed to $a cmp $b)
731 * OPpSORT_INTEGER ditto in scope of 'use integer'
732 * OPpSORT_DESCEND { $b <=> $a }
733 * OPpSORT_REVERSE @a= reverse sort ....;
734 * OPpSORT_INPLACE @a = sort @a;
737 if (flags & OPf_STACKED) {
738 if (flags & OPf_SPECIAL) {
739 OP *nullop = OpSIBLING(cLISTOP->op_first); /* pass pushmark */
740 assert(nullop->op_type == OP_NULL);
741 PL_sortcop = nullop->op_next;
746 cv = sv_2cv(*++MARK, &stash, &gv, GV_ADD);
748 if (cv && SvPOK(cv)) {
749 const char * const proto = SvPV_nolen_const(MUTABLE_SV(cv));
750 if (proto && strEQ(proto, "$$")) {
754 if (cv && CvISXSUB(cv) && CvXSUB(cv)) {
757 else if (!(cv && CvROOT(cv))) {
761 else if (!CvANON(cv) && (gv = CvGV(cv))) {
762 if (cv != GvCV(gv)) cv = GvCV(gv);
765 autogv = gv_autoload_pvn(
766 GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
767 GvNAMEUTF8(gv) ? SVf_UTF8 : 0
774 SV *tmpstr = sv_newmortal();
775 gv_efullname3(tmpstr, gv, NULL);
776 DIE(aTHX_ "Undefined sort subroutine \"%" SVf "\" called",
781 DIE(aTHX_ "Undefined subroutine in sort");
786 PL_sortcop = (OP*)cv;
788 PL_sortcop = CvSTART(cv);
795 /* optimiser converts "@a = sort @a" to "sort \@a". In this case,
796 * push (@a) onto stack, then assign result back to @a at the end of
798 if (priv & OPpSORT_INPLACE) {
799 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
800 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
801 av = MUTABLE_AV((*SP));
803 Perl_croak_no_modify();
804 max = AvFILL(av) + 1;
807 for (i=0; i < max; i++) {
808 SV **svp = av_fetch(av, i, FALSE);
809 *SP++ = (svp) ? *svp : NULL;
813 SV **svp = AvARRAY(av);
814 assert(svp || max == 0);
815 for (i = 0; i < max; i++)
819 p1 = p2 = SP - (max-1);
826 /* shuffle stack down, removing optional initial cv (p1!=p2), plus
827 * any nulls; also stringify or converting to integer or number as
828 * required any args */
829 copytmps = cBOOL(PL_sortcop);
830 for (i=max; i > 0 ; i--) {
831 if ((*p1 = *p2++)) { /* Weed out nulls. */
832 if (copytmps && SvPADTMP(*p1)) {
833 *p1 = sv_mortalcopy(*p1);
837 if (priv & OPpSORT_NUMERIC) {
838 if (priv & OPpSORT_INTEGER) {
840 (void)sv_2iv_flags(*p1, SV_GMAGIC|SV_SKIP_OVERLOAD);
844 (void)sv_2nv_flags(*p1, SV_GMAGIC|SV_SKIP_OVERLOAD);
845 if (all_SIVs && !SvSIOK(*p1))
851 (void)sv_2pv_flags(*p1, 0,
852 SV_GMAGIC|SV_CONST_RETURN|SV_SKIP_OVERLOAD);
866 const bool oldcatch = CATCH_GET;
867 I32 old_savestack_ix = PL_savestack_ix;
872 PUSHSTACKi(PERLSI_SORT);
873 if (!hasargs && !is_xsub) {
874 SAVEGENERICSV(PL_firstgv);
875 SAVEGENERICSV(PL_secondgv);
876 PL_firstgv = MUTABLE_GV(SvREFCNT_inc(
877 gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV)
879 PL_secondgv = MUTABLE_GV(SvREFCNT_inc(
880 gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV)
882 /* make sure the GP isn't removed out from under us for
884 save_gp(PL_firstgv, 0);
885 save_gp(PL_secondgv, 0);
886 /* we don't want modifications localized */
887 GvINTRO_off(PL_firstgv);
888 GvINTRO_off(PL_secondgv);
889 SAVEGENERICSV(GvSV(PL_firstgv));
890 SvREFCNT_inc(GvSV(PL_firstgv));
891 SAVEGENERICSV(GvSV(PL_secondgv));
892 SvREFCNT_inc(GvSV(PL_secondgv));
896 cx = cx_pushblock(CXt_NULL, gimme, PL_stack_base, old_savestack_ix);
897 if (!(flags & OPf_SPECIAL)) {
898 cx->cx_type = CXt_SUB|CXp_MULTICALL;
899 cx_pushsub(cx, cv, NULL, hasargs);
901 PADLIST * const padlist = CvPADLIST(cv);
903 if (++CvDEPTH(cv) >= 2)
904 pad_push(padlist, CvDEPTH(cv));
905 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
908 /* This is mostly copied from pp_entersub */
909 AV * const av0 = MUTABLE_AV(PAD_SVl(0));
911 cx->blk_sub.savearray = GvAV(PL_defgv);
912 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av0));
919 Perl_sortsv_flags(aTHX_ start, max,
920 (is_xsub ? S_sortcv_xsub : hasargs ? S_sortcv_stacked : S_sortcv),
923 /* Reset cx, in case the context stack has been reallocated. */
926 PL_stack_sp = PL_stack_base + cx->blk_oldsp;
929 if (!(flags & OPf_SPECIAL)) {
930 assert(CxTYPE(cx) == CXt_SUB);
934 assert(CxTYPE(cx) == CXt_NULL);
935 /* there isn't a POPNULL ! */
943 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
945 if (priv & OPpSORT_NUMERIC) {
946 if ((priv & OPpSORT_INTEGER) || all_SIVs) {
949 sortsv_amagic_i_ncmp_desc(aTHX_ start, max, sort_flags);
951 sortsv_amagic_i_ncmp(aTHX_ start, max, sort_flags);
954 sortsv_i_ncmp_desc(aTHX_ start, max, sort_flags);
956 sortsv_i_ncmp(aTHX_ start, max, sort_flags);
961 sortsv_amagic_ncmp_desc(aTHX_ start, max, sort_flags);
963 sortsv_amagic_ncmp(aTHX_ start, max, sort_flags);
966 sortsv_ncmp_desc(aTHX_ start, max, sort_flags);
968 sortsv_ncmp(aTHX_ start, max, sort_flags);
971 #ifdef USE_LOCALE_COLLATE
972 else if(IN_LC_RUNTIME(LC_COLLATE)) {
975 sortsv_amagic_cmp_locale_desc(aTHX_ start, max, sort_flags);
977 sortsv_amagic_cmp_locale(aTHX_ start, max, sort_flags);
980 sortsv_cmp_locale_desc(aTHX_ start, max, sort_flags);
982 sortsv_cmp_locale(aTHX_ start, max, sort_flags);
988 sortsv_amagic_cmp_desc(aTHX_ start, max, sort_flags);
990 sortsv_amagic_cmp(aTHX_ start, max, sort_flags);
993 sortsv_cmp_desc(aTHX_ start, max, sort_flags);
995 sortsv_cmp(aTHX_ start, max, sort_flags);
998 if ((priv & OPpSORT_REVERSE) != 0) {
999 SV **q = start+max-1;
1001 SV * const tmp = *start;
1009 /* copy back result to the array */
1010 SV** const base = MARK+1;
1011 SSize_t max_minus_one = max - 1; /* attempt to work around mingw bug */
1012 if (SvMAGICAL(av)) {
1013 for (i = 0; i <= max_minus_one; i++)
1014 base[i] = newSVsv(base[i]);
1016 if (max_minus_one >= 0)
1017 av_extend(av, max_minus_one);
1018 for (i=0; i <= max_minus_one; i++) {
1019 SV * const sv = base[i];
1020 SV ** const didstore = av_store(av, i, sv);
1028 /* the elements of av are likely to be the same as the
1029 * (non-refcounted) elements on the stack, just in a different
1030 * order. However, its possible that someone's messed with av
1031 * in the meantime. So bump and unbump the relevant refcounts
1034 for (i = 0; i <= max_minus_one; i++) {
1037 if (SvREFCNT(sv) > 1)
1038 base[i] = newSVsv(sv);
1040 SvREFCNT_inc_simple_void_NN(sv);
1043 if (max_minus_one >= 0) {
1044 av_extend(av, max_minus_one);
1045 Copy(base, AvARRAY(av), max, SV*);
1047 AvFILLp(av) = max_minus_one;
1053 PL_stack_sp = ORIGMARK + max;
1058 S_sortcv(pTHX_ SV *const a, SV *const b)
1060 const I32 oldsaveix = PL_savestack_ix;
1062 PMOP * const pm = PL_curpm;
1063 COP * const cop = PL_curcop;
1066 PERL_ARGS_ASSERT_SORTCV;
1068 olda = GvSV(PL_firstgv);
1069 GvSV(PL_firstgv) = SvREFCNT_inc_simple_NN(a);
1071 oldb = GvSV(PL_secondgv);
1072 GvSV(PL_secondgv) = SvREFCNT_inc_simple_NN(b);
1074 PL_stack_sp = PL_stack_base;
1078 /* entry zero of a stack is always PL_sv_undef, which
1079 * simplifies converting a '()' return into undef in scalar context */
1080 assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
1081 result = SvIV(*PL_stack_sp);
1083 LEAVE_SCOPE(oldsaveix);
1089 S_sortcv_stacked(pTHX_ SV *const a, SV *const b)
1091 const I32 oldsaveix = PL_savestack_ix;
1093 AV * const av = GvAV(PL_defgv);
1094 PMOP * const pm = PL_curpm;
1095 COP * const cop = PL_curcop;
1097 PERL_ARGS_ASSERT_SORTCV_STACKED;
1104 if (AvMAX(av) < 1) {
1105 SV **ary = AvALLOC(av);
1106 if (AvARRAY(av) != ary) {
1107 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1110 if (AvMAX(av) < 1) {
1121 PL_stack_sp = PL_stack_base;
1125 /* entry zero of a stack is always PL_sv_undef, which
1126 * simplifies converting a '()' return into undef in scalar context */
1127 assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
1128 result = SvIV(*PL_stack_sp);
1130 LEAVE_SCOPE(oldsaveix);
1136 S_sortcv_xsub(pTHX_ SV *const a, SV *const b)
1139 const I32 oldsaveix = PL_savestack_ix;
1140 CV * const cv=MUTABLE_CV(PL_sortcop);
1142 PMOP * const pm = PL_curpm;
1144 PERL_ARGS_ASSERT_SORTCV_XSUB;
1152 (void)(*CvXSUB(cv))(aTHX_ cv);
1153 /* entry zero of a stack is always PL_sv_undef, which
1154 * simplifies converting a '()' return into undef in scalar context */
1155 assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
1156 result = SvIV(*PL_stack_sp);
1158 LEAVE_SCOPE(oldsaveix);
1164 PERL_STATIC_FORCE_INLINE I32
1165 S_sv_ncmp(pTHX_ SV *const a, SV *const b)
1167 I32 cmp = do_ncmp(a, b);
1169 PERL_ARGS_ASSERT_SV_NCMP;
1172 if (ckWARN(WARN_UNINITIALIZED)) report_uninit(NULL);
1179 PERL_STATIC_FORCE_INLINE I32
1180 S_sv_ncmp_desc(pTHX_ SV *const a, SV *const b)
1182 PERL_ARGS_ASSERT_SV_NCMP_DESC;
1184 return -S_sv_ncmp(aTHX_ a, b);
1187 PERL_STATIC_FORCE_INLINE I32
1188 S_sv_i_ncmp(pTHX_ SV *const a, SV *const b)
1190 const IV iv1 = SvIV(a);
1191 const IV iv2 = SvIV(b);
1193 PERL_ARGS_ASSERT_SV_I_NCMP;
1195 return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0;
1198 PERL_STATIC_FORCE_INLINE I32
1199 S_sv_i_ncmp_desc(pTHX_ SV *const a, SV *const b)
1201 PERL_ARGS_ASSERT_SV_I_NCMP_DESC;
1203 return -S_sv_i_ncmp(aTHX_ a, b);
1206 #define tryCALL_AMAGICbin(left,right,meth) \
1207 (SvAMAGIC(left)||SvAMAGIC(right)) \
1208 ? amagic_call(left, right, meth, 0) \
1211 #define SORT_NORMAL_RETURN_VALUE(val) (((val) > 0) ? 1 : ((val) ? -1 : 0))
1213 PERL_STATIC_FORCE_INLINE I32
1214 S_amagic_ncmp(pTHX_ SV *const a, SV *const b)
1216 SV * const tmpsv = tryCALL_AMAGICbin(a,b,ncmp_amg);
1218 PERL_ARGS_ASSERT_AMAGIC_NCMP;
1222 const I32 i = SvIVX(tmpsv);
1223 return SORT_NORMAL_RETURN_VALUE(i);
1226 const NV d = SvNV(tmpsv);
1227 return SORT_NORMAL_RETURN_VALUE(d);
1230 return S_sv_ncmp(aTHX_ a, b);
1233 PERL_STATIC_FORCE_INLINE I32
1234 S_amagic_ncmp_desc(pTHX_ SV *const a, SV *const b)
1236 PERL_ARGS_ASSERT_AMAGIC_NCMP_DESC;
1238 return -S_amagic_ncmp(aTHX_ a, b);
1241 PERL_STATIC_FORCE_INLINE I32
1242 S_amagic_i_ncmp(pTHX_ SV *const a, SV *const b)
1244 SV * const tmpsv = tryCALL_AMAGICbin(a,b,ncmp_amg);
1246 PERL_ARGS_ASSERT_AMAGIC_I_NCMP;
1250 const I32 i = SvIVX(tmpsv);
1251 return SORT_NORMAL_RETURN_VALUE(i);
1254 const NV d = SvNV(tmpsv);
1255 return SORT_NORMAL_RETURN_VALUE(d);
1258 return S_sv_i_ncmp(aTHX_ a, b);
1261 PERL_STATIC_FORCE_INLINE I32
1262 S_amagic_i_ncmp_desc(pTHX_ SV *const a, SV *const b)
1264 PERL_ARGS_ASSERT_AMAGIC_I_NCMP_DESC;
1266 return -S_amagic_i_ncmp(aTHX_ a, b);
1269 PERL_STATIC_FORCE_INLINE I32
1270 S_amagic_cmp(pTHX_ SV *const str1, SV *const str2)
1272 SV * const tmpsv = tryCALL_AMAGICbin(str1,str2,scmp_amg);
1274 PERL_ARGS_ASSERT_AMAGIC_CMP;
1278 const I32 i = SvIVX(tmpsv);
1279 return SORT_NORMAL_RETURN_VALUE(i);
1282 const NV d = SvNV(tmpsv);
1283 return SORT_NORMAL_RETURN_VALUE(d);
1286 return sv_cmp(str1, str2);
1289 PERL_STATIC_FORCE_INLINE I32
1290 S_amagic_cmp_desc(pTHX_ SV *const str1, SV *const str2)
1292 PERL_ARGS_ASSERT_AMAGIC_CMP_DESC;
1294 return -S_amagic_cmp(aTHX_ str1, str2);
1297 PERL_STATIC_FORCE_INLINE I32
1298 S_cmp_desc(pTHX_ SV *const str1, SV *const str2)
1300 PERL_ARGS_ASSERT_CMP_DESC;
1302 return -sv_cmp(str1, str2);
1305 #ifdef USE_LOCALE_COLLATE
1307 PERL_STATIC_FORCE_INLINE I32
1308 S_amagic_cmp_locale(pTHX_ SV *const str1, SV *const str2)
1310 SV * const tmpsv = tryCALL_AMAGICbin(str1,str2,scmp_amg);
1312 PERL_ARGS_ASSERT_AMAGIC_CMP_LOCALE;
1316 const I32 i = SvIVX(tmpsv);
1317 return SORT_NORMAL_RETURN_VALUE(i);
1320 const NV d = SvNV(tmpsv);
1321 return SORT_NORMAL_RETURN_VALUE(d);
1324 return sv_cmp_locale(str1, str2);
1327 PERL_STATIC_FORCE_INLINE I32
1328 S_amagic_cmp_locale_desc(pTHX_ SV *const str1, SV *const str2)
1330 PERL_ARGS_ASSERT_AMAGIC_CMP_LOCALE_DESC;
1332 return -S_amagic_cmp_locale(aTHX_ str1, str2);
1335 PERL_STATIC_FORCE_INLINE I32
1336 S_cmp_locale_desc(pTHX_ SV *const str1, SV *const str2)
1338 PERL_ARGS_ASSERT_CMP_LOCALE_DESC;
1340 return -sv_cmp_locale(str1, str2);
1346 * ex: set ts=8 sts=4 sw=4 et: