From 567b368fa6ebcd75b7a347e171ccbcb8c6f894ff Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Thu, 2 Jan 2014 01:31:41 +0100 Subject: [PATCH] rewired some functions in qmc.h for better convenience --- include/mitsuba/core/qmc.h | 67 +++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/include/mitsuba/core/qmc.h b/include/mitsuba/core/qmc.h index 940667af..eb01f39b 100644 --- a/include/mitsuba/core/qmc.h +++ b/include/mitsuba/core/qmc.h @@ -155,11 +155,10 @@ inline uint64_t sampleTEA(uint32_t v0, uint32_t v1, int rounds = 4) { return ((uint64_t) v1 << 32) + v0; } -#if defined(DOUBLE_PRECISION) /** * \brief Generate fast and reasonably good pseudorandom numbers using the * Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham. - * + * * This function uses \ref sampleTEA to return single precision floating point * numbers on the interval [0, 1) * @@ -173,7 +172,35 @@ inline uint64_t sampleTEA(uint32_t v0, uint32_t v1, int rounds = 4) { * \return * A uniformly distributed floating point number on the interval [0, 1) */ -inline Float sampleTEAFloat(uint32_t v0, uint32_t v1, int rounds = 4) { +inline float sampleTEASingle(uint32_t v0, uint32_t v1, int rounds = 4) { + /* Trick from MTGP: generate an uniformly distributed + single precision number in [1,2) and subtract 1. */ + union { + uint32_t u; + float f; + } x; + x.u = ((sampleTEA(v0, v1, rounds) & 0xFFFFFFFF) >> 9) | 0x3f800000UL; + return x.f - 1.0f; +} + +/** + * \brief Generate fast and reasonably good pseudorandom numbers using the + * Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham. + * + * This function uses \ref sampleTEA to return single precision floating point + * numbers on the interval [0, 1) + * + * \param v0 + * First input value to be encrypted (could be the sample index) + * \param v1 + * Second input value to be encrypted (e.g. the requested random number dimension) + * \param rounds + * How many rounds should be executed? The default for random number + * generation is 4. + * \return + * A uniformly distributed floating point number on the interval [0, 1) + */ +inline double sampleTEADouble(uint32_t v0, uint32_t v1, int rounds = 4) { /* Trick from MTGP: generate an uniformly distributed single precision number in [1,2) and subtract 1. */ union { @@ -184,33 +211,15 @@ inline Float sampleTEAFloat(uint32_t v0, uint32_t v1, int rounds = 4) { return x.f - 1.0; } -#else -/** - * \brief Generate fast and reasonably good pseudorandom numbers using the - * Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham. - * - * This function uses \ref sampleTEA to return single precision floating point - * numbers on the interval [0, 1) - * - * \param v0 - * First input value to be encrypted (could be the sample index) - * \param v1 - * Second input value to be encrypted (e.g. the requested random number dimension) - * \param rounds - * How many rounds should be executed? The default for random number - * generation is 4. - * \return - * A uniformly distributed floating point number on the interval [0, 1) - */ +#if defined(SINGLE_PRECISION) +/// Alias to \ref sampleTEASingle or \ref sampleTEADouble based on compilation flags inline Float sampleTEAFloat(uint32_t v0, uint32_t v1, int rounds = 4) { - /* Trick from MTGP: generate an uniformly distributed - single precision number in [1,2) and subtract 1. */ - union { - uint32_t u; - float f; - } x; - x.u = ((sampleTEA(v0, v1, rounds) & 0xFFFFFFFF) >> 9) | 0x3f800000UL; - return x.f - 1.0f; + return sampleTEASingle(v0, v1, rounds); +} +#else +/// Alias to \ref sampleTEASingle or \ref sampleTEADouble based on compilation flags +inline Float sampleTEAFloat(uint32_t v0, uint32_t v1, int rounds = 4) { + return sampleTEADouble(v0, v1, rounds); } #endif