Permalink
Browse files

Ensure that C++11 checksum is evaluated at compile-time

Wrapping the expression in an std::integral_constant<> seems to do the
right thing, even with no optimization.

Unfortunately, it seems that gcc 4.6 doesn't support user-defined string
literals, so we'll need to wait until we start building with 4.7 before
enabling this.
  • Loading branch information...
bgamari committed Jan 31, 2013
1 parent d9664db commit 20c3039cd8408fdb96fb8ab5fbf65fc16b2ab2f2
Showing with 6 additions and 2 deletions.
  1. +6 −2 src/libs/checksumm.h
View
@@ -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);
}
@@ -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 */

0 comments on commit 20c3039

Please sign in to comment.