Skip to content

Commit 14b50c6

Browse files
Zob314ZachOBjwellbelove
authored
Allow compile time CRC calculation (#1016)
* Making crc constexpr for c++14. Allows compile time CRC computation. * Fix syntax when using c++03 or `ETL_FORCE_NO_ADVANCED_CPP` * Remove use of `ETL_FORCE_NO_ADVANCED_CPP` option since it is no longer used. --------- Co-authored-by: Zach O'Brien <[email protected]> Co-authored-by: John Wellbelove <[email protected]>
1 parent 778351a commit 14b50c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1610
-21
lines changed

include/etl/frame_check_sequence.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ namespace etl
107107
//*************************************************************************
108108
/// Default constructor.
109109
//*************************************************************************
110-
frame_check_sequence()
110+
ETL_CONSTEXPR14 frame_check_sequence() : frame_check()
111111
{
112112
reset();
113113
}
@@ -118,7 +118,7 @@ namespace etl
118118
/// \param end End of the range.
119119
//*************************************************************************
120120
template<typename TIterator>
121-
frame_check_sequence(TIterator begin, const TIterator end)
121+
ETL_CONSTEXPR14 frame_check_sequence(TIterator begin, const TIterator end) : frame_check()
122122
{
123123
ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
124124

@@ -129,7 +129,7 @@ namespace etl
129129
//*************************************************************************
130130
/// Resets the FCS to the initial state.
131131
//*************************************************************************
132-
void reset()
132+
ETL_CONSTEXPR14 void reset()
133133
{
134134
frame_check = policy.initial();
135135
}
@@ -140,7 +140,7 @@ namespace etl
140140
/// \param end
141141
//*************************************************************************
142142
template<typename TIterator>
143-
void add(TIterator begin, const TIterator end)
143+
ETL_CONSTEXPR14 void add(TIterator begin, const TIterator end)
144144
{
145145
ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
146146

@@ -154,31 +154,31 @@ namespace etl
154154
//*************************************************************************
155155
/// \param value The uint8_t to add to the FCS.
156156
//*************************************************************************
157-
void add(uint8_t value_)
157+
ETL_CONSTEXPR14 void add(uint8_t value_)
158158
{
159159
frame_check = policy.add(frame_check, value_);
160160
}
161161

162162
//*************************************************************************
163163
/// Gets the FCS value.
164164
//*************************************************************************
165-
value_type value() const
165+
ETL_CONSTEXPR14 value_type value() const
166166
{
167167
return policy.final(frame_check);
168168
}
169169

170170
//*************************************************************************
171171
/// Conversion operator to value_type.
172172
//*************************************************************************
173-
operator value_type () const
173+
ETL_CONSTEXPR14 operator value_type () const
174174
{
175175
return policy.final(frame_check);
176176
}
177177

178178
//*************************************************************************
179179
/// Gets an add_insert_iterator for input.
180180
//*************************************************************************
181-
add_insert_iterator input()
181+
ETL_CONSTEXPR14 add_insert_iterator input()
182182
{
183183
return add_insert_iterator(*this);
184184
}

include/etl/private/crc_implementation.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ namespace etl
163163
// Accumulator_Bits > Chunk_Bits
164164
// Not Reflected
165165
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, bool Reflect>
166-
static
166+
static ETL_CONSTEXPR14
167167
typename etl::enable_if<(Accumulator_Bits > Chunk_Bits) && !Reflect, TAccumulator>::type
168168
crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[])
169169
{
@@ -181,7 +181,7 @@ namespace etl
181181
// Accumulator_Bits > Chunk_Bits
182182
// Reflected
183183
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, bool Reflect>
184-
static
184+
static ETL_CONSTEXPR14
185185
typename etl::enable_if<(Accumulator_Bits > Chunk_Bits) && Reflect, TAccumulator>::type
186186
crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[])
187187
{
@@ -199,7 +199,7 @@ namespace etl
199199
// Accumulator_Bits == Chunk_Bits
200200
// Not Reflected
201201
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, bool Reflect>
202-
static
202+
static ETL_CONSTEXPR14
203203
typename etl::enable_if<(Accumulator_Bits == Chunk_Bits) && !Reflect, TAccumulator>::type
204204
crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[])
205205
{
@@ -216,7 +216,7 @@ namespace etl
216216
// Accumulator_Bits == Chunk_Bits
217217
// Reflected
218218
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, bool Reflect>
219-
static
219+
static ETL_CONSTEXPR14
220220
typename etl::enable_if<(Accumulator_Bits == Chunk_Bits) && Reflect, TAccumulator>::type
221221
crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[])
222222
{
@@ -241,16 +241,21 @@ namespace etl
241241
struct crc_table<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Polynomial, Reflect, 4U>
242242
{
243243
//*************************************************************************
244+
#if !ETL_USING_CPP11
244245
TAccumulator add(TAccumulator crc, uint8_t value) const
245246
{
247+
#endif
246248
static ETL_CONSTANT TAccumulator table[4U] =
247249
{
248250
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 0U, Chunk_Bits>::value,
249251
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 1U, Chunk_Bits>::value,
250252
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 2U, Chunk_Bits>::value,
251253
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 3U, Chunk_Bits>::value
252254
};
253-
255+
#if ETL_USING_CPP11
256+
ETL_CONSTEXPR14 TAccumulator add(TAccumulator crc, uint8_t value) const
257+
{
258+
#endif
254259
if ETL_IF_CONSTEXPR(Reflect)
255260
{
256261
crc = crc_update_chunk<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Reflect>(crc, value, table);
@@ -269,15 +274,21 @@ namespace etl
269274
return crc;
270275
}
271276
};
277+
#if ETL_USING_CPP11
278+
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, TAccumulator Polynomial, bool Reflect>
279+
ETL_CONSTANT TAccumulator crc_table<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Polynomial, Reflect, 4U>::table[4U];
280+
#endif
272281

273282
//*********************************
274283
// Table size of 16.
275284
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, TAccumulator Polynomial, bool Reflect>
276285
struct crc_table<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Polynomial, Reflect, 16U>
277286
{
278287
//*************************************************************************
288+
#if !ETL_USING_CPP11
279289
TAccumulator add(TAccumulator crc, uint8_t value) const
280290
{
291+
#endif
281292
static ETL_CONSTANT TAccumulator table[16U] =
282293
{
283294
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 0U, Chunk_Bits>::value,
@@ -297,7 +308,10 @@ namespace etl
297308
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 14U, Chunk_Bits>::value,
298309
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 15U, Chunk_Bits>::value
299310
};
300-
311+
#if ETL_USING_CPP11
312+
ETL_CONSTEXPR14 TAccumulator add(TAccumulator crc, uint8_t value) const
313+
{
314+
#endif
301315
if ETL_IF_CONSTEXPR(Reflect)
302316
{
303317
crc = crc_update_chunk<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Reflect>(crc, value, table);
@@ -312,16 +326,22 @@ namespace etl
312326
return crc;
313327
}
314328
};
329+
#if ETL_USING_CPP11
330+
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, TAccumulator Polynomial, bool Reflect>
331+
ETL_CONSTANT TAccumulator crc_table<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Polynomial, Reflect, 16U>::table[16U];
332+
#endif
315333

316334
//*********************************
317335
// Table size of 256.
318336
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, TAccumulator Polynomial, bool Reflect>
319337
struct crc_table<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Polynomial, Reflect, 256U>
320338
{
321339
//*************************************************************************
340+
#if !ETL_USING_CPP11
322341
TAccumulator add(TAccumulator crc, uint8_t value) const
323342
{
324-
static ETL_CONSTANT TAccumulator table[256U] =
343+
#endif
344+
static ETL_CONSTANT TAccumulator table[256U]=
325345
{
326346
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 0U, Chunk_Bits>::value,
327347
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 1U, Chunk_Bits>::value,
@@ -580,13 +600,20 @@ namespace etl
580600
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 254U, Chunk_Bits>::value,
581601
crc_table_entry<TAccumulator, Accumulator_Bits, Polynomial, Reflect, 255U, Chunk_Bits>::value
582602
};
603+
#if ETL_USING_CPP11
604+
ETL_CONSTEXPR14 TAccumulator add(TAccumulator crc, uint8_t value) const
605+
{
606+
#endif
583607

584608
crc = crc_update_chunk<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Reflect>(crc, value, table);
585609

586610
return crc;
587611
}
588612
};
589-
613+
#if ETL_USING_CPP11
614+
template <typename TAccumulator, size_t Accumulator_Bits, size_t Chunk_Bits, uint8_t Chunk_Mask, TAccumulator Polynomial, bool Reflect>
615+
ETL_CONSTANT TAccumulator crc_table<TAccumulator, Accumulator_Bits, Chunk_Bits, Chunk_Mask, Polynomial, Reflect, 256U>::table[256U];
616+
#endif
590617
//*****************************************************************************
591618
// CRC Policies.
592619
//*****************************************************************************
@@ -615,7 +642,7 @@ namespace etl
615642
}
616643

617644
//*************************************************************************
618-
accumulator_type final(accumulator_type crc) const
645+
ETL_CONSTEXPR accumulator_type final(accumulator_type crc) const
619646
{
620647
return crc ^ TCrcParameters::Xor_Out;
621648
}
@@ -643,7 +670,7 @@ namespace etl
643670
}
644671

645672
//*************************************************************************
646-
accumulator_type final(accumulator_type crc) const
673+
ETL_CONSTEXPR accumulator_type final(accumulator_type crc) const
647674
{
648675
return crc ^ TCrcParameters::Xor_Out;
649676
}
@@ -671,7 +698,7 @@ namespace etl
671698
}
672699

673700
//*************************************************************************
674-
accumulator_type final(accumulator_type crc) const
701+
ETL_CONSTEXPR accumulator_type final(accumulator_type crc) const
675702
{
676703
return crc ^ TCrcParameters::Xor_Out;
677704
}
@@ -691,7 +718,7 @@ namespace etl
691718
//*************************************************************************
692719
/// Default constructor.
693720
//*************************************************************************
694-
crc_type()
721+
ETL_CONSTEXPR14 crc_type()
695722
{
696723
this->reset();
697724
}
@@ -702,7 +729,7 @@ namespace etl
702729
/// \param end End of the range.
703730
//*************************************************************************
704731
template<typename TIterator>
705-
crc_type(TIterator begin, const TIterator end)
732+
ETL_CONSTEXPR14 crc_type(TIterator begin, const TIterator end)
706733
{
707734
this->reset();
708735
this->add(begin, end);

test/test_crc16.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ namespace
127127
CHECK_EQUAL(0xBB3DU, crc);
128128
}
129129

130+
#if ETL_USING_CPP14
131+
//*************************************************************************
132+
TEST(test_crc16_16_constexpr)
133+
{
134+
constexpr char data[] = "123456789";
135+
constexpr uint16_t crc = etl::crc16_t16(data, data + 9);
136+
137+
CHECK_EQUAL(0xBB3DU, crc);
138+
}
139+
#endif
140+
130141
//*************************************************************************
131142
TEST(test_crc16_16_add_values)
132143
{
@@ -199,6 +210,17 @@ namespace
199210
CHECK_EQUAL(0xBB3DU, crc);
200211
}
201212

213+
#if ETL_USING_CPP14
214+
//*************************************************************************
215+
TEST(test_crc16_4_constexpr)
216+
{
217+
constexpr char data[] = "123456789";
218+
constexpr uint16_t crc = etl::crc16_t4(data, data + 9);
219+
220+
CHECK_EQUAL(0xBB3DU, crc);
221+
}
222+
#endif
223+
202224
//*************************************************************************
203225
TEST(test_crc16_4_add_values)
204226
{

test/test_crc16_a.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ namespace
5555
CHECK_EQUAL(0xBF05U, crc);
5656
}
5757

58+
#if ETL_USING_CPP14
59+
//*************************************************************************
60+
TEST(test_crc16_a_constexpr)
61+
{
62+
constexpr char data[] = "123456789";
63+
constexpr uint16_t crc = etl::crc16_a(data, data + 9);
64+
65+
CHECK_EQUAL(0xBF05U, crc);
66+
}
67+
#endif
68+
5869
//*************************************************************************
5970
TEST(test_crc16_a_add_values)
6071
{
@@ -127,6 +138,17 @@ namespace
127138
CHECK_EQUAL(0xBF05U, crc);
128139
}
129140

141+
#if ETL_USING_CPP14
142+
//*************************************************************************
143+
TEST(test_crc16_a_16_constexpr)
144+
{
145+
constexpr char data[] = "123456789";
146+
constexpr uint16_t crc = etl::crc16_a_t16(data, data + 9);
147+
148+
CHECK_EQUAL(0xBF05U, crc);
149+
}
150+
#endif
151+
130152
//*************************************************************************
131153
TEST(test_crc16_a_16_add_values)
132154
{
@@ -199,6 +221,17 @@ namespace
199221
CHECK_EQUAL(0xBF05U, crc);
200222
}
201223

224+
#if ETL_USING_CPP14
225+
//*************************************************************************
226+
TEST(test_crc16_a_4_constexpr)
227+
{
228+
constexpr char data[] = "123456789";
229+
constexpr uint16_t crc = etl::crc16_a_t4(data, data + 9);
230+
231+
CHECK_EQUAL(0xBF05U, crc);
232+
}
233+
#endif
234+
202235
//*************************************************************************
203236
TEST(test_crc16_a_4_add_values)
204237
{

test/test_crc16_arc.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ namespace
5555
CHECK_EQUAL(0xBB3DU, crc);
5656
}
5757

58+
#if ETL_USING_CPP14
59+
//*************************************************************************
60+
TEST(test_crc16_arc_constexpr)
61+
{
62+
constexpr char data[] = "123456789";
63+
constexpr uint16_t crc = etl::crc16_arc(data, data + 9);
64+
65+
CHECK_EQUAL(0xBB3DU, crc);
66+
}
67+
#endif
68+
5869
//*************************************************************************
5970
TEST(test_crc16_arc_add_values)
6071
{
@@ -127,6 +138,17 @@ namespace
127138
CHECK_EQUAL(0xBB3DU, crc);
128139
}
129140

141+
#if ETL_USING_CPP14
142+
//*************************************************************************
143+
TEST(test_crc16_arc_16_constexpr)
144+
{
145+
constexpr char data[] = "123456789";
146+
constexpr uint16_t crc = etl::crc16_arc_t16(data, data + 9);
147+
148+
CHECK_EQUAL(0xBB3DU, crc);
149+
}
150+
#endif
151+
130152
//*************************************************************************
131153
TEST(test_crc16_arc_16_add_values)
132154
{
@@ -199,6 +221,17 @@ namespace
199221
CHECK_EQUAL(0xBB3DU, crc);
200222
}
201223

224+
#if ETL_USING_CPP14
225+
//*************************************************************************
226+
TEST(test_crc16_arc_4_constexpr)
227+
{
228+
constexpr char data[] = "123456789";
229+
constexpr uint16_t crc = etl::crc16_arc_t4(data, data + 9);
230+
231+
CHECK_EQUAL(0xBB3DU, crc);
232+
}
233+
#endif
234+
202235
//*************************************************************************
203236
TEST(test_crc16_arc_4_add_values)
204237
{

0 commit comments

Comments
 (0)