Skip to contentSkip to conversation

speaker-test: move existing endian macros up in the file

PATCH New Dan McGee 1 participant 2 messages May 10, 2009

lore
Share & tools
lore
author
#1 lore raw

PATCH 2/2 speaker-test: only check byte order once

In reply to Dan McGee

Rather than having numerous preprocessor directives scattered in the code checking __BYTE_ORDER, only check it once and define a set of macros accordingly that can be used in the rest of the code. This makes things simpler to read and less error-prone.

---
 speaker-test/speaker-test.c |   54 ++++++++++--------------------------------
 1 files changed, 13 insertions(+), 41 deletions(-)
speaker-test/speaker-test.c +13 -41
index c1d53b6..e22c937 100644
@@ -72,10 +72,14 @@ enum {
 #define COMPOSE_ID(a,b,c,d)	((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
 #define LE_SHORT(v)		(v)
 #define LE_INT(v)		(v)
-#else
+#define BE_SHORT(v)		bswap_16(v)
+#define BE_INT(v)		bswap_32(v)
+#else /* __BIG_ENDIAN */
 #define COMPOSE_ID(a,b,c,d)	((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
 #define LE_SHORT(v)		bswap_16(v)
 #define LE_INT(v)		bswap_32(v)
+#define BE_SHORT(v)		(v)
+#define BE_INT(v)		(v)
 #endif
 
 static char              *device      = "default";       /* playback device */
@@ -167,11 +171,7 @@ static void generate_sine(uint8_t *frames, int channel, int count, double *_phas
         if (chn==channel) {
           res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */
           ires = res;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp16++ = ires >> 16;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-          *samp16++ = bswap_16(ires >> 16);
-#endif
+          *samp16++ = LE_SHORT(ires >> 16);
         } else {
           *samp16++ = 0;
         }
@@ -180,11 +180,7 @@ static void generate_sine(uint8_t *frames, int channel, int count, double *_phas
         if (chn==channel) {
           res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */
           ires = res;
-#if __BYTE_ORDER == __BIG_ENDIAN
-          *samp16++ = ires >> 16;
-#elif __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp16++ = bswap_16(ires >> 16);
-#endif
+          *samp16++ = BE_SHORT(ires >> 16);
         } else {
           *samp16++ = 0;
         }
@@ -202,11 +198,7 @@ static void generate_sine(uint8_t *frames, int channel, int count, double *_phas
         if (chn==channel) {
           res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */
           ires = res;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp32++ = ires;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-          *samp32++ = bswap_32(ires);
-#endif
+          *samp32++ = LE_INT(ires);
         } else {
           *samp32++ = 0;
         }
@@ -215,11 +207,7 @@ static void generate_sine(uint8_t *frames, int channel, int count, double *_phas
         if (chn==channel) {
           res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */
           ires = res;
-#if __BYTE_ORDER == __BIG_ENDIAN
-          *samp32++ = ires;
-#elif __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp32++ = bswap_32(ires);
-#endif
+          *samp32++ = BE_INT(ires);
         } else {
           *samp32++ = 0;
         }
@@ -266,11 +254,7 @@ static void generate_pink_noise( uint8_t *frames, int channel, int count) {
         if (chn==channel) {
 	  res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */
 	  ires = res;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp16++ = ires >> 16;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-          *samp16++ = bswap_16(ires >> 16);
-#endif
+          *samp16++ = LE_SHORT(ires >> 16);
         } else {
 	  *samp16++ = 0;
         }
@@ -279,11 +263,7 @@ static void generate_pink_noise( uint8_t *frames, int channel, int count) {
         if (chn==channel) {
           res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */
           ires = res;
-#if __BYTE_ORDER == __BIG_ENDIAN
-          *samp16++ = ires >> 16;
-#elif __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp16++ = bswap_16(ires >> 16);
-#endif
+          *samp16++ = BE_SHORT(ires >> 16);
         } else {
           *samp16++ = 0;
         }
@@ -292,11 +272,7 @@ static void generate_pink_noise( uint8_t *frames, int channel, int count) {
         if (chn==channel) {
           res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */
           ires = res;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp32++ = ires;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-          *samp32++ = bswap_32(ires);
-#endif
+          *samp32++ = LE_INT(ires);
         } else {
           *samp32++ = 0;
         }
@@ -305,11 +281,7 @@ static void generate_pink_noise( uint8_t *frames, int channel, int count) {
         if (chn==channel) {
 	  res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */
 	  ires = res;
-#if __BYTE_ORDER == __BIG_ENDIAN
-	  *samp32++ = ires;
-#elif __BYTE_ORDER == __LITTLE_ENDIAN
-          *samp32++ = bswap_32(ires);
-#endif
+	  *samp32++ = BE_INT(ires);
         } else {
 	  *samp32++ = 0;
         }
Signature · 1 line
1.6.2.4

Dan McGee

author
#0 lore raw

PATCH 1/2 speaker-test: move existing endian macros up in the file

This is necessary for a later patch removing the various endianness checks sprinkled throughout the code.

---
 speaker-test/speaker-test.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
speaker-test/speaker-test.c +10 -10
index d983a4f..c1d53b6 100644
@@ -68,6 +68,16 @@ enum {
 
 #define MAX_CHANNELS	16
 
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define COMPOSE_ID(a,b,c,d)	((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
+#define LE_SHORT(v)		(v)
+#define LE_INT(v)		(v)
+#else
+#define COMPOSE_ID(a,b,c,d)	((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
+#define LE_SHORT(v)		bswap_16(v)
+#define LE_INT(v)		bswap_32(v)
+#endif
+
 static char              *device      = "default";       /* playback device */
 static snd_pcm_format_t   format      = SND_PCM_FORMAT_S16; /* sample format */
 static unsigned int       rate        = 48000;	            /* stream rate */
@@ -520,16 +530,6 @@ struct wave_header {
   } chunk;
 };
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-#define COMPOSE_ID(a,b,c,d)	((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
-#define LE_SHORT(v)		(v)
-#define LE_INT(v)		(v)
-#else
-#define COMPOSE_ID(a,b,c,d)	((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
-#define LE_SHORT(v)		bswap_16(v)
-#define LE_INT(v)		bswap_32(v)
-#endif
-
 #define WAV_RIFF		COMPOSE_ID('R','I','F','F')
 #define WAV_WAVE		COMPOSE_ID('W','A','V','E')
 #define WAV_FMT			COMPOSE_ID('f','m','t',' ')
Signature · 1 line
1.6.2.4