Author: sparky Date: Fri Jul 30 17:00:26 2010 GMT Module: packages Tag: HEAD ---- Log message: - NEW; integer-only replacement for floor( rand( ) / N )
---- Files affected: packages/lvm2: lvm2-no-floor.patch (NONE -> 1.1) (NEW) ---- Diffs: ================================================================ Index: packages/lvm2/lvm2-no-floor.patch diff -u /dev/null packages/lvm2/lvm2-no-floor.patch:1.1 --- /dev/null Fri Jul 30 19:00:26 2010 +++ packages/lvm2/lvm2-no-floor.patch Fri Jul 30 19:00:21 2010 @@ -0,0 +1,69 @@ +From ab3a151bdae8566fddbd91cdc86c600083890f59 Mon Sep 17 00:00:00 2001 +From: Przemyslaw Iskra <[email protected]> +Date: Fri, 30 Jul 2010 18:45:43 +0200 +Subject: [PATCH] Don't use floor() in _bitset_with_random_bits + +Use _even_rand() function instead of floor() in +_bitset_with_random_bits(). floor() function is missing in dietlibc (on +architectures other than x86). Moreover using floor() to clip rand +results does not assure even result distribution. +_even_rand() uses integer arithmetic only and is designed to return evenly +distributed results. + +Signed-off-by: Przemyslaw Iskra <[email protected]> +--- + configure.in | 3 +-- + lib/metadata/metadata.c | 16 +++++++++++++++- + 2 files changed, 16 insertions(+), 3 deletions(-) + +diff --git a/configure.in b/configure.in +index bd56136..6f6c67e 100644 +--- a/configure.in ++++ b/configure.in +@@ -125,8 +125,7 @@ AC_STRUCT_TM + + ################################################################################ + dnl -- Check for functions +-AC_SEARCH_LIBS([floor], [m], , [AC_MSG_ERROR(bailing out)]) +-AC_CHECK_FUNCS([floor ftruncate gethostname getpagesize \ ++AC_CHECK_FUNCS([ftruncate gethostname getpagesize \ + gettimeofday memset mkdir mkfifo rmdir munmap nl_langinfo setenv setlocale \ + strcasecmp strchr strcspn strspn strdup strncasecmp strerror strrchr \ + strstr strtol strtoul uname], , [AC_MSG_ERROR(bailing out)]) +diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c +index 07222a7..6ee7731 100644 +--- a/lib/metadata/metadata.c ++++ b/lib/metadata/metadata.c +@@ -1018,6 +1018,20 @@ static int _recalc_extents(uint32_t *extents, const char *desc1, + return 1; + } + ++/* return random integer in [0,max) interval */ ++static unsigned _even_rand( unsigned *seed, unsigned max ) ++{ ++ unsigned r, ret; ++ ++ /* make sure distribution is even */ ++ do { ++ r = (unsigned) rand_r( seed ); ++ ret = r % max; ++ } while ( r - ret >= RAND_MAX - max ); ++ ++ return ret; ++} ++ + static dm_bitset_t _bitset_with_random_bits(struct dm_pool *mem, uint32_t num_bits, + uint32_t num_set_bits, unsigned *seed) + { +@@ -1040,7 +1054,7 @@ static dm_bitset_t _bitset_with_random_bits(struct dm_pool *mem, uint32_t num_bi + /* Perform loop num_set_bits times, selecting one bit each time */ + while (i++ < num_bits) { + /* Select a random bit between 0 and (i-1) inclusive. */ +- bit_selected = (unsigned) floor(i * (rand_r(seed) / (RAND_MAX + 1.0))); ++ bit_selected = _even_rand( seed, i ); + + /* + * If the bit was already set, set the new bit that became +-- +1.7.1 + ================================================================ _______________________________________________ pld-cvs-commit mailing list [email protected] http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit
