On Thu, May 10, 2018 at 07:40:57PM +0200, Richard Biener wrote: > On May 10, 2018 5:56:39 PM GMT+02:00, Alexander Monakov <amona...@ispras.ru> > wrote: > >/* This implements a sort function suitable for GCC use cases: > > - signature-compatible to C qsort, but relaxed contract: > > - may apply the comparator to elements in a temporary buffer > > What consequences has this or rather how is this observable and makes > comparators behave differently?
A comparison function is allowed to compare the addresses of the elements, or compute what should be the index into the array that is sorted from the pointers to the elements, etc. That will not work if the pointers to the elements do not actually point into the original array. For example from reload1.c: static int reload_reg_class_lower (const void *r1p, const void *r2p) { int r1 = *(const short *) r1p, r2 = *(const short *) r2p; // ... return r1 - r2; } (called as static short reload_order[MAX_RELOADS]; // ... qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower); ). Such a comparator will still work with temporary arrays if both elements are always in the same temp array (in deterministic order, etc.) array; but it won't work if the comparator did e.g. return (r1 - reload_order) - (r2 - reload_order); (it would be undefined behaviour, to start with). Segher