Skip to content

Commit

Permalink
ICU-22528 Avoid unnecessary float division
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram committed Oct 11, 2023
1 parent aeef654 commit 6e8182a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions icu4c/source/i18n/gregoimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ int64_t ClockMath::floorDivide(int64_t numerator, int64_t denominator) {
numerator / denominator : ((numerator + 1) / denominator) - 1;
}

int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator,
int32_t* remainder) {
auto quotient = floorDivide(numerator, denominator);
if (remainder != nullptr) {
*remainder = numerator - (quotient * denominator);
}
return quotient;
}

int32_t ClockMath::floorDivide(double numerator, int32_t denominator,
int32_t* remainder) {
// For an integer n and representable ⌊x/n⌋, ⌊RN(x/n)⌋=⌊x/n⌋, where RN is
Expand Down
18 changes: 18 additions & 0 deletions icu4c/source/i18n/gregoimp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ class ClockMath {
*/
static inline double floorDivide(double numerator, double denominator);

/**
* Divide two numbers, returning the floor of the quotient and
* the modulus remainder. Unlike the built-in division, this is
* mathematically well-behaved. E.g., <code>-1/4</code> => 0 and
* <code>-1%4</code> => -1, but <code>floorDivide(-1,4)</code> =>
* -1 with <code>remainder</code> => 3. NOTE: If numerator is
* too large, the returned quotient may overflow.
* @param numerator the numerator
* @param denominator a divisor which must be != 0
* @param remainder output parameter to receive the
* remainder. Unlike <code>numerator % denominator</code>, this
* will always be non-negative, in the half-open range <code>[0,
* |denominator|)</code>.
* @return the floor of the quotient
*/
static int32_t floorDivide(int32_t numerator, int32_t denominator,
int32_t* remainder);

/**
* Divide two numbers, returning the floor of the quotient and
* the modulus remainder. Unlike the built-in division, this is
Expand Down

0 comments on commit 6e8182a

Please sign in to comment.