Skip to content

Commit

Permalink
Merge pull request #91 from bgamari/edge
Browse files Browse the repository at this point in the history
Ensure that C++11 checksum is evaluated at compile-time
  • Loading branch information
bgamari committed Jan 31, 2013
2 parents d9664db + 20c3039 commit 09f67f2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/libs/checksumm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
*/
#ifdef CHECKSUM_USE_CPP

/* Cool C++11 approach contributed by bgamari on smoothieware IRC channel. */
#include <type_traits>

/* Cool C++11 approach contributed by bgamari on #smoothieware IRC channel.
* Unfortunately this will have to wait until after we switch to GCC 4.7.
*/
constexpr uint16_t checksum(const char* s, size_t n, size_t i, uint16_t sum1, uint16_t sum2) {
return (i <= n) ? checksum(s, n, i+1, (sum1 + s[i]) % 255, (sum2 + sum1) % 255) : ((sum2 << 8) | sum1);
}
Expand All @@ -42,7 +46,7 @@ constexpr uint16_t operator "" _checksum(const char* s, size_t n) {
return checksum(s, n, 0, 0, 0);
}

#define CHECKSUM(X) X##_checksum
#define CHECKSUM(X) std::integral_constant<uint16_t, X##_checksum>::value

#else /* !CHECKSUM_USE_CPP */

Expand Down

0 comments on commit 09f67f2

Please sign in to comment.