位对齐访问任何位长度字节对齐的缓冲区

时间:2012-08-28 13:09:34

标签: c++ c

对于嵌入式系统(little Endian ARM,语言C - 即使我在这里用于测试目的C ++),我编写了附带的代码片段。我必须发送和接收不同的配置数据,位对齐和给定的位长度。对于我必须发送/接收的每个变量,使用field_conf_t执行配置,如:

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
};

我遇到的问题是我对单个Bit-Ops的考虑很多,测试失败了。

这里是代码:

#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();

和测试运行输出

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"

缓冲区大小始终为8个字节;我很高兴我能在这里使用64位长;在我对字节对齐的操作失败之前(每个字节上的掩码和Bit-Ops)。注意,该值不能大于int32 / uint32。我也知道C位域。它们太慢了,我需要为所使用的嵌入式系统提供快速解决方案(其他耗时的任务都在那里)。

我也对使用lsb / msb 32位甚至8字节切片快速解决方案的解决方案感兴趣,但我认为由于编译器,使用64位数据类型的性能更高。

2 个答案:

答案 0 :(得分:0)

您的第一个代码块中是否有任何问题:

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
};

但是稍后在你的申请中你有:

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;
};

这是一个重要的差异吗?也许没什么,但我注意到了......以为我会指出它。

答案 1 :(得分:0)

问题在于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是一个32位值,因此在field->offset + field->length >= 32时溢出边缘。将mask更改为64位应该可以将位移作为64位操作执行:

const uint64 mask = (1 << field->length) - 1;