Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- typedef struct {
- uint8_t number, category, version, data;
- } value_t;
- static void test0(uint32_t *, uint32_t *, size_t, size_t);
- static void set_value(value_t *, uint32_t);
- static bool check_number_lt(value_t *, value_t *);
- static bool check_number(value_t *, value_t *);
- static bool check_category(value_t *, value_t *);
- static bool check_version(value_t *, value_t *);
- static void test1(uint32_t *, uint32_t *, size_t, size_t);
- int main(void) {
- uint32_t a[] = { 0x11223344, };
- uint32_t b[] = { 0x10000000, 0x11000000, 0x11023344, 0x11223344, 0x12000000, };
- test0(a, b, sizeof(a) / sizeof(uint32_t), sizeof(b) / sizeof(uint32_t));
- test1(a, b, sizeof(a) / sizeof(uint32_t), sizeof(b) / sizeof(uint32_t));
- }
- static void test0(uint32_t *a, uint32_t *b, size_t a_size, size_t b_size) {
- for (size_t i = 0; i < a_size; ++i) {
- value_t va; set_value(&va, a[i]);
- for (size_t j = 0; j < b_size; ++j) {
- value_t vb; set_value(&vb, b[j]);
- if (check_number_lt(&va, &vb)) {
- break;
- }
- if (check_number(&va, &vb)) {
- printf("[%d] A=%x, B=%x\n", va.number, va.data, vb.data);
- }
- }
- }
- }
- static void set_value(value_t *v, uint32_t value) {
- v->number = (value >> 24) & 0xff;
- v->category = (value >> 16) & 0xff;
- v->version = (value >> 8) & 0xff;
- v->data = value & 0xff;
- }
- static bool check_number(value_t *va, value_t *vb) {
- if (va->number != vb->number) {
- return false;
- }
- return check_category(va, vb);
- }
- static bool check_number_lt(value_t *va, value_t *vb) {
- return (va->number < vb->number) ? true : false;
- }
- static bool check_category(value_t *va, value_t *vb) {
- if (vb->category != 1 && vb->category != 2) {
- return false;
- }
- return check_version(va, vb);
- }
- static bool check_version(value_t *va, value_t *vb) {
- return (va->version == vb->version) ? true : false;
- }
- #define MASK_NUMBER (0xff000000)
- #define MASK_CATEGORY (0xff0000)
- #define MASK_CATEGORY1 (0x10000)
- #define MASK_CATEGORY2 (0x20000)
- #define MASK_VERSION (0xff00)
- #define MASK_DATA (0xff)
- static void test1(uint32_t *a, uint32_t *b, size_t a_size, size_t b_size) {
- for (size_t i = 0; i < a_size; ++i) {
- for (size_t j = 0; j < b_size; ++j) {
- if (
- !((a[i] & MASK_NUMBER) ^ (b[j] & MASK_NUMBER)) &&
- (
- !((b[j] & MASK_CATEGORY) ^ MASK_CATEGORY1) ||
- !((b[j] & MASK_CATEGORY) ^ MASK_CATEGORY2)
- ) &&
- !((a[i] & MASK_VERSION) ^ (b[j] & MASK_VERSION))
- ) {
- printf(
- "[%d] A=%x, B=%x\n",
- (a[i] & MASK_NUMBER) >> 24,
- a[i] & MASK_DATA,
- b[j] & MASK_DATA
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement