util: Add a helper for faster remainders

This should be at least as fast as using fast_idiv_by_const, and has the
advantage that the precomputation is simple enough to be evaluated at
Mesa-compile time for hash tables and sets which have a fixed table of
possible divisors.

Acked-by: Eric Anholt <eric@anholt.net>
Acked-by: Jason Ekstrand <jason@jlekstrand.net>
This commit is contained in:
Connor Abbott
2019-03-29 15:08:17 +01:00
parent 983b001c77
commit b87817871b
5 changed files with 210 additions and 0 deletions

View File

@@ -834,6 +834,10 @@ if cc.compiles('int foo(void) __attribute__((__noreturn__));',
name : '__attribute__((__noreturn__))')
pre_args += '-DHAVE_FUNC_ATTRIBUTE_NORETURN'
endif
if cc.compiles('__uint128_t foo(void) { return 0; }',
name : '__uint128_t')
pre_args += '-DHAVE_UINT128'
endif
# TODO: this is very incomplete
if ['linux', 'cygwin', 'gnu'].contains(host_machine.system())

View File

@@ -0,0 +1,74 @@
/*
* Copyright © 2010 Valve Software
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdint.h>
/*
* Code for fast 32-bit unsigned remainder, based off of "Faster Remainder by
* Direct Computation: Applications to Compilers and Software Libraries,"
* available at https://arxiv.org/pdf/1902.01961.pdf.
*
* util_fast_urem32(n, d, REMAINDER_MAGIC(d)) returns the same thing as
* n % d for any unsigned n and d, however it compiles down to only a few
* multiplications, so it should be faster than plain uint32_t modulo if the
* same divisor is used many times.
*/
#define REMAINDER_MAGIC(divisor) \
((uint64_t) ~0ull / (divisor) + 1)
/*
* Get bits 64-96 of a 32x64-bit multiply. If __int128_t is available, we use
* it, which usually compiles down to one instruction on 64-bit architectures.
* Otherwise on 32-bit architectures we usually get four instructions (one
* 32x32->64 multiply, one 32x32->32 multiply, and one 64-bit add).
*/
static inline uint32_t
_mul32by64_hi(uint32_t a, uint64_t b)
{
#ifdef HAVE_UINT128
return ((__uint128_t) b * a) >> 64;
#else
/*
* Let b = b0 + 2^32 * b1. Then a * b = a * b0 + 2^32 * a * b1. We would
* have to do a 96-bit addition to get the full result, except that only
* one term has non-zero lower 32 bits, which means that to get the high 32
* bits, we only have to add the high 64 bits of each term. Unfortunately,
* we have to do the 64-bit addition in case the low 32 bits overflow.
*/
uint32_t b0 = (uint32_t) b;
uint32_t b1 = b >> 32;
return ((((uint64_t) a * b0) >> 32) + (uint64_t) a * b1) >> 32;
#endif
}
static inline uint32_t
util_fast_urem32(uint32_t n, uint32_t d, uint64_t magic)
{
uint64_t lowbits = magic * n;
uint32_t result = _mul32by64_hi(d, lowbits);
assert(result == n % d);
return result;
}

View File

@@ -208,6 +208,7 @@ if with_tests
)
subdir('tests/fast_idiv_by_const')
subdir('tests/fast_urem_by_const')
subdir('tests/hash_table')
subdir('tests/string_buffer')
subdir('tests/vma')

View File

@@ -0,0 +1,99 @@
/*
* Copyright © 2018 Intel Corporation
* Copyright © 2019 Valve Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#undef NDEBUG
#include <gtest/gtest.h>
#include "util/fast_urem_by_const.h"
#define RAND_TEST_ITERATIONS 100000
static uint32_t
rand_uint(unsigned min)
{
/* Make sure we get some small and large numbers and powers of two every
* once in a while
*/
int k = rand() % 64;
if (k == 17) {
return min + (rand() % 16);
} else if (k == 42) {
return UINT32_MAX - (rand() % 16);
} else if (k == 9) {
uint32_t r;
do {
r = 1ull << (rand() % 32);
} while (r < min);
return r;
}
if (min == 0) {
uint32_t r = 0;
for (unsigned i = 0; i < 4; i++)
r |= ((uint32_t)rand() & 0xf) << i * 8;
return r >> (31 - (rand() % 32));
} else {
uint64_t r;
do {
r = rand_uint(0);
} while (r < min);
return r;
}
}
static void
test_case(uint32_t n, uint32_t d)
{
assert(d >= 1);
uint64_t magic = REMAINDER_MAGIC(d);
/* Note: there's already an assert inside util_fast_urem32(), so the
* EXPECT_EQ is only here to make sure the test fails with a wrong result
* even if asserts are disabled. Ideally we could disable the assert just
* for the test to get a better error message, but that doesn't seem too
* easy.
*/
EXPECT_EQ(util_fast_urem32(n, d, magic), n % d);
}
TEST(fast_urem_by_const, random)
{
for (unsigned i = 0; i < RAND_TEST_ITERATIONS; i++) {
uint64_t n = rand_uint(0);
uint64_t d = rand_uint(1);
test_case(n, d);
}
}
TEST(fast_urem_by_const, special_cases)
{
test_case(0, 1);
test_case(0, UINT32_MAX);
test_case(UINT32_MAX, 1);
test_case(1, UINT32_MAX);
test_case(UINT32_MAX, UINT32_MAX);
test_case(UINT32_MAX, UINT32_MAX - 1);
test_case(UINT32_MAX - 1, UINT32_MAX - 1);
test_case(UINT32_MAX - 2, UINT32_MAX - 1);
}

View File

@@ -0,0 +1,32 @@
# Copyright © 2018 Intel Corporation
# Copyright © 2010 Valve Corporation
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
test(
'fast_urem_by_const',
executable(
'fast_urem_by_const_test',
'fast_urem_by_const_test.cpp',
dependencies : [dep_thread, dep_dl, idep_gtest],
include_directories : inc_common,
link_with : [libmesa_util],
),
suite : ['util'],
)