From df27f90958f7684750dcfcda4ce3627ca9cb3a8a Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Sun, 20 Sep 2020 12:13:04 +1000 Subject: util/bitmap.cpp: Use std::fill_n to get better code for modern CPUs. --- src/lib/util/bitmap.cpp | 89 +++++++++---------------------------------------- 1 file changed, 15 insertions(+), 74 deletions(-) diff --git a/src/lib/util/bitmap.cpp b/src/lib/util/bitmap.cpp index ed814aae218..2b7cd74b5a1 100644 --- a/src/lib/util/bitmap.cpp +++ b/src/lib/util/bitmap.cpp @@ -8,14 +8,13 @@ ***************************************************************************/ -#include - #include "bitmap.h" +#include +#include #include - //************************************************************************** // INLINE HELPERS //************************************************************************** @@ -420,90 +419,32 @@ void bitmap_t::set_palette(palette_t *palette) void bitmap_t::fill(uint32_t color, const rectangle &cliprect) { // if we have a cliprect, intersect with that - rectangle fill = cliprect; + rectangle fill(cliprect); fill &= m_cliprect; - if (fill.empty()) - return; - - // based on the bpp go from there - switch (m_bpp) + if (!fill.empty()) { + // based on the bpp go from there + switch (m_bpp) + { case 8: - // 8bpp always uses memset for (int32_t y = fill.top(); y <= fill.bottom(); y++) - memset(raw_pixptr(y, fill.left()), uint8_t(color), fill.width()); + std::fill_n(&pixt(y, fill.left()), fill.width(), uint8_t(color)); break; case 16: - // 16bpp can use memset if the bytes are equal - if (uint8_t(color >> 8) == uint8_t(color)) - { - for (int32_t y = fill.top(); y <= fill.bottom(); y++) - memset(raw_pixptr(y, fill.left()), uint8_t(color), fill.width() * 2); - } - else - { - // Fill the first line the hard way - uint16_t *destrow = &pixt(fill.top()); - for (int32_t x = fill.left(); x <= fill.right(); x++) - destrow[x] = uint16_t(color); - - // For the other lines, just copy the first one - void *destrow0 = &pixt(fill.top(), fill.left()); - for (int32_t y = fill.top() + 1; y <= fill.bottom(); y++) - { - destrow = &pixt(y, fill.left()); - memcpy(destrow, destrow0, fill.width() * 2); - } - } + for (int32_t y = fill.top(); y <= fill.bottom(); ++y) + std::fill_n(&pixt(y, fill.left()), fill.width(), uint16_t(color)); break; case 32: - // 32bpp can use memset if the bytes are equal - if ((uint8_t)(color >> 8) == (uint8_t)color && (uint16_t)(color >> 16) == (uint16_t)color) - { - for (int32_t y = fill.top(); y <= fill.bottom(); y++) - memset(&pixt(y, fill.left()), uint8_t(color), fill.width() * 4); - } - else - { - // Fill the first line the hard way - uint32_t *destrow = &pixt(fill.top()); - for (int32_t x = fill.left(); x <= fill.right(); x++) - destrow[x] = uint32_t(color); - - // For the other lines, just copy the first one - uint32_t *destrow0 = &pixt(fill.top(), fill.left()); - for (int32_t y = fill.top() + 1; y <= fill.bottom(); y++) - { - destrow = &pixt(y, fill.left()); - memcpy(destrow, destrow0, fill.width() * 4); - } - } + for (int32_t y = fill.top(); y <= fill.bottom(); ++y) + std::fill_n(&pixt(y, fill.left()), fill.width(), uint32_t(color)); break; case 64: - // 64bpp can use memset if the bytes are equal - if (uint8_t(color >> 8) == uint8_t(color) && uint16_t(color >> 16) == uint16_t(color)) // FIXME: really? wat about the upper bits that would be zeroed when done the "hard way"? - { - for (int32_t y = fill.top(); y <= fill.bottom(); y++) - memset(&pixt(y, fill.left()), uint8_t(color), fill.width() * 8); - } - else - { - // Fill the first line the hard way - uint64_t *destrow = &pixt(fill.top()); - for (int32_t x = fill.left(); x <= fill.right(); x++) - destrow[x] = uint64_t(color); - - // For the other lines, just copy the first one - uint64_t *destrow0 = &pixt(fill.top(), fill.left()); - for (int32_t y = fill.top() + 1; y <= fill.bottom(); y++) - { - destrow = &pixt(y, fill.left()); - memcpy(destrow, destrow0, fill.width() * 8); - } - } + for (int32_t y = fill.top(); y <= fill.bottom(); ++y) + std::fill_n(&pixt(y, fill.left()), fill.width(), uint64_t(color)); break; + } } } -- cgit v1.2.3