Replace SWAB16 and SWAB32 with a comprehensive set of byte-swap macros - letoh16, letoh32, htole16, htole32, betoh16, betoh32, htobe16 and htobe32

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7584 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dave Chapman 2005-10-06 19:27:43 +00:00
parent 1c3a83c44e
commit 9e19c95d8e
8 changed files with 89 additions and 60 deletions

View file

@ -42,15 +42,6 @@ enum {
MPC_DECODER_SYNTH_DELAY = 481 MPC_DECODER_SYNTH_DELAY = 481
}; };
/// Big/little endian 32 bit byte swapping routine.
static inline
mpc_uint32_t swap32(mpc_uint32_t val) {
const unsigned char* src = (const unsigned char*)&val;
return
(mpc_uint32_t)src[0] |
((mpc_uint32_t)src[1] << 8) | ((mpc_uint32_t)src[2] << 16) | ((mpc_uint32_t)src[3] << 24);
}
/// Searches for a ID3v2-tag and reads the length (in bytes) of it. /// Searches for a ID3v2-tag and reads the length (in bytes) of it.
/// \param reader supplying raw stream data /// \param reader supplying raw stream data
/// \return size of tag, in bytes /// \return size of tag, in bytes

View file

@ -482,7 +482,7 @@ enum codec_status codec_start(struct codec_api* api)
else if (bitspersample > 8) { else if (bitspersample > 8) {
/* Byte-swap data. */ /* Byte-swap data. */
for (i=0;i<n/2;i++) { for (i=0;i<n/2;i++) {
int16_samples[i]=(int16_t)SWAB16(wavbuf[i]); int16_samples[i]=(int16_t)letoh16(wavbuf[i]);
} }
wavbufsize = n; wavbufsize = n;
} }

View file

@ -152,7 +152,7 @@ static void convert_endian(void *data, const char *format)
{ {
long* d = (long*) data; long* d = (long*) data;
*d = SWAB32(*d); *d = letoh32(*d);
data = d + 1; data = d + 1;
} }
@ -162,7 +162,7 @@ static void convert_endian(void *data, const char *format)
{ {
short* d = (short*) data; short* d = (short*) data;
*d = SWAB16(*d); *d = letoh16(*d);
data = d + 1; data = d + 1;
} }

View file

@ -97,12 +97,11 @@ struct GWaveform * loadWaveform(int file)
*/ */
/* Iriver needs byteswapping.. big endian, go figure. Gus files are little endian */ #ifdef ROCKBOX_BIG_ENDIAN
/* Byte-swap if necessary. Gus files are little endian */
#if !defined(SIMULATOR)
for(a=0; a<wav->numSamples; a++) for(a=0; a<wav->numSamples; a++)
{ {
((unsigned short *) wav->data)[a] = SWAB16(((unsigned short *) wav->data)[a]); ((unsigned short *) wav->data)[a] = letoh16(((unsigned short *) wav->data)[a]);
} }
#endif #endif

View file

@ -362,7 +362,7 @@ static void copy_read_sectors(unsigned char* buf, int wordcount)
{ /* loop compiles to 7 assembler instructions */ { /* loop compiles to 7 assembler instructions */
/* takes 12 clock cycles (2 pipeline stalls, 1 wait) */ /* takes 12 clock cycles (2 pipeline stalls, 1 wait) */
#ifdef SWAP_WORDS #ifdef SWAP_WORDS
*wbuf = SWAB16(ATA_DATA); *wbuf = letoh16(ATA_DATA);
#else #else
*wbuf = ATA_DATA; *wbuf = ATA_DATA;
#endif #endif
@ -677,7 +677,7 @@ static void copy_write_sectors(const unsigned char* buf, int wordcount)
#ifdef SWAP_WORDS #ifdef SWAP_WORDS
/* loop compiles to 6 assembler instructions */ /* loop compiles to 6 assembler instructions */
/* takes 10 clock cycles (2 pipeline stalls) */ /* takes 10 clock cycles (2 pipeline stalls) */
SET_16BITREG(ATA_DATA, SWAB16(*wbuf)); SET_16BITREG(ATA_DATA, htole16(*wbuf));
#else #else
SET_16BITREG(ATA_DATA, *wbuf); SET_16BITREG(ATA_DATA, *wbuf);
#endif #endif
@ -1270,7 +1270,7 @@ static int identify(void)
#ifdef SWAP_WORDS #ifdef SWAP_WORDS
identify_info[i] = ATA_DATA; identify_info[i] = ATA_DATA;
#else #else
identify_info[i] = SWAB16(ATA_DATA); identify_info[i] = letoh16(ATA_DATA);
#endif #endif
} }

