diff options
author | Alexander Monakov <amonakov@ispras.ru> | 2018-05-14 11:51:51 +0300 |
---|---|---|
committer | Alexander Monakov <amonakov@gcc.gnu.org> | 2018-05-14 11:51:51 +0300 |
commit | c86ed2f12bccff7558c1032e22bc603e73941547 (patch) | |
tree | 1996c15cb9d81b639c81a6cea57abe08c1eca27d /gcc | |
parent | cfbd17d53edf1b8a84bdabdaef30a5833123d056 (diff) | |
download | gcc-c86ed2f12bccff7558c1032e22bc603e73941547.zip gcc-c86ed2f12bccff7558c1032e22bc603e73941547.tar.gz gcc-c86ed2f12bccff7558c1032e22bc603e73941547.tar.bz2 |
gcc_qsort: avoid oversized memcpy temporaries
* sort.cc (REORDER_23): Pass the type for the temporaries instead of
intended memcpy size.
(REORDER_45): Likewise.
From-SVN: r260222
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/sort.cc | 72 |
2 files changed, 42 insertions, 36 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 2a24628..edf2e41 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-05-14 Alexander Monakov <amonakov@ispras.ru> + + * sort.cc (REORDER_23): Pass the type for the temporaries instead of + intended memcpy size. + (REORDER_45): Likewise. + 2018-05-13 Alexander Monakov <amonakov@ispras.ru> * sort.cc: New file. diff --git a/gcc/sort.cc b/gcc/sort.cc index 28d5226..a48a477 100644 --- a/gcc/sort.cc +++ b/gcc/sort.cc @@ -62,29 +62,29 @@ struct sort_ctx static void reorder23 (sort_ctx *c, char *e0, char *e1, char *e2) { -#define REORDER_23(SIZE, STRIDE, OFFSET) \ -do { \ - size_t t0, t1; \ - memcpy (&t0, e0 + OFFSET, SIZE); \ - memcpy (&t1, e1 + OFFSET, SIZE); \ - char *out = c->out + OFFSET; \ - if (likely (c->n == 3)) \ - memcpy (out + 2*STRIDE, e2 + OFFSET, SIZE); \ - memcpy (out, &t0, SIZE); out += STRIDE; \ - memcpy (out, &t1, SIZE); \ +#define REORDER_23(TYPE, STRIDE, OFFSET) \ +do { \ + TYPE t0, t1; \ + memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \ + memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \ + char *out = c->out + OFFSET; \ + if (likely (c->n == 3)) \ + memcpy (out + 2*STRIDE, e2 + OFFSET, sizeof (TYPE)); \ + memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \ + memcpy (out, &t1, sizeof (TYPE)); \ } while (0) - if (sizeof (size_t) == 8 && likely (c->size == 8)) - REORDER_23 (8, 8, 0); - else if (likely (c->size == 4)) - REORDER_23 (4, 4, 0); + if (likely (c->size == sizeof (size_t))) + REORDER_23 (size_t, sizeof (size_t), 0); + else if (likely (c->size == sizeof (int))) + REORDER_23 (int, sizeof (int), 0); else { size_t offset = 0, step = sizeof (size_t); for (; offset + step <= c->size; offset += step) - REORDER_23 (step, c->size, offset); + REORDER_23 (size_t, c->size, offset); for (; offset < c->size; offset++) - REORDER_23 (1, c->size, offset); + REORDER_23 (char, c->size, offset); } } @@ -92,33 +92,33 @@ do { \ static void reorder45 (sort_ctx *c, char *e0, char *e1, char *e2, char *e3, char *e4) { -#define REORDER_45(SIZE, STRIDE, OFFSET) \ -do { \ - size_t t0, t1, t2, t3; \ - memcpy (&t0, e0 + OFFSET, SIZE); \ - memcpy (&t1, e1 + OFFSET, SIZE); \ - memcpy (&t2, e2 + OFFSET, SIZE); \ - memcpy (&t3, e3 + OFFSET, SIZE); \ - char *out = c->out + OFFSET; \ - if (likely (c->n == 5)) \ - memcpy (out + 4*STRIDE, e4 + OFFSET, SIZE); \ - memcpy (out, &t0, SIZE); out += STRIDE; \ - memcpy (out, &t1, SIZE); out += STRIDE; \ - memcpy (out, &t2, SIZE); out += STRIDE; \ - memcpy (out, &t3, SIZE); \ +#define REORDER_45(TYPE, STRIDE, OFFSET) \ +do { \ + TYPE t0, t1, t2, t3; \ + memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \ + memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \ + memcpy (&t2, e2 + OFFSET, sizeof (TYPE)); \ + memcpy (&t3, e3 + OFFSET, sizeof (TYPE)); \ + char *out = c->out + OFFSET; \ + if (likely (c->n == 5)) \ + memcpy (out + 4*STRIDE, e4 + OFFSET, sizeof (TYPE)); \ + memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \ + memcpy (out, &t1, sizeof (TYPE)); out += STRIDE; \ + memcpy (out, &t2, sizeof (TYPE)); out += STRIDE; \ + memcpy (out, &t3, sizeof (TYPE)); \ } while (0) - if (sizeof (size_t) == 8 && likely (c->size == 8)) - REORDER_45 (8, 8, 0); - else if (likely(c->size == 4)) - REORDER_45 (4, 4, 0); + if (likely (c->size == sizeof (size_t))) + REORDER_45 (size_t, sizeof (size_t), 0); + else if (likely(c->size == sizeof (int))) + REORDER_45 (int, sizeof (int), 0); else { size_t offset = 0, step = sizeof (size_t); for (; offset + step <= c->size; offset += step) - REORDER_45 (step, c->size, offset); + REORDER_45 (size_t, c->size, offset); for (; offset < c->size; offset++) - REORDER_45 (1, c->size, offset); + REORDER_45 (char, c->size, offset); } } |