SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
softword.h
Go to the documentation of this file.
1 #ifndef LEANSDR_SOFTWORD_H
2 #define LEANSDR_SOFTWORD_H
3 
4 namespace leansdr
5 {
6 
7 // This file implements options for SOFTBYTE in s2_frame_receiver
8 // (representation of bytes after deinterleaving).
9 // Also used as SOFTWORD in ldpc_engine
10 // (representation of packed SOFTBITs).
11 
12 // Interface for ldpc_engine:
13 // SOFTBIT softword_get(const SOFTWORD &p, int b)
14 // SOFTBIT softwords_xor(const SOFTWORD p1[], const SOFTWORD p2[], int b)
15 // void softword_zero(SOFTWORD *p, int b)
16 // void softwords_set(SOFTWORD p[], int b)
17 // void softwords_flip(SOFTWORD p[], int b)
18 
19 // HARD SOFTWORD / SOFTBYTE
20 
21 typedef uint8_t hard_sb; // Bit 7 is transmitted first.
22 
23 inline bool softword_get(const hard_sb &p, int b)
24 {
25  return p & (1 << (7 - b));
26 }
27 inline void softword_set(hard_sb *p, int b, bool v)
28 {
29  hard_sb mask = 1 << (7 - b);
30  *p = ((*p) & ~mask) | (v << (7 - b));
31 }
32 inline void softword_clear(hard_sb *p) { *p = 0; }
33 inline bool softword_weight(const bool &l)
34 {
35  return true;
36 }
37 inline void softbit_set(bool *p, bool v) { *p = v; }
38 inline bool softbit_harden(bool b) { return b; }
39 inline uint8_t softbyte_harden(const hard_sb &b) { return b; }
40 inline bool softbit_xor(bool x, bool y) { return x ^ y; }
41 inline void softbit_clear(bool *p) { *p = false; }
42 inline bool softwords_xor(const hard_sb p1[], const hard_sb p2[], int b)
43 {
44  return (p1[b / 8] ^ p2[b / 8]) & (1 << (7 - (b & 7)));
45 }
46 inline void softword_zero(hard_sb *p)
47 {
48  *p = 0;
49 }
50 inline void softwords_set(hard_sb p[], int b)
51 {
52  p[b / 8] |= 1 << (7 - (b & 7));
53 }
54 inline void softword_write(hard_sb &p, int b, bool v)
55 {
56  hard_sb mask = 1 << (7 - b);
57  p = (p & ~mask) | (hard_sb)v << (7 - b);
58 }
59 inline void softwords_write(hard_sb p[], int b, bool v)
60 {
61  softword_write(p[b / 8], b & 7, v);
62 }
63 inline void softwords_flip(hard_sb p[], int b)
64 {
65  p[b / 8] ^= 1 << (7 - (b & 7));
66 }
67 uint8_t *softbytes_harden(hard_sb p[], int nbytes, uint8_t storage[])
68 {
69  return p;
70 }
71 
72 // LLR SOFTWORD
73 
74 struct llr_sb
75 {
76  llr_t bits[8]; // bits[0] is transmitted first.
77 };
78 
79 float prob(llr_t l)
80 {
81  return (127.0 + l) / 254;
82 }
83 llr_t llr(float p)
84 {
85  int r = -127 + 254 * p;
86  if (r < -127)
87  r = -127;
88  if (r > 127)
89  r = 127;
90  return r;
91 }
92 inline llr_t llr_xor(llr_t lx, llr_t ly)
93 {
94  float px = prob(lx), py = prob(ly);
95  return llr(px * (1 - py) + (1 - px) * py);
96 }
97 
98 inline llr_t softword_get(const llr_sb &p, int b)
99 {
100  return p.bits[b];
101 }
102 // Special case to avoid computing b/8*8+b%7. Assumes llr_sb[] packed.
103 inline llr_t softwords_get(const llr_sb p[], int b)
104 {
105  return p[0].bits[b]; // Beyond bounds on purpose
106 }
107 inline void softword_set(llr_sb *p, int b, llr_t v)
108 {
109  p->bits[b] = v;
110 }
111 inline void softword_clear(llr_sb *p)
112 {
113  memset(p->bits, 0, sizeof(p->bits));
114 }
115 // inline llr_t softwords_get(const llr_sb p[], int b) {
116 // return softword_get(p[b/8], b&7);
117 // }
118 inline llr_t softword_weight(llr_t l) { return abs(l); }
119 inline void softbit_set(llr_t *p, bool v) { *p = v ? -127 : 127; }
120 inline bool softbit_harden(llr_t l) { return (l < 0); }
122 {
123  // Without conditional jumps
124  uint8_t r = (((b.bits[0] & 128) >> 0) |
125  ((b.bits[1] & 128) >> 1) |
126  ((b.bits[2] & 128) >> 2) |
127  ((b.bits[3] & 128) >> 3) |
128  ((b.bits[4] & 128) >> 4) |
129  ((b.bits[5] & 128) >> 5) |
130  ((b.bits[6] & 128) >> 6) |
131  ((b.bits[7] & 128) >> 7));
132  return r;
133 }
134 inline llr_t softbit_xor(llr_t x, llr_t y) { return llr_xor(x, y); }
135 inline void softbit_clear(llr_t *p) { *p = -127; }
136 inline llr_t softwords_xor(const llr_sb p1[], const llr_sb p2[], int b)
137 {
138  return llr_xor(p1[b / 8].bits[b & 7], p2[b / 8].bits[b & 7]);
139 }
140 inline void softword_zero(llr_sb *p)
141 {
142  memset(p, -127, sizeof(*p));
143 }
144 inline void softwords_set(llr_sb p[], int b)
145 {
146  p[b / 8].bits[b & 7] = 127;
147 }
148 inline void softword_write(llr_sb &p, int b, llr_t v)
149 {
150  p.bits[b] = v;
151 }
152 inline void softwords_write(llr_sb p[], int b, llr_t v)
153 {
154  softword_write(p[b / 8], b & 7, v);
155 }
156 inline void softwords_flip(llr_sb p[], int b)
157 {
158  llr_t *l = &p[b / 8].bits[b & 7];
159  *l = -*l;
160 }
161 uint8_t *softbytes_harden(llr_sb p[], int nbytes, uint8_t storage[])
162 {
163  for (uint8_t *q = storage; nbytes--; ++p, ++q)
164  *q = softbyte_harden(*p);
165  return storage;
166 }
167 
168 // Generic wrappers
169 
170 template <typename SOFTBIT, typename SOFTBYTE>
171 inline SOFTBIT softwords_get(const SOFTBYTE p[], int b)
172 {
173  return softword_get(p[b / 8], b & 7);
174 }
175 template <typename SOFTBIT, typename SOFTBYTE>
176 inline void softwords_set(SOFTBYTE p[], int b, SOFTBIT v)
177 {
178  softword_set(&p[b / 8], b & 7, v);
179 }
180 template <typename SOFTBIT, typename SOFTBYTE>
181 inline SOFTBIT softwords_weight(const SOFTBYTE p[], int b)
182 {
183  return softword_weight(softwords_get<SOFTBIT, SOFTBYTE>(p, b));
184 }
185 
186 } // namespace leansdr
187 
188 #endif // LEANSDR_SOFTWORD_H
uint8_t * softbytes_harden(hard_sb p[], int nbytes, uint8_t storage[])
Definition: softword.h:67
void softword_write(hard_sb &p, int b, bool v)
Definition: softword.h:54
bool softwords_xor(const hard_sb p1[], const hard_sb p2[], int b)
Definition: softword.h:42
bool softbit_harden(bool b)
Definition: softword.h:38
Fixed< IntType, IntBits > abs(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2313
void softwords_flip(hard_sb p[], int b)
Definition: softword.h:63
void softbit_clear(bool *p)
Definition: softword.h:41
llr_t bits[8]
Definition: softword.h:76
float prob(llr_t l)
Definition: softword.h:79
uint8_t softbyte_harden(const hard_sb &b)
Definition: softword.h:39
llr_t llr(float p)
Definition: softword.h:83
unsigned char uint8_t
Definition: rtptypes_win.h:42
bool softword_weight(const bool &l)
Definition: softword.h:33
llr_t llr_xor(llr_t lx, llr_t ly)
Definition: softword.h:92
void softword_zero(hard_sb *p)
Definition: softword.h:46
void softword_set(hard_sb *p, int b, bool v)
Definition: softword.h:27
llr_t softwords_get(const llr_sb p[], int b)
Definition: softword.h:103
uint8_t hard_sb
Definition: softword.h:21
bool softbit_xor(bool x, bool y)
Definition: softword.h:40
void softwords_set(hard_sb p[], int b)
Definition: softword.h:50
void softbit_set(bool *p, bool v)
Definition: softword.h:37
bool softword_get(const hard_sb &p, int b)
Definition: softword.h:23
int8_t llr_t
Definition: sdr.h:444
void softword_clear(hard_sb *p)
Definition: softword.h:32
void softwords_write(hard_sb p[], int b, bool v)
Definition: softword.h:59
SOFTBIT softwords_weight(const SOFTBYTE p[], int b)
Definition: softword.h:181