View file

@ -534,7 +534,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume))
if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */ if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */
break; break;
if (SWAB16(fat[j]) == 0x0000) { if (letoh16(fat[j]) == 0x0000) {
free++; free++;
if ( fat_bpb->fsinfo.nextfree == 0xffffffff ) if ( fat_bpb->fsinfo.nextfree == 0xffffffff )
fat_bpb->fsinfo.nextfree = c; fat_bpb->fsinfo.nextfree = c;
@ -553,7 +553,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume))
if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */ if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */
break; break;
if (!(SWAB32(fat[j]) & 0x0fffffff)) { if (!(letoh32(fat[j]) & 0x0fffffff)) {
free++; free++;
if ( fat_bpb->fsinfo.nextfree == 0xffffffff ) if ( fat_bpb->fsinfo.nextfree == 0xffffffff )
fat_bpb->fsinfo.nextfree = c; fat_bpb->fsinfo.nextfree = c;
@ -740,7 +740,7 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon
break; break;
for (j = 0; j < CLUSTERS_PER_FAT16_SECTOR; j++) { for (j = 0; j < CLUSTERS_PER_FAT16_SECTOR; j++) {
int k = (j + offset) % CLUSTERS_PER_FAT16_SECTOR; int k = (j + offset) % CLUSTERS_PER_FAT16_SECTOR;
if (SWAB16(fat[k]) == 0x0000) { if (letoh16(fat[k]) == 0x0000) {
unsigned int c = nr * CLUSTERS_PER_FAT16_SECTOR + k; unsigned int c = nr * CLUSTERS_PER_FAT16_SECTOR + k;
/* Ignore the reserved clusters 0 & 1, and also /* Ignore the reserved clusters 0 & 1, and also
cluster numbers out of bounds */ cluster numbers out of bounds */
@ -768,7 +768,7 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon
break; break;
for (j = 0; j < CLUSTERS_PER_FAT_SECTOR; j++) { for (j = 0; j < CLUSTERS_PER_FAT_SECTOR; j++) {
int k = (j + offset) % CLUSTERS_PER_FAT_SECTOR; int k = (j + offset) % CLUSTERS_PER_FAT_SECTOR;
if (!(SWAB32(fat[k]) & 0x0fffffff)) { if (!(letoh32(fat[k]) & 0x0fffffff)) {
unsigned long c = nr * CLUSTERS_PER_FAT_SECTOR + k; unsigned long c = nr * CLUSTERS_PER_FAT_SECTOR + k;
/* Ignore the reserved clusters 0 & 1, and also /* Ignore the reserved clusters 0 & 1, and also
cluster numbers out of bounds */ cluster numbers out of bounds */
@ -819,17 +819,17 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,)
} }
if ( val ) { if ( val ) {
if (SWAB16(sec[offset]) == 0x0000 && fat_bpb->fsinfo.freecount > 0) if (htole16(sec[offset]) == 0x0000 && fat_bpb->fsinfo.freecount > 0)
fat_bpb->fsinfo.freecount--; fat_bpb->fsinfo.freecount--;
} }
else { else {
if (SWAB16(sec[offset])) if (htole16(sec[offset]))
fat_bpb->fsinfo.freecount++; fat_bpb->fsinfo.freecount++;
} }
LDEBUGF("update_fat_entry: %d free clusters\n", fat_bpb->fsinfo.freecount); LDEBUGF("update_fat_entry: %d free clusters\n", fat_bpb->fsinfo.freecount);
sec[offset] = SWAB16(val); sec[offset] = htole16(val);
} }
else else
#endif /* #ifdef HAVE_FAT16SUPPORT */ #endif /* #ifdef HAVE_FAT16SUPPORT */
@ -854,20 +854,20 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,)
} }
if ( val ) { if ( val ) {
if (!(SWAB32(sec[offset]) & 0x0fffffff) && if (!(htole32(sec[offset]) & 0x0fffffff) &&
fat_bpb->fsinfo.freecount > 0) fat_bpb->fsinfo.freecount > 0)
fat_bpb->fsinfo.freecount--; fat_bpb->fsinfo.freecount--;
} }
else { else {
if (SWAB32(sec[offset]) & 0x0fffffff) if (htole32(sec[offset]) & 0x0fffffff)
fat_bpb->fsinfo.freecount++; fat_bpb->fsinfo.freecount++;
} }
LDEBUGF("update_fat_entry: %ld free clusters\n", fat_bpb->fsinfo.freecount); LDEBUGF("update_fat_entry: %ld free clusters\n", fat_bpb->fsinfo.freecount);
/* don't change top 4 bits */ /* don't change top 4 bits */
sec[offset] &= SWAB32(0xf0000000); sec[offset] &= htole32(0xf0000000);
sec[offset] |= SWAB32(val & 0x0fffffff); sec[offset] |= htole32(val & 0x0fffffff);
} }
return 0; return 0;
@ -892,7 +892,7 @@ static long read_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry)
return -1; return -1;
} }
return SWAB16(sec[offset]); return letoh16(sec[offset]);
} }
else else
#endif /* #ifdef HAVE_FAT16SUPPORT */ #endif /* #ifdef HAVE_FAT16SUPPORT */
@ -908,7 +908,7 @@ static long read_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry)
return -1; return -1;
} }
return SWAB32(sec[offset]) & 0x0fffffff; return letoh32(sec[offset]) & 0x0fffffff;
} }
} }
@ -960,10 +960,10 @@ static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb))
return rc * 10 - 1; return rc * 10 - 1;
} }
intptr = (long*)&(fsinfo[FSINFO_FREECOUNT]); intptr = (long*)&(fsinfo[FSINFO_FREECOUNT]);
*intptr = SWAB32(fat_bpb->fsinfo.freecount); *intptr = htole32(fat_bpb->fsinfo.freecount);
intptr = (long*)&(fsinfo[FSINFO_NEXTFREE]); intptr = (long*)&(fsinfo[FSINFO_NEXTFREE]);
*intptr = SWAB32(fat_bpb->fsinfo.nextfree); *intptr = htole32(fat_bpb->fsinfo.nextfree);
rc = ata_write_sectors(IF_MV2(fat_bpb->drive,) rc = ata_write_sectors(IF_MV2(fat_bpb->drive,)
fat_bpb->startsector + fat_bpb->bpb_fsinfo,1,fsinfo); fat_bpb->startsector + fat_bpb->bpb_fsinfo,1,fsinfo);
@ -1212,11 +1212,11 @@ static int write_long_name(struct fat_file* file,
fat_time(&date, &time, &tenth); fat_time(&date, &time, &tenth);
entry[FATDIR_CRTTIMETENTH] = tenth; entry[FATDIR_CRTTIMETENTH] = tenth;
*(unsigned short*)(entry + FATDIR_CRTTIME) = SWAB16(time); *(unsigned short*)(entry + FATDIR_CRTTIME) = htole16(time);
*(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); *(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time);
*(unsigned short*)(entry + FATDIR_CRTDATE) = SWAB16(date); *(unsigned short*)(entry + FATDIR_CRTDATE) = htole16(date);
*(unsigned short*)(entry + FATDIR_WRTDATE) = SWAB16(date); *(unsigned short*)(entry + FATDIR_WRTDATE) = htole16(date);
*(unsigned short*)(entry + FATDIR_LSTACCDATE) = SWAB16(date); *(unsigned short*)(entry + FATDIR_LSTACCDATE) = htole16(date);
} }
idx++; idx++;
nameidx -= NAME_BYTES_PER_ENTRY; nameidx -= NAME_BYTES_PER_ENTRY;
@ -1520,13 +1520,13 @@ static int update_short_entry( struct fat_file* file, long size, int attr )
entry[FATDIR_ATTR] = attr & 0xFF; entry[FATDIR_ATTR] = attr & 0xFF;
clusptr = (short*)(entry + FATDIR_FSTCLUSHI); clusptr = (short*)(entry + FATDIR_FSTCLUSHI);
*clusptr = SWAB16(file->firstcluster >> 16); *clusptr = htole16(file->firstcluster >> 16);
clusptr = (short*)(entry + FATDIR_FSTCLUSLO); clusptr = (short*)(entry + FATDIR_FSTCLUSLO);
*clusptr = SWAB16(file->firstcluster & 0xffff); *clusptr = htole16(file->firstcluster & 0xffff);
sizeptr = (long*)(entry + FATDIR_FILESIZE); sizeptr = (long*)(entry + FATDIR_FILESIZE);
*sizeptr = SWAB32(size); *sizeptr = htole32(size);
{ {
#ifdef HAVE_RTC #ifdef HAVE_RTC
@ -1534,13 +1534,13 @@ static int update_short_entry( struct fat_file* file, long size, int attr )
unsigned short date = 0; unsigned short date = 0;
#else #else
/* get old time to increment from */ /* get old time to increment from */
unsigned short time = SWAB16(*(unsigned short*)(entry + FATDIR_WRTTIME)); unsigned short time = htole16(*(unsigned short*)(entry + FATDIR_WRTTIME));
unsigned short date = SWAB16(*(unsigned short*)(entry + FATDIR_WRTDATE)); unsigned short date = htole16(*(unsigned short*)(entry + FATDIR_WRTDATE));
#endif #endif
fat_time(&date, &time, NULL); fat_time(&date, &time, NULL);
*(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); *(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time);
*(unsigned short*)(entry + FATDIR_WRTDATE) = SWAB16(date); *(unsigned short*)(entry + FATDIR_WRTDATE) = htole16(date);
*(unsigned short*)(entry + FATDIR_LSTACCDATE) = SWAB16(date); *(unsigned short*)(entry + FATDIR_LSTACCDATE) = htole16(date);
} }
rc = fat_seek( &dir, sector ); rc = fat_seek( &dir, sector );

