Skip to content

Commit

Permalink
sampleconv.h: Use lrint() instead of lround().
Browse files Browse the repository at this point in the history
lrint() is faster. Also put parentheses around macro arguments.
  • Loading branch information
bmc0 committed Sep 8, 2024
1 parent 33b8de4 commit e43c6a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions sampleconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ void read_buf_s24_3(void *in, sample_t *out, ssize_t s)
int32_t v;
uint8_t *inn = (uint8_t *) in;
while (s-- > 0) {
v = (inn[s*3 + 0] & 0xff) << 0;
v |= (inn[s*3 + 1] & 0xff) << 8;
v |= (inn[s*3 + 2] & 0xff) << 16;
v = (int32_t) (inn[s*3 + 0] & 0xff) << 0;
v |= (int32_t) (inn[s*3 + 1] & 0xff) << 8;
v |= (int32_t) (inn[s*3 + 2] & 0xff) << 16;
out[s] = S24_TO_SAMPLE(v);
}
}
Expand Down
46 changes: 23 additions & 23 deletions sampleconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,38 @@
* this is true for all supported formats.
*/

#define S24_SIGN_EXTEND(x) ((x & 0x800000) ? x | ~0x7fffff : x)
#define S24_SIGN_EXTEND(x) (((x) & 0x800000) ? (x) | ~0x7fffff : (x))

#if BIT_PERFECT
/* these clip, but are bit perfect */
#define SAMPLE_TO_U8(x) ((uint8_t) ((x * 128.0 + 127.0 <= -0.5) ? 0 : lround(x * 128.0 + 127.0)))
#define U8_TO_SAMPLE(x) (((sample_t) x - 127.0) / 128.0)
#define SAMPLE_TO_S8(x) ((int8_t) ((x * 128.0 >= 127.5) ? 127 : lround(x * 128.0)))
#define S8_TO_SAMPLE(x) ((sample_t) x / 128.0)
#define SAMPLE_TO_S16(x) ((int16_t) ((x * 32768.0 >= 32767.5) ? 32767 : lround(x * 32768.0)))
#define S16_TO_SAMPLE(x) ((sample_t) x / 32768.0)
#define SAMPLE_TO_S24(x) ((int32_t) ((x * 8388608.0 >= 8388607.5) ? 8388607 : lround(x * 8388608.0)))
#define SAMPLE_TO_U8(x) ((uint8_t) (((x) * 128.0 + 127.0 < 0.0) ? 0 : lrint((x) * 128.0 + 127.0)))
#define U8_TO_SAMPLE(x) (((sample_t) (x) - 127.0) / 128.0)
#define SAMPLE_TO_S8(x) ((int8_t) (((x) * 128.0 > 127.0) ? 127 : lrint((x) * 128.0)))
#define S8_TO_SAMPLE(x) ((sample_t) (x) / 128.0)
#define SAMPLE_TO_S16(x) ((int16_t) (((x) * 32768.0 > 32767.0) ? 32767 : lrint((x) * 32768.0)))
#define S16_TO_SAMPLE(x) ((sample_t) (x) / 32768.0)
#define SAMPLE_TO_S24(x) ((int32_t) (((x) * 8388608.0 > 8388607.0) ? 8388607 : lrint((x) * 8388608.0)))
#define S24_TO_SAMPLE(x) ((sample_t) S24_SIGN_EXTEND(x) / 8388608.0)
#define SAMPLE_TO_S32(x) ((int32_t) ((x * 2147483648.0 >= 2147483647.5) ? 2147483647 : lround(x * 2147483648.0)))
#define S32_TO_SAMPLE(x) ((sample_t) x / 2147483648.0)
#define SAMPLE_TO_S32(x) ((int32_t) (((x) * 2147483648.0 > 2147483647.0) ? 2147483647 : lrint((x) * 2147483648.0)))
#define S32_TO_SAMPLE(x) ((sample_t) (x) / 2147483648.0)
#else
/* these do not clip, but are also not bit perfect */
#define SAMPLE_TO_U8(x) ((uint8_t) lround(x * 127.0 + 127.0))
#define U8_TO_SAMPLE(x) (((sample_t) x - 127.0) / 128.0)
#define SAMPLE_TO_S8(x) ((int8_t) lround(x * 127.0))
#define S8_TO_SAMPLE(x) ((sample_t) x / 128.0)
#define SAMPLE_TO_S16(x) ((int16_t) lround(x * 32767.0))
#define S16_TO_SAMPLE(x) ((sample_t) x / 32768.0)
#define SAMPLE_TO_S24(x) ((int32_t) lround(x * 8388607.0))
#define SAMPLE_TO_U8(x) ((uint8_t) lrint((x) * 127.0 + 127.0))
#define U8_TO_SAMPLE(x) (((sample_t) (x) - 127.0) / 128.0)
#define SAMPLE_TO_S8(x) ((int8_t) lrint((x) * 127.0))
#define S8_TO_SAMPLE(x) ((sample_t) (x) / 128.0)
#define SAMPLE_TO_S16(x) ((int16_t) lrint((x) * 32767.0))
#define S16_TO_SAMPLE(x) ((sample_t) (x) / 32768.0)
#define SAMPLE_TO_S24(x) ((int32_t) lrint((x) * 8388607.0))
#define S24_TO_SAMPLE(x) ((sample_t) S24_SIGN_EXTEND(x) / 8388608.0)
#define SAMPLE_TO_S32(x) ((int32_t) lround(x * 2147483647.0))
#define S32_TO_SAMPLE(x) ((sample_t) x / 2147483648.0)
#define SAMPLE_TO_S32(x) ((int32_t) lrint((x) * 2147483647.0))
#define S32_TO_SAMPLE(x) ((sample_t) (x) / 2147483648.0)
#endif

#define SAMPLE_TO_FLOAT(x) ((float) x)
#define FLOAT_TO_SAMPLE(x) ((sample_t) x)
#define SAMPLE_TO_DOUBLE(x) ((double) x)
#define DOUBLE_TO_SAMPLE(x) ((sample_t) x)
#define SAMPLE_TO_FLOAT(x) ((float) (x))
#define FLOAT_TO_SAMPLE(x) ((sample_t) (x))
#define SAMPLE_TO_DOUBLE(x) ((double) (x))
#define DOUBLE_TO_SAMPLE(x) ((sample_t) (x))

void write_buf_u8(sample_t *, void *, ssize_t);
void read_buf_u8(void *, sample_t *, ssize_t);
Expand Down

0 comments on commit e43c6a5

Please sign in to comment.