From 6e38d50829efca4270459bb9f74c5230f34345e2 Mon Sep 17 00:00:00 2001 From: Nick Briggs Date: Sun, 30 Oct 2022 15:49:32 -0700 Subject: [PATCH] Improve implementation of word_swap_page() and remove unused code (#447) word_swap_page() should only touch each 32-bit word once to change the byte order from ABCD to DCBA rather than twice doing a transform of the entire region from ABCD to BADC and then again to DCBA The compiler provided ntohl() usually gets favorable optimizations applied, so use it for the byte reordering. With word_swap_page() rewritten, byte_swap_word() is superfluous. --- inc/byteswapdefs.h | 10 ---------- src/byteswap.c | 21 +++------------------ 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/inc/byteswapdefs.h b/inc/byteswapdefs.h index 9c9d1ac..83cc6d1 100644 --- a/inc/byteswapdefs.h +++ b/inc/byteswapdefs.h @@ -10,16 +10,6 @@ static inline unsigned int swapx(unsigned int word) { return (((word >> 16) & 0xffff) | ((word & 0xffff) << 16)); } -/****************************************************************/ -/* */ -/* Byte-swap a single 2-byte word */ -/* */ -/****************************************************************/ -static inline unsigned short byte_swap_word(unsigned short word) { - return ((word >> 8) | (unsigned short)((word & 0xff) << 8)); -} - -void byte_swap_page(unsigned short *page, int wordcount); void word_swap_page(unsigned short *page, int longwordcount); void bit_reverse_region(unsigned short *top, int width, int height, int rasterwidth); #ifdef RESWAPPEDCODESTREAM diff --git a/src/byteswap.c b/src/byteswap.c index 51f7c2c..ae52456 100644 --- a/src/byteswap.c +++ b/src/byteswap.c @@ -19,33 +19,18 @@ /* */ /***************************************************************************/ +#include #include "byteswapdefs.h" #include "lsptypes.h" -/****************************************************************/ -/* */ -/* Byte-swap a region wordcount words long */ -/* This does NOT swap words in a long-word! */ -/* */ -/****************************************************************/ -void byte_swap_page(unsigned short *page, int wordcount) { - int i; - for (i = 0; i < wordcount; i++) { *(page + i) = byte_swap_word(*(page + i)); } -} - /****************************************************************/ /* */ /* Byte- & word-swap a region wordcount long-words long */ /* */ /****************************************************************/ void word_swap_page(unsigned short *page, int longwordcount) { - int i; - unsigned int *longpage; - longpage = (unsigned int *)page; - for (i = 0; i < (longwordcount + longwordcount); i++) { - *(page + i) = byte_swap_word(*(page + i)); - } - for (i = 0; i < longwordcount; i++) { *(longpage + i) = swapx(*(longpage + i)); } + unsigned int *longpage = (unsigned int *)page; + for (int i = 0; i < longwordcount; i++) { *(longpage + i) = ntohl(*(longpage + i)); } } /****************************************************************/