View file

@ -56,10 +56,26 @@ void cpu_idle_mode(bool on_off);
#endif #endif
#ifdef ROCKBOX_LITTLE_ENDIAN #ifdef ROCKBOX_LITTLE_ENDIAN
#define SWAB16(x) (x) #define letoh16(x) (x)
#define SWAB32(x) (x) #define letoh32(x) (x)
#define htole16(x) (x)
#define htole32(x) (x)
#define betoh16(x) swap16(x)
#define betoh32(x) swap32(x)
#define htobe16(x) swap16(x)
#define htobe32(x) swap32(x)
#else
#define letoh16(x) swap16(x)
#define letoh32(x) swap32(x)
#define htole16(x) swap16(x)
#define htole32(x) swap32(x)
#define betoh16(x) (x)
#define betoh32(x) (x)
#define htobe16(x) (x)
#define htobe32(x) (x)
#endif #endif
#define nop \ #define nop \
asm volatile ("nop") asm volatile ("nop")
@ -162,7 +178,7 @@ static inline int set_irq_level(int level)
return i; return i;
} }
static inline unsigned short SWAB16(unsigned short value) static inline unsigned short swap16(unsigned short value)
/* /*
result[15..8] = value[ 7..0]; result[15..8] = value[ 7..0];
result[ 7..0] = value[15..8]; result[ 7..0] = value[15..8];
@ -184,7 +200,7 @@ static inline unsigned long SWAW32(unsigned long value)
return result; return result;
} }
static inline unsigned long SWAB32(unsigned long value) static inline unsigned long swap32(unsigned long value)
/* /*
result[31..24] = value[ 7.. 0]; result[31..24] = value[ 7.. 0];
result[23..16] = value[15.. 8]; result[23..16] = value[15.. 8];
@ -212,7 +228,7 @@ static inline int set_irq_level(int level)
return oldlevel; return oldlevel;
} }
static inline unsigned short SWAB16(unsigned short value) static inline unsigned short swap16(unsigned short value)
/* /*
result[15..8] = value[ 7..0]; result[15..8] = value[ 7..0];
result[ 7..0] = value[15..8]; result[ 7..0] = value[15..8];
@ -231,7 +247,7 @@ static inline unsigned long SWAW32(unsigned long value)
return value; return value;
} }
static inline unsigned long SWAB32(unsigned long value) static inline unsigned long swap32(unsigned long value)
/* /*
result[31..24] = value[ 7.. 0]; result[31..24] = value[ 7.. 0];
result[23..16] = value[15.. 8]; result[23..16] = value[15.. 8];
@ -299,7 +315,7 @@ static inline int set_irq_level(int level)
return result; return result;
} }
static inline unsigned short SWAB16(unsigned short value) static inline unsigned short swap16(unsigned short value)
/* /*
result[15..8] = value[ 7..0]; result[15..8] = value[ 7..0];
result[ 7..0] = value[15..8]; result[ 7..0] = value[15..8];
@ -308,7 +324,7 @@ static inline unsigned short SWAB16(unsigned short value)
return (value >> 8) | (value << 8); return (value >> 8) | (value << 8);
} }
static inline unsigned long SWAB32(unsigned long value) static inline unsigned long swap32(unsigned long value)
/* /*
result[31..24] = value[ 7.. 0]; result[31..24] = value[ 7.. 0];
result[23..16] = value[15.. 8]; result[23..16] = value[15.. 8];
@ -316,8 +332,8 @@ static inline unsigned long SWAB32(unsigned long value)
result[ 7.. 0] = value[31..24]; result[ 7.. 0] = value[31..24];
*/ */
{ {
unsigned long hi = SWAB16(value >> 16); unsigned long hi = swap16(value >> 16);
unsigned long lo = SWAB16(value & 0xffff); unsigned long lo = swap16(value & 0xffff);
return (lo << 16) | hi; return (lo << 16) | hi;
} }
@ -338,6 +354,29 @@ static inline unsigned long SWAB32(unsigned long value)
#endif #endif
#else #else
static inline unsigned short swap16(unsigned short value)
/*
result[15..8] = value[ 7..0];
result[ 7..0] = value[15..8];
*/
{
return (value >> 8) | (value << 8);
}
static inline unsigned long swap32(unsigned long value)
/*
result[31..24] = value[ 7.. 0];
result[23..16] = value[15.. 8];
result[15.. 8] = value[23..16];
result[ 7.. 0] = value[31..24];
*/
{
unsigned long hi = swap16(value >> 16);
unsigned long lo = swap16(value & 0xffff);
return (lo << 16) | hi;
}
#define invalidate_icache() #define invalidate_icache()
#endif #endif

View file

@ -354,7 +354,7 @@ void pcmrec_callback(bool flush)
for (i=0; i<EACH_BUFFER_SIZE * num_ready / 4; i++) for (i=0; i<EACH_BUFFER_SIZE * num_ready / 4; i++)
{ {
*ptr = SWAB32(*ptr); *ptr = htole32(*ptr);
ptr++; ptr++;
} }
@ -476,11 +476,11 @@ static void close_wave(void)
{ {
long l; long l;
l = SWAB32(num_rec_bytes + 36); l = htole32(num_rec_bytes + 36);
lseek(wav_file, 4, SEEK_SET); lseek(wav_file, 4, SEEK_SET);
write(wav_file, &l, 4); write(wav_file, &l, 4);
l = SWAB32(num_rec_bytes); l = htole32(num_rec_bytes);
lseek(wav_file, 40, SEEK_SET); lseek(wav_file, 40, SEEK_SET);
write(wav_file, &l, 4); write(wav_file, &l, 4);