ewah: unconditionally ntohll ewah data
authorJeff King <peff@peff.net>
Wed, 12 Feb 2014 16:48:28 +0000 (11:48 -0500)
committerJunio C Hamano <gitster@pobox.com>
Wed, 12 Feb 2014 19:21:29 +0000 (11:21 -0800)
Commit a201c20 tried to optimize out a loop like:

for (i = 0; i < len; i++)
data[i] = ntohll(data[i]);

in the big-endian case, because we know that ntohll is a
noop, and we do not need to pay the cost of the loop at all.
However, it mistakenly assumed that __BYTE_ORDER was always
defined, whereas it may not be on systems which do not
define it by default, and where we did not need to define it
to set up the ntohll macro. This includes OS X and Windows.

We could muck with the ordering in compat/bswap.h to make
sure it is defined unconditionally, but it is simpler to
still to just execute the loop unconditionally. That avoids
the application code knowing anything about these magic
macros, and lets it depend only on having ntohll defined.

And since the resulting loop looks like (on a big-endian
system):

for (i = 0; i < len; i++)
data[i] = data[i];

any decent compiler can probably optimize it out.

Original report and analysis by Brian Gernhardt.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ewah/ewah_io.c
index 4a7fae6b8ceabcb5202c615724b73606795aa543..f7f700ef516eea2fe908f66b1518b5e3a1d9fd33 100644 (file)
@@ -113,6 +113,7 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)
 int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)
 {
        uint8_t *ptr = map;
+       size_t i;
 
        self->bit_size = get_be32(ptr);
        ptr += sizeof(uint32_t);
@@ -135,13 +136,8 @@ int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)
        memcpy(self->buffer, ptr, self->buffer_size * sizeof(uint64_t));
        ptr += self->buffer_size * sizeof(uint64_t);
 
-#if __BYTE_ORDER != __BIG_ENDIAN
-       {
-               size_t i;
-               for (i = 0; i < self->buffer_size; ++i)
-                       self->buffer[i] = ntohll(self->buffer[i]);
-       }
-#endif
+       for (i = 0; i < self->buffer_size; ++i)
+               self->buffer[i] = ntohll(self->buffer[i]);
 
        self->rlw = self->buffer + get_be32(ptr);