Grey  0.1.0
Simple Grey codes a.k.a. Reflected Binary Codes library.
grey.h
Go to the documentation of this file.
1 
19 #ifndef GREY_H
20 #define GREY_H
21 
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
30 #define GREY_VERSION "1.0.0"
31 
32 #include <stdint.h>
33 #include <inttypes.h>
34 
52 #ifndef GREY_UINTBITS
53 #define GREY_UINTBITS 64
54 #endif
55 
100 #if (GREY_UINTBITS == 64)
101 typedef uint64_t grey_code_t;
102 #define GREY_MAX ((uint64_t) UINT64_MAX)
103 #define GREY_FMT PRIu64
104 #define GREY_FMTx PRIx64
105 #define GREY_FMTX PRIX64
106 #elif (GREY_UINTBITS == 32)
107 typedef uint32_t grey_code_t;
108 #define GREY_MAX ((uint32_t) UINT32_MAX)
109 #define GREY_FMT PRIu32
110 #define GREY_FMTx PRIx32
111 #define GREY_FMTX PRIX32
112 #elif (GREY_UINTBITS == 16)
113 typedef uint16_t grey_code_t;
114 #define GREY_MAX ((uint16_t) UINT16_MAX)
115 #define GREY_FMT PRIu16
116 #define GREY_FMTx PRIx16
117 #define GREY_FMTX PRIX16
118 #elif (GREY_UINTBITS == 8)
119 typedef uint8_t grey_code_t;
120 #define GREY_MAX ((uint8_t) UINT8_MAX)
121 #define GREY_FMT PRIu8
122 #define GREY_FMTx PRIx8
123 #define GREY_FMTX PRIX8
124 #else
125 #error "Please set GREY_UINTBITS to a value in {8, 16, 32, 64}."
126 #endif
127 
130 
140 #define grey_add(grey, delta) grey_to(grey_from((grey)) + (delta))
141 
149 #define grey_incr(grey) grey_add((grey), 1)
150 
158 #define grey_decr(grey) grey_add((grey), -1)
159 
167 
175 
176 
199 uint8_t grey_binstr(char str[GREY_UINTBITS+1], grey_code_t grey);
200 
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 #endif /* GREY_H */
grey_from
grey_int_t grey_from(grey_code_t grey)
Converts a Grey-encoded value into a regular binary unsigned integer.
grey_int_t
grey_code_t grey_int_t
Binary value (regular integer), of the same size as Grey-coded values.
Definition: grey.h:129
grey_binstr
uint8_t grey_binstr(char str[GREY_UINTBITS+1], grey_code_t grey)
Fills a string with the Grey code in binary representation.
grey_to
grey_code_t grey_to(grey_int_t value)
Converts a regular binary unsigned integer to Grey code.
grey_code_t
uint64_t grey_code_t
Definition: grey.h:101
GREY_UINTBITS
#define GREY_UINTBITS
Definition: grey.h:53