Strict sign conversions

This commit is contained in:
Pavel Kirienko
2014-08-28 02:51:29 +04:00
parent 4cc7dfb2d4
commit d9d6e80aea
23 changed files with 93 additions and 98 deletions
+18 -26
View File
@@ -22,45 +22,39 @@
namespace uavcan
{
void bitarrayCopy(const unsigned char* src_org, int src_offset, int src_len, unsigned char* dst_org, int dst_offset)
void bitarrayCopy(const unsigned char* src_org, unsigned src_offset, unsigned src_len,
unsigned char* dst_org, unsigned dst_offset)
{
static const unsigned char reverse_mask[] = { 0x55U, 0x80U, 0xC0U, 0xE0U, 0xF0U, 0xF8U, 0xFCU, 0xFEU, 0xFFU };
static const unsigned char reverse_mask_xor[] = { 0xFFU, 0x7FU, 0x3FU, 0x1FU, 0x0FU, 0x07U, 0x03U, 0x01U, 0x00U };
if (src_len > 0)
if (src_len > 0U)
{
const unsigned char* src;
unsigned char* dst;
int src_offset_modulo;
int dst_offset_modulo;
const unsigned char *src = src_org + (src_offset / CHAR_BIT);
unsigned char *dst = dst_org + (dst_offset / CHAR_BIT);
src = src_org + (src_offset / CHAR_BIT);
dst = dst_org + (dst_offset / CHAR_BIT);
src_offset_modulo = src_offset % CHAR_BIT;
dst_offset_modulo = dst_offset % CHAR_BIT;
const unsigned src_offset_modulo = src_offset % CHAR_BIT;
const unsigned dst_offset_modulo = dst_offset % CHAR_BIT;
if (src_offset_modulo == dst_offset_modulo)
{
int byte_len;
int src_len_modulo;
if (src_offset_modulo)
if (src_offset_modulo > 0U)
{
unsigned char c = reverse_mask_xor[dst_offset_modulo] & *src++;
PREPARE_FIRST_COPY();
*dst++ |= c;
}
byte_len = src_len / CHAR_BIT;
src_len_modulo = src_len % CHAR_BIT;
const unsigned byte_len = src_len / CHAR_BIT;
const unsigned src_len_modulo = src_len % CHAR_BIT;
if (byte_len)
if (byte_len > 0U)
{
(void)std::memcpy(dst, src, byte_len);
src += byte_len;
dst += byte_len;
}
if (src_len_modulo)
if (src_len_modulo > 0U)
{
*dst &= reverse_mask_xor[src_len_modulo];
*dst |= reverse_mask[src_len_modulo] & *src;
@@ -68,11 +62,9 @@ void bitarrayCopy(const unsigned char* src_org, int src_offset, int src_len, uns
}
else
{
int bit_diff_ls;
int bit_diff_rs;
int byte_len;
int src_len_modulo;
unsigned char c;
unsigned bit_diff_ls = 0U;
unsigned bit_diff_rs = 0U;
unsigned char c = 0U;
/*
* Begin: Line things up on destination.
*/
@@ -98,7 +90,7 @@ void bitarrayCopy(const unsigned char* src_org, int src_offset, int src_len, uns
/*
* Middle: copy with only shifting the source.
*/
byte_len = src_len / CHAR_BIT;
int byte_len = int(src_len / CHAR_BIT);
while (--byte_len >= 0)
{
@@ -110,8 +102,8 @@ void bitarrayCopy(const unsigned char* src_org, int src_offset, int src_len, uns
/*
* End: copy the remaing bits;
*/
src_len_modulo = src_len % CHAR_BIT;
if (src_len_modulo)
unsigned src_len_modulo = src_len % CHAR_BIT;
if (src_len_modulo > 0U)
{
c = static_cast<unsigned char>(*src++ << bit_diff_ls);
c = static_cast<unsigned char>(c | (*src >> bit_diff_rs));
+5 -5
View File
@@ -12,7 +12,7 @@ namespace uavcan
const unsigned BitStream::MaxBytesPerRW;
const unsigned BitStream::MaxBitsPerRW;
int BitStream::write(const uint8_t* bytes, const int bitlen)
int BitStream::write(const uint8_t* bytes, const unsigned bitlen)
{
// Temporary buffer is needed to merge new bits with cached unaligned bits from the last write() (see byte_cache_)
uint8_t tmp[MaxBytesPerRW + 1];
@@ -25,7 +25,7 @@ int BitStream::write(const uint8_t* bytes, const int bitlen)
fill(tmp, tmp + bytelen, uint8_t(0));
copyBitArray(bytes, 0, bitlen, tmp, bit_offset_ % 8);
const int new_bit_offset = bit_offset_ + bitlen;
const unsigned new_bit_offset = bit_offset_ + bitlen;
// Bitcopy algorithm resets skipped bits in the first byte. Restore them back.
tmp[0] |= byte_cache_;
@@ -52,7 +52,7 @@ int BitStream::write(const uint8_t* bytes, const int bitlen)
return ResultOk;
}
int BitStream::read(uint8_t* bytes, const int bitlen)
int BitStream::read(uint8_t* bytes, const unsigned bitlen)
{
uint8_t tmp[MaxBytesPerRW + 1];
@@ -81,10 +81,10 @@ std::string BitStream::toString() const
std::string out;
out.reserve(128);
for (int offset = 0; true; offset++)
for (unsigned offset = 0; true; offset++)
{
uint8_t byte = 0;
if (1 != buf_.read(offset, &byte, 1))
if (1U != buf_.read(offset, &byte, 1U))
{
break;
}
+2 -2
View File
@@ -51,7 +51,7 @@ uint16_t IEEE754Converter::nativeNonIeeeToHalf(float value)
hbits |= uint16_t((exp + 14) << 10);
}
const int32_t ival = static_cast<int32_t>(value);
hbits = uint16_t(hbits | (((ival < 0) ? (-ival) : ival) & 0x3FFU));
hbits = uint16_t(hbits | (uint32_t((ival < 0) ? (-ival) : ival) & 0x3FFU));
float diff = std::fabs(value - static_cast<float>(ival));
hbits = uint16_t(hbits + (diff >= 0.5F));
return hbits;
@@ -80,7 +80,7 @@ float IEEE754Converter::halfToNativeNonIeee(uint16_t value)
}
else if (abs > 0x3FFU)
{
out = std::ldexp(static_cast<float>((value & 0x3FFU) | 0x400U), (abs >> 10) - 25);
out = std::ldexp(static_cast<float>((value & 0x3FFU) | 0x400U), int(abs >> 10) - 25);
}
else
{
+1 -1
View File
@@ -10,7 +10,7 @@ namespace uavcan
void ScalarCodec::swapByteOrder(uint8_t* const bytes, const unsigned len)
{
UAVCAN_ASSERT(bytes);
for (int i = 0, j = len - 1; i < j; i++, j--)
for (unsigned i = 0, j = len - 1; i < j; i++, j--)
{
const uint8_t c = bytes[i];
bytes[i] = bytes[j];