From c3c3137ab3a3d09c12eaa7140cb7ec760bdfe924 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Sat, 14 Dec 2019 12:13:09 +0000 Subject: [PATCH] BACKENDS: Move nextHigher2() into common/algorithm.h --- backends/graphics/opengl/texture.cpp | 18 +++--------------- backends/platform/3ds/sprite.cpp | 17 +++-------------- common/algorithm.h | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/backends/graphics/opengl/texture.cpp b/backends/graphics/opengl/texture.cpp index 93480f73603..2b6f9ce0a1b 100644 --- a/backends/graphics/opengl/texture.cpp +++ b/backends/graphics/opengl/texture.cpp @@ -26,25 +26,13 @@ #include "backends/graphics/opengl/pipelines/clut8.h" #include "backends/graphics/opengl/framebuffer.h" +#include "common/algorithm.h" #include "common/endian.h" #include "common/rect.h" #include "common/textconsole.h" namespace OpenGL { -static GLuint nextHigher2(GLuint v) { - if (v == 0) - return 1; - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - return ++v; -} - - GLTexture::GLTexture(GLenum glIntFormat, GLenum glFormat, GLenum glType) : _glIntFormat(glIntFormat), _glFormat(glFormat), _glType(glType), _width(0), _height(0), _logicalWidth(0), _logicalHeight(0), @@ -107,8 +95,8 @@ void GLTexture::setSize(uint width, uint height) { const uint oldHeight = _height; if (!g_context.NPOTSupported) { - _width = nextHigher2(width); - _height = nextHigher2(height); + _width = Common::nextHigher2(width); + _height = Common::nextHigher2(height); } else { _width = width; _height = height; diff --git a/backends/platform/3ds/sprite.cpp b/backends/platform/3ds/sprite.cpp index bbccf94fe60..74805b40cb2 100644 --- a/backends/platform/3ds/sprite.cpp +++ b/backends/platform/3ds/sprite.cpp @@ -21,20 +21,9 @@ */ #include "backends/platform/3ds/sprite.h" +#include "common/algorithm.h" #include "common/util.h" -static uint nextHigher2(uint v) { - if (v == 0) - return 1; - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - return ++v; -} - Sprite::Sprite() : dirtyPixels(true) , dirtyMatrix(true) @@ -62,8 +51,8 @@ void Sprite::create(uint16 width, uint16 height, const Graphics::PixelFormat &f) actualWidth = width; actualHeight = height; format = f; - w = MAX(nextHigher2(width), 64u); - h = MAX(nextHigher2(height), 64u); + w = MAX(Common::nextHigher2(width), 64u); + h = MAX(Common::nextHigher2(height), 64u); pitch = w * format.bytesPerPixel; dirtyPixels = true; diff --git a/common/algorithm.h b/common/algorithm.h index 8384eb7089d..1aac0376dbc 100644 --- a/common/algorithm.h +++ b/common/algorithm.h @@ -271,6 +271,22 @@ T gcd(T a, T b) { #pragma warning(pop) #endif +/** + * Get the next highest power of 2. + */ +template +T nextHigher2(T v) { + if (v == 0) + return 1; + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + return ++v; +} + /** * Replacement algorithm for iterables. *