Skip to content

Commit e43c6a5

Browse files
committed
sampleconv.h: Use lrint() instead of lround().
lrint() is faster. Also put parentheses around macro arguments.
1 parent 33b8de4 commit e43c6a5

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

sampleconv.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ void read_buf_s24_3(void *in, sample_t *out, ssize_t s)
111111
int32_t v;
112112
uint8_t *inn = (uint8_t *) in;
113113
while (s-- > 0) {
114-
v = (inn[s*3 + 0] & 0xff) << 0;
115-
v |= (inn[s*3 + 1] & 0xff) << 8;
116-
v |= (inn[s*3 + 2] & 0xff) << 16;
114+
v = (int32_t) (inn[s*3 + 0] & 0xff) << 0;
115+
v |= (int32_t) (inn[s*3 + 1] & 0xff) << 8;
116+
v |= (int32_t) (inn[s*3 + 2] & 0xff) << 16;
117117
out[s] = S24_TO_SAMPLE(v);
118118
}
119119
}

sampleconv.h

+23-23
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,38 @@
3030
* this is true for all supported formats.
3131
*/
3232

33-
#define S24_SIGN_EXTEND(x) ((x & 0x800000) ? x | ~0x7fffff : x)
33+
#define S24_SIGN_EXTEND(x) (((x) & 0x800000) ? (x) | ~0x7fffff : (x))
3434

3535
#if BIT_PERFECT
3636
/* these clip, but are bit perfect */
37-
#define SAMPLE_TO_U8(x) ((uint8_t) ((x * 128.0 + 127.0 <= -0.5) ? 0 : lround(x * 128.0 + 127.0)))
38-
#define U8_TO_SAMPLE(x) (((sample_t) x - 127.0) / 128.0)
39-
#define SAMPLE_TO_S8(x) ((int8_t) ((x * 128.0 >= 127.5) ? 127 : lround(x * 128.0)))
40-
#define S8_TO_SAMPLE(x) ((sample_t) x / 128.0)
41-
#define SAMPLE_TO_S16(x) ((int16_t) ((x * 32768.0 >= 32767.5) ? 32767 : lround(x * 32768.0)))
42-
#define S16_TO_SAMPLE(x) ((sample_t) x / 32768.0)
43-
#define SAMPLE_TO_S24(x) ((int32_t) ((x * 8388608.0 >= 8388607.5) ? 8388607 : lround(x * 8388608.0)))
37+
#define SAMPLE_TO_U8(x) ((uint8_t) (((x) * 128.0 + 127.0 < 0.0) ? 0 : lrint((x) * 128.0 + 127.0)))
38+
#define U8_TO_SAMPLE(x) (((sample_t) (x) - 127.0) / 128.0)
39+
#define SAMPLE_TO_S8(x) ((int8_t) (((x) * 128.0 > 127.0) ? 127 : lrint((x) * 128.0)))
40+
#define S8_TO_SAMPLE(x) ((sample_t) (x) / 128.0)
41+
#define SAMPLE_TO_S16(x) ((int16_t) (((x) * 32768.0 > 32767.0) ? 32767 : lrint((x) * 32768.0)))
42+
#define S16_TO_SAMPLE(x) ((sample_t) (x) / 32768.0)
43+
#define SAMPLE_TO_S24(x) ((int32_t) (((x) * 8388608.0 > 8388607.0) ? 8388607 : lrint((x) * 8388608.0)))
4444
#define S24_TO_SAMPLE(x) ((sample_t) S24_SIGN_EXTEND(x) / 8388608.0)
45-
#define SAMPLE_TO_S32(x) ((int32_t) ((x * 2147483648.0 >= 2147483647.5) ? 2147483647 : lround(x * 2147483648.0)))
46-
#define S32_TO_SAMPLE(x) ((sample_t) x / 2147483648.0)
45+
#define SAMPLE_TO_S32(x) ((int32_t) (((x) * 2147483648.0 > 2147483647.0) ? 2147483647 : lrint((x) * 2147483648.0)))
46+
#define S32_TO_SAMPLE(x) ((sample_t) (x) / 2147483648.0)
4747
#else
4848
/* these do not clip, but are also not bit perfect */
49-
#define SAMPLE_TO_U8(x) ((uint8_t) lround(x * 127.0 + 127.0))
50-
#define U8_TO_SAMPLE(x) (((sample_t) x - 127.0) / 128.0)
51-
#define SAMPLE_TO_S8(x) ((int8_t) lround(x * 127.0))
52-
#define S8_TO_SAMPLE(x) ((sample_t) x / 128.0)
53-
#define SAMPLE_TO_S16(x) ((int16_t) lround(x * 32767.0))
54-
#define S16_TO_SAMPLE(x) ((sample_t) x / 32768.0)
55-
#define SAMPLE_TO_S24(x) ((int32_t) lround(x * 8388607.0))
49+
#define SAMPLE_TO_U8(x) ((uint8_t) lrint((x) * 127.0 + 127.0))
50+
#define U8_TO_SAMPLE(x) (((sample_t) (x) - 127.0) / 128.0)
51+
#define SAMPLE_TO_S8(x) ((int8_t) lrint((x) * 127.0))
52+
#define S8_TO_SAMPLE(x) ((sample_t) (x) / 128.0)
53+
#define SAMPLE_TO_S16(x) ((int16_t) lrint((x) * 32767.0))
54+
#define S16_TO_SAMPLE(x) ((sample_t) (x) / 32768.0)
55+
#define SAMPLE_TO_S24(x) ((int32_t) lrint((x) * 8388607.0))
5656
#define S24_TO_SAMPLE(x) ((sample_t) S24_SIGN_EXTEND(x) / 8388608.0)
57-
#define SAMPLE_TO_S32(x) ((int32_t) lround(x * 2147483647.0))
58-
#define S32_TO_SAMPLE(x) ((sample_t) x / 2147483648.0)
57+
#define SAMPLE_TO_S32(x) ((int32_t) lrint((x) * 2147483647.0))
58+
#define S32_TO_SAMPLE(x) ((sample_t) (x) / 2147483648.0)
5959
#endif
6060

61-
#define SAMPLE_TO_FLOAT(x) ((float) x)
62-
#define FLOAT_TO_SAMPLE(x) ((sample_t) x)
63-
#define SAMPLE_TO_DOUBLE(x) ((double) x)
64-
#define DOUBLE_TO_SAMPLE(x) ((sample_t) x)
61+
#define SAMPLE_TO_FLOAT(x) ((float) (x))
62+
#define FLOAT_TO_SAMPLE(x) ((sample_t) (x))
63+
#define SAMPLE_TO_DOUBLE(x) ((double) (x))
64+
#define DOUBLE_TO_SAMPLE(x) ((sample_t) (x))
6565

6666
void write_buf_u8(sample_t *, void *, ssize_t);
6767
void read_buf_u8(void *, sample_t *, ssize_t);

0 commit comments

Comments
 (0)