aboutsummaryrefslogtreecommitdiffstats
path: root/libs/bitfield-c/src/canutil/write.h
blob: 7f653b98ee66ed428e0d1d371d36924ed808003b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef __WRITE_H__
#define __WRITE_H__

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Public: Encode a floating point number into a fixed point, fixed bit width
 *      field in a bit array.
 *
 * value - the floating point value to encode.
 * bit_offset - the starting point for the encoded bits in the returned value.
 * bit_size - The max width of the field in the resulting bit array. If bit_size
 *      isn't big enough to store the fixed point version of the value, the
 *      bitfeld will *not* be set. TODO some error reporting would be nice.
 * factor - a factor used to transform from floating to fixed point before
 *      encoding. Use 1.0 for no factor.
 * offset - an offset used to transform from floating to fixed point before
 *      encoding. Use 0 for no offset.
 *
 * Returns a big-endian uint64_t with the value encoded as a bitfield.
 */
uint64_t eightbyte_encode_float(float value, unsigned int bit_offset,
        unsigned int bit_size, float factor, float offset);

uint64_t float_to_fixed_point(const float value, const float factor,
        const float offset);

bool bitfield_encode_float(const float value, const unsigned int bit_offset,
        const unsigned int bit_size, const float factor, const float offset,
        uint8_t destination[], const unsigned int destination_length);

/* Public: Encode a boolean into fixed bit width field in a bit array.
 *
 * value - the boolean value to encode - true will be 1, false will be 0.
 * bit_offset - the starting point for the encoded bits in the returned value.
 * bit_size - The max width of the field in the resulting bit array. If bit_size
 *      isn't big enough to store the fixed point version of the value, the
 *      bitfeld will *not* be set. TODO some error reporting would be nice.
 *
 * Returns a big-endian uint64_t with the value encoded as a bitfield.
 */
uint64_t eightbyte_encode_bool(const bool value, const unsigned int bit_offset,
        const unsigned int bit_size);

bool bitfield_encode_bool(const bool value, const unsigned int bit_offset, const
        unsigned int bit_size, uint8_t destination[],
        const unsigned int destination_length);

#ifdef __cplusplus
}
#endif

#endif // __WRITE_H__