Use portable guf_math_ckdint functions in guf_rand

This commit is contained in:
jun 2025-05-19 00:48:38 +02:00
parent 096b83638b
commit 2355feaa70

View File

@ -637,9 +637,7 @@ GUF_RAND_KWRDS int_fast32_t guf_rand64_range_i32(guf_rand64_state *state, int_fa
// cf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random (last-retrieved 2025-02-12) // cf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random (last-retrieved 2025-02-12)
const double result = floor(guf_rand64_f64(state) * (delta + 1.0) + min); const double result = floor(guf_rand64_f64(state) * (delta + 1.0) + min);
GUF_ASSERT(result >= min && result <= max); GUF_ASSERT(result >= min && result <= max);
#ifdef INT32_MAX GUF_ASSERT((int_fast32_t)result <= GUF_INT32_MAX && (int_fast32_t)result >= GUF_INT32_MIN);
GUF_ASSERT((int_fast32_t)result <= INT32_MAX && (int_fast32_t)result >= INT32_MIN);
#endif
return (int_fast32_t)result; return (int_fast32_t)result;
} }
@ -682,14 +680,12 @@ GUF_RAND_KWRDS int_fast32_t guf_rand32_range_i32(guf_rand32_state *state, int_fa
} while (step >= limit); } while (step >= limit);
step = GUF_UWRAP_32(step / bin_size); step = GUF_UWRAP_32(step / bin_size);
#ifdef INT32_MAX GUF_ASSERT(guf_ckd_add_int_fast32_t(min, step) == GUF_MATH_CKD_SUCCESS);
GUF_ASSERT(guf_ckd_add_i32(min, step) == GUF_MATH_CKD_SUCCESS);
#endif
const int_fast32_t rnd = min + (int_fast32_t)step; const int_fast32_t rnd = min + (int_fast32_t)step;
GUF_ASSERT(rnd >= min && rnd <= max); GUF_ASSERT(rnd >= min && rnd <= max);
#ifdef INT32_MAX GUF_ASSERT(rnd <= GUF_INT32_MAX && rnd >= GUF_INT32_MIN);
GUF_ASSERT(rnd <= INT32_MAX && rnd >= INT32_MIN);
#endif
return rnd; return rnd;
} }
@ -787,9 +783,8 @@ GUF_RAND_KWRDS int_fast64_t guf_rand64_range_i64(guf_rand64_state *state, int_fa
} while (step >= limit); } while (step >= limit);
step = GUF_UWRAP_64(step / bin_size); step = GUF_UWRAP_64(step / bin_size);
#ifdef INT64_MAX GUF_ASSERT(guf_ckd_add_int_fast64_t(min, step) == GUF_MATH_CKD_SUCCESS);
GUF_ASSERT(guf_ckd_add_i64(min, step) == GUF_MATH_CKD_SUCCESS);
#endif
const int_fast64_t rnd = min + (int_fast64_t)step; const int_fast64_t rnd = min + (int_fast64_t)step;
GUF_ASSERT(rnd >= min && rnd <= max); GUF_ASSERT(rnd >= min && rnd <= max);
return rnd; return rnd;
@ -832,9 +827,8 @@ GUF_RAND_KWRDS int_fast64_t guf_rand32_range_i64(guf_rand32_state *state, int_fa
} while (step >= limit); } while (step >= limit);
step = GUF_UWRAP_64(step / bin_size); step = GUF_UWRAP_64(step / bin_size);
#ifdef INT64_MAX GUF_ASSERT(guf_ckd_add_int_fast64_t(min, step) == GUF_MATH_CKD_SUCCESS);
GUF_ASSERT(guf_ckd_add_i64(min, step) == GUF_MATH_CKD_SUCCESS);
#endif
const int_fast64_t rnd = min + (int_fast64_t)step; const int_fast64_t rnd = min + (int_fast64_t)step;
GUF_ASSERT(rnd >= min && rnd <= max); GUF_ASSERT(rnd >= min && rnd <= max);
return rnd; return rnd;