Extra helpers

This commit is contained in:
bnnm 2021-01-23 15:49:29 +01:00
parent 4635fe2011
commit 64c95f6842
2 changed files with 21 additions and 4 deletions

View File

@ -308,14 +308,18 @@ static inline int min_s32(int32_t a, int32_t b) { return a < b ? a : b; }
#endif
/* fastest to compare would be read_u32x == (uint32), but should be pre-optimized (see get_id32x) */
static inline const int is_id32be(off_t offset, STREAMFILE* sf, const char* s) {
static inline /*const*/ int is_id32be(off_t offset, STREAMFILE* sf, const char* s) {
return read_u32be(offset, sf) == get_id32be(s);
}
static inline const int is_id32le(off_t offset, STREAMFILE* sf, const char* s) {
static inline /*const*/ int is_id32le(off_t offset, STREAMFILE* sf, const char* s) {
return read_u32le(offset, sf) == get_id32be(s);
}
static inline /*const*/ int is_id64be(off_t offset, STREAMFILE* sf, const char* s) {
return read_u64be(offset, sf) == get_id64be(s);
}
//TODO: maybe move to streamfile.c
/* guess byte endianness from a given value, return true if big endian and false if little endian */

View File

@ -101,14 +101,27 @@ static inline int clamp16(int32_t val) {
/* transforms a string to uint32 (for comparison), but if this is static + all goes well
* compiler should pre-calculate and use uint32 directly */
static inline const uint32_t get_id32be(const char* s) {
static inline /*const*/ uint32_t get_id32be(const char* s) {
return (uint32_t)(s[0] << 24) | (s[1] << 16) | (s[2] << 8) | (s[3] << 0);
}
//static inline const uint32_t get_id32le(const char* s) {
//static inline /*const*/ uint32_t get_id32le(const char* s) {
// return (uint32_t)(s[0] << 0) | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
//}
static inline /*const*/ uint64_t get_id64be(const char* s) {
return (uint64_t)(
((uint64_t)s[0] << 56) |
((uint64_t)s[1] << 48) |
((uint64_t)s[2] << 40) |
((uint64_t)s[3] << 32) |
((uint64_t)s[4] << 24) |
((uint64_t)s[5] << 16) |
((uint64_t)s[6] << 8) |
((uint64_t)s[7] << 0)
);
}
/* less common functions, no need to inline */