From f5404c393283b81adb06aff8d5a8954c635e75db Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 20 Dec 2024 08:58:36 -0800 Subject: [PATCH] vertexcodec: Use generic zigzag() to match unzigzag() Instead of having three versions of zigzag per type we can use a template similarly to unzigzag. This produces ~same code with less duplication. --- src/vertexcodec.cpp | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/vertexcodec.cpp b/src/vertexcodec.cpp index 1990fa29c..759a6ce76 100644 --- a/src/vertexcodec.cpp +++ b/src/vertexcodec.cpp @@ -147,24 +147,15 @@ static size_t getVertexBlockSize(size_t vertex_size) return (result < kVertexBlockMaxSize) ? result : kVertexBlockMaxSize; } -inline unsigned char zigzag(unsigned char v) -{ - return ((signed char)(v) >> 7) ^ (v << 1); -} - -inline unsigned short zigzag(unsigned short v) -{ - return ((signed short)(v) >> 15) ^ (v << 1); -} - -inline unsigned int zigzag(unsigned int v) +inline unsigned int rotate(unsigned int v, int r) { - return ((signed int)(v) >> 31) ^ (v << 1); + return (v << r) | (v >> ((32 - r) & 31)); } -inline unsigned int rotate(unsigned int v, int r) +template +inline T zigzag(T v) { - return (v << r) | (v >> ((32 - r) & 31)); + return (0 - (v >> (sizeof(T) * 8 - 1))) ^ (v << 1); } template