Dawn Framework 1.0
Universal data acquisition framework for embedded systems
bitwise.hxx
1// dawn/src/prog/bitwise.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <cstddef>
9#include <cstdint>
10
11#include "dawn/io/common.hxx"
12#include "dawn/io/ddata.hxx"
13
14namespace dawn
15{
16namespace prog
17{
18inline bool isBitwiseDtype(uint8_t dtype)
19{
20 return dtype != SObjectId::DTYPE_ANY && dtype != SObjectId::DTYPE_CHAR &&
22}
23
24inline size_t getLogicalElementBits(uint8_t dtype, size_t storageSize)
25{
26 return dtype == SObjectId::DTYPE_BOOL ? 1 : storageSize * 8;
27}
28
29inline size_t getLogicalBits(uint8_t dtype, size_t storageSize, size_t dim)
30{
31 return dim * getLogicalElementBits(dtype, storageSize);
32}
33
34inline size_t getLogicalBits(const CIOCommon *io)
35{
36 return getLogicalBits(io->getDtype(), io->getDtypeSize(), io->getDataDim());
37}
38
39inline size_t getLogicalBits(io_ddata_t *data)
40{
41 return getLogicalBits(data->getDtype(), data->getSize(), data->getItems());
42}
43
44inline bool readLogicalBit(uint8_t dtype, size_t storageSize, const void *data, size_t bitIndex)
45{
46 const size_t elementBits = getLogicalElementBits(dtype, storageSize);
47 const size_t element = bitIndex / elementBits;
48 const size_t bitInElem = bitIndex % elementBits;
49 const uint8_t *ptr = static_cast<const uint8_t *>(data) + element * storageSize;
50
51 if (dtype == SObjectId::DTYPE_BOOL)
52 {
53 return ptr[0] != 0;
54 }
55
56 return ((ptr[bitInElem / 8] >> (bitInElem % 8)) & 1u) != 0;
57}
58
59inline bool readLogicalBit(const CIOCommon *io, const void *data, size_t bitIndex)
60{
61 return readLogicalBit(io->getDtype(), io->getDtypeSize(), data, bitIndex);
62}
63
64inline void writeLogicalBit(uint8_t dtype,
65 size_t storageSize,
66 void *data,
67 size_t bitIndex,
68 bool value)
69{
70 const size_t elementBits = getLogicalElementBits(dtype, storageSize);
71 const size_t element = bitIndex / elementBits;
72 const size_t bitInElem = bitIndex % elementBits;
73 uint8_t *ptr = static_cast<uint8_t *>(data) + element * storageSize;
74
75 if (dtype == SObjectId::DTYPE_BOOL)
76 {
77 ptr[0] = value ? 1 : 0;
78 return;
79 }
80
81 if (value)
82 {
83 ptr[bitInElem / 8] |= static_cast<uint8_t>(1u << (bitInElem % 8));
84 }
85}
86
87inline void writeLogicalBit(CIOCommon *io, void *data, size_t bitIndex, bool value)
88{
89 writeLogicalBit(io->getDtype(), io->getDtypeSize(), data, bitIndex, value);
90}
91} // namespace prog
92} // namespace dawn
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
@ DTYPE_BLOCK
Opaque block/byte-stream data type.
Definition objectid.hxx:153
@ DTYPE_CHAR
Character/string type (null-terminated, 4-byte aligned).
Definition objectid.hxx:144
@ DTYPE_BOOL
Boolean data type (stored in 32-bit container).
Definition objectid.hxx:72