Alineación de bits Acceso a cualquier búfer alineado de bytes de longitud de bits
Frecuentes
Visto 472 veces
0
For an embedded System (little Endian ARM, language C - even if I use here for test purpose C++) I wrote the attached piece of code. I have to transmit and receive different configured data, bit aligned and of a given bit length. The configuration is performed using field_conf_t for each variable I have to send/receive like:
typedef struct field_conf_t { ... }; // see below
typedef struct {
field_conf_t a;
field_conf_t b;
field_conf_t c;
} tx_fields_conf_t;
const tx_fields_conf_t tx_fields_conf = {
{ 0, 8 }, // a - offset/length
{ 28, 12 }, // b - offset/length
{ 56, 8 } // c - offset/length
};
The problem I have is that I think to much about single Bit-Ops, the tests fail.
Aquí el código:
#include <stdint.h>
#define BOOST_TEST_MODULE BitFieldTest
#include <boost/test/unit_test.hpp>
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef unsigned long long uint64;
typedef struct {
uint8 offset; // [0-63]
uint8 length; // [1-32]
} field_conf_t;
typedef struct {
field_conf_t a;
field_conf_t b;
field_conf_t c;
} tx_fields_conf_t;
typedef union {
uint8 buf[8];
uint64 contents;
} msg_t;
void set_value(msg_t* const msg, const field_conf_t* const field, uint32 value)
{
const uint32 mask = (1 << field->length) - 1;
msg->contents &= ~(mask << field->offset); // clear old contens
msg->contents |= (value & mask) << field->offset; // set new contens
}
uint32 get_value(const msg_t* const msg, const field_conf_t* const field)
{
const uint32 mask = (1 << field->length) - 1;
uint64 value = (msg->contents >> field->offset);
value &= mask;
return (uint32)value;
}
// ########################################################################
struct TestFixture {
TestFixture() : a(0xAA), b(0xBBB), c(0xCC) {
conf.a.offset = 0; conf.a.length = 8;
conf.b.offset = 25; conf.b.length = 12;
conf.c.offset = 56; conf.a.length = 8;
}
uint32 a, b, c;
msg_t msg;
tx_fields_conf_t conf;
};
BOOST_FIXTURE_TEST_SUITE(MsgBitfieldTest, TestFixture);
BOOST_AUTO_TEST_CASE(Test_01)
{
set_value(&msg, &conf.a, a);
BOOST_CHECK(get_value(&msg, &conf.a) == a);
set_value(&msg, &conf.b, b);
BOOST_CHECK(get_value(&msg, &conf.a) == a);
BOOST_CHECK(get_value(&msg, &conf.b) == b);
set_value(&msg, &conf.c, c);
BOOST_CHECK(get_value(&msg, &conf.a) == a);
BOOST_CHECK(get_value(&msg, &conf.b) == b);
BOOST_CHECK(get_value(&msg, &conf.c) == c);
}
BOOST_AUTO_TEST_SUITE_END();
and the test run output
Running 1 test case...
d:/work/bugee/test/can_msg/msg_bitsfields/msg_bitsfields/main.cpp(65): error in
"Test_01": check get_value(&msg, &conf.b) == b failed
d:/work/bugee/test/can_msg/msg_bitsfields/msg_bitsfields/main.cpp(69): error in
"Test_01": check get_value(&msg, &conf.b) == b failed
d:/work/bugee/test/can_msg/msg_bitsfields/msg_bitsfields/main.cpp(70): error in
"Test_01": check get_value(&msg, &conf.c) == c failed
*** 3 failures detected in test suite "BitFieldTest"
The buffer size is always 8 bytes; I'm happy that I can use 64-bit long long here; before I failed on byte aligned ops also (mask and Bit-Ops on each of the bytes). Note, that the value can not be greater than int32 /uint32. Also I know about C bitfields. They are too slow and I need a fast solution for the Embedded System used (other time consuming tasks are there).
I'm also interested in a solution with lsb/msb 32bit or even 8-byte slice fast solution but I would assume the use of 64 bit datatype is even more performant due to the compiler.
2 Respuestas
0
Is there any problem with the fact that in your first code block you have:
const tx_fields_conf_t tx_fields_conf = {
{ 0, 8 }, // a - offset/length
{ 28, 12 }, // b - offset/length -- note the 28 here as the offset
{ 56, 8 } // c - offset/length
};
But later in your application you have:
struct TestFixture {
TestFixture() : a(0xAA), b(0xBBB), c(0xCC) {
conf.a.offset = 0; conf.a.length = 8;
conf.b.offset = 25; conf.b.length = 12; // offset is *25* here, not 28 as above. Poss. problem?
conf.c.offset = 56; conf.a.length = 8;
}
uint32 a, b, c;
msg_t msg;
tx_fields_conf_t conf;
};
Any chance that's an important discrepancy? Maybe nothing, but I did notice it...thought I'd point it out.
Respondido 28 ago 12, 14:08
0
El problema es que en set_value
const uint32 mask = (1 << field->length) - 1;
msg->contents &= ~(mask << field->offset); // clear old contens
msg->contents |= (value & mask) << field->offset; // set new contens
mask << field->offset
is a 32-bit value and so will be overflowed off the edge when field->offset + field->length >= 32
. Cambiando mask
to 64-bit should work to get the bit-shifts performed as 64-bit operations:
const uint64 mask = (1 << field->length) - 1;
Respondido 28 ago 12, 14:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c++ c or haz tu propia pregunta.
Thanks that you did point out this, but this doesn't matter. The structure 'API' I like to use in the final version. - user1630297