amiga_imgv
annotate src/endian.c @ 7:4c36d0f44aa6
lbm loading
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 29 Oct 2017 13:21:11 +0200 |
parents | |
children |
rev | line source |
---|---|
nuclear@7 | 1 #include "endian.h" |
nuclear@7 | 2 |
nuclear@7 | 3 #ifdef NO_INET |
nuclear@7 | 4 static uint32_t init_once32(uint32_t x); |
nuclear@7 | 5 static uint16_t init_once16(uint16_t x); |
nuclear@7 | 6 |
nuclear@7 | 7 static uint16_t swap16(uint16_t x); |
nuclear@7 | 8 static uint32_t swap32(uint32_t x); |
nuclear@7 | 9 static uint16_t nop16(uint16_t x); |
nuclear@7 | 10 static uint32_t nop32(uint32_t x); |
nuclear@7 | 11 |
nuclear@7 | 12 uint16_t (*ntohs)(uint16_t) = init_once16; |
nuclear@7 | 13 uint16_t (*htons)(uint16_t) = init_once16; |
nuclear@7 | 14 uint32_t (*ntohl)(uint32_t) = init_once32; |
nuclear@7 | 15 uint32_t (*htonl)(uint32_t) = init_once32; |
nuclear@7 | 16 |
nuclear@7 | 17 static uint16_t init_once16(uint16_t x) |
nuclear@7 | 18 { |
nuclear@7 | 19 int be = bigendian(); |
nuclear@7 | 20 ntohs = be ? nop16 : swap16; |
nuclear@7 | 21 htons = be ? nop16 : swap16; |
nuclear@7 | 22 return ntohs(x); |
nuclear@7 | 23 } |
nuclear@7 | 24 |
nuclear@7 | 25 static uint32_t init_once32(uint32_t x) |
nuclear@7 | 26 { |
nuclear@7 | 27 int be = bigendian(); |
nuclear@7 | 28 ntohl = be ? nop32 : swap32; |
nuclear@7 | 29 htonl = be ? nop32 : swap32; |
nuclear@7 | 30 return ntohl(x); |
nuclear@7 | 31 } |
nuclear@7 | 32 |
nuclear@7 | 33 static uint16_t swap16(uint16_t x) |
nuclear@7 | 34 { |
nuclear@7 | 35 return (x << 8) | (x >> 8); |
nuclear@7 | 36 } |
nuclear@7 | 37 |
nuclear@7 | 38 static uint32_t swap32(uint32_t x) |
nuclear@7 | 39 { |
nuclear@7 | 40 return (x << 24) | (x >> 24) | ((x << 16) & 0xff0000) | ((x >> 16) & 0xff00); |
nuclear@7 | 41 } |
nuclear@7 | 42 |
nuclear@7 | 43 static uint16_t nop16(uint16_t x) |
nuclear@7 | 44 { |
nuclear@7 | 45 return x; |
nuclear@7 | 46 } |
nuclear@7 | 47 |
nuclear@7 | 48 static uint32_t nop32(uint32_t x) |
nuclear@7 | 49 { |
nuclear@7 | 50 return x; |
nuclear@7 | 51 } |
nuclear@7 | 52 #endif |
nuclear@7 | 53 |
nuclear@7 | 54 int bigendian(void) |
nuclear@7 | 55 { |
nuclear@7 | 56 static const uint16_t x = 0xaabb; |
nuclear@7 | 57 return *(unsigned char*)&x == 0xaa; |
nuclear@7 | 58 } |