Skip to content

Commit 787fb98

Browse files
committed
Calculate shift for int24 sign propagation inline
in theory this could be extrapolated to read arbitrary size integers (e.g. 5, 6, or 7 bytes), but we'd need to figure out a better way to deal with the byte order flipping
1 parent 5e3bcb8 commit 787fb98

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

encoding.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static inline bool readFixedSizeType(unsigned char* bytes, size_t used, size_t&
117117
return true;
118118
}
119119

120-
template<typename TValue, ByteOrder byteOrder, unsigned int signedShift>
120+
template<typename TValue, ByteOrder byteOrder>
121121
static inline bool readInt24(unsigned char* bytes, size_t used, size_t& offset, TValue& result) {
122122
const size_t SIZE = 3;
123123

@@ -137,8 +137,9 @@ static inline bool readInt24(unsigned char* bytes, size_t used, size_t& offset,
137137
result |= bytes[offset] << 16;
138138
}
139139

140-
if (signedShift > 0) {
141-
result = (result << signedShift) >> signedShift;
140+
const size_t SIGNED_SHIFT = (sizeof(TValue) - SIZE) * CHAR_BIT;
141+
if (SIGNED_SHIFT > 0) {
142+
result = (result << SIGNED_SHIFT) >> SIGNED_SHIFT;
142143
}
143144

144145
offset += SIZE;
@@ -685,16 +686,16 @@ PHP_RINIT_FUNCTION(encoding)
685686
ZEND_RAW_FENTRY("read" zend_name, (zif_readType<native_type, readByte<native_type>, zval_long_wrapper>), arginfo_read_integer, ZEND_ACC_PUBLIC) \
686687
ZEND_RAW_FENTRY("write" zend_name, (zif_writeType<native_type, zend_parse_parameters_long_wrapper<native_type>, writeByte<native_type>>), arginfo_write_integer, ZEND_ACC_PUBLIC)
687688

688-
#define READ_TRIAD_ENTRY(zend_name, native_type, shift) \
689-
ZEND_RAW_FENTRY("read" zend_name "BE", (zif_readType<native_type, readInt24<native_type, ByteOrder::BigEndian, shift>, zval_long_wrapper>), arginfo_read_integer, ZEND_ACC_PUBLIC) \
690-
ZEND_RAW_FENTRY("read" zend_name "LE", (zif_readType<native_type, readInt24<native_type, ByteOrder::LittleEndian, shift>, zval_long_wrapper>), arginfo_read_integer, ZEND_ACC_PUBLIC)
689+
#define READ_TRIAD_ENTRY(zend_name, native_type) \
690+
ZEND_RAW_FENTRY("read" zend_name "BE", (zif_readType<native_type, readInt24<native_type, ByteOrder::BigEndian>, zval_long_wrapper>), arginfo_read_integer, ZEND_ACC_PUBLIC) \
691+
ZEND_RAW_FENTRY("read" zend_name "LE", (zif_readType<native_type, readInt24<native_type, ByteOrder::LittleEndian>, zval_long_wrapper>), arginfo_read_integer, ZEND_ACC_PUBLIC)
691692

692693
#define WRITE_TRIAD_ENTRY(zend_name, native_type) \
693694
ZEND_RAW_FENTRY("write" zend_name "BE", (zif_writeType<native_type, zend_parse_parameters_long_wrapper<native_type>, writeInt24<native_type, ByteOrder::BigEndian>>), arginfo_write_integer, ZEND_ACC_PUBLIC) \
694695
ZEND_RAW_FENTRY("write" zend_name "LE", (zif_writeType<native_type, zend_parse_parameters_long_wrapper<native_type>, writeInt24<native_type, ByteOrder::LittleEndian>>), arginfo_write_integer, ZEND_ACC_PUBLIC)
695696

696-
#define READ_WRITE_TRIAD_ENTRY(zend_name, native_type, readShift) \
697-
READ_TRIAD_ENTRY(zend_name, native_type, readShift) \
697+
#define READ_WRITE_TRIAD_ENTRY(zend_name, native_type) \
698+
READ_TRIAD_ENTRY(zend_name, native_type) \
698699
WRITE_TRIAD_ENTRY(zend_name, native_type)
699700

700701
static zend_function_entry byte_buffer_methods[] = {
@@ -713,8 +714,8 @@ static zend_function_entry byte_buffer_methods[] = {
713714
READ_WRITE_VARINT_ENTRY("Int", uint32_t, int32_t)
714715
READ_WRITE_VARINT_ENTRY("Long", uint64_t, int64_t)
715716

716-
READ_WRITE_TRIAD_ENTRY("UnsignedTriad", uint32_t, 0)
717-
READ_WRITE_TRIAD_ENTRY("SignedTriad", int32_t, 8)
717+
READ_WRITE_TRIAD_ENTRY("UnsignedTriad", uint32_t)
718+
READ_WRITE_TRIAD_ENTRY("SignedTriad", int32_t)
718719

719720
PHP_ME(ByteBuffer, __construct, ByteBuffer___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
720721
PHP_ME(ByteBuffer, toString, ByteBuffer_toString, ZEND_ACC_PUBLIC)

0 commit comments

Comments
 (0)