[英]Fast floor of a signed integer division in C / C++
在 C 中可以進行樓層划分,例如:
int floor_div(int a, int b) {
int d = a / b;
if (a < 0 != b < 0) { /* negative output (check inputs since 'd' isn't floored) */
if (d * a != b) { /* avoid modulo, use multiply instead */
d -= 1; /* floor */
}
}
return d;
}
但這似乎可以簡化。
在 C 中有沒有更有效的方法來做到這一點?
請注意,這幾乎與這個問題相反: C/C++ 中整數除法的快速上限
生成的代碼中的匯編指令更少,我認為獲得結果的路徑更快。
對於具有大量寄存器的 RISC 機器,這個更好,因為根本沒有分支,而且它有利於管道和緩存。
對於 x86 實際上沒有關系。
int floor_div3(int a, int b) {
int d = a / b;
return d * b == a ? d : d - ((a < 0) ^ (b < 0));
}
div()
函數我認為您應該查看<stdlib.h>
中的div()
函數。 (它們是標准的 C 函數,並且在標准的所有版本中都有定義,盡管鏈接到 POSIX 規范。)
C11 標准 §7.22.6.2 規定:
div
... 函數在單個操作中計算numer / denom
和numer % denom
。
請注意,C11 在 §6.5.5 中指定了整數除法(和 C99 類似):
當整數被除時,
/
運算符的結果是代數商,其中任何小數部分都被丟棄。 105)105)這通常被稱為“向零截斷”。
但是 C90(第 6.3.5 節)更靈活但不太有用:
當整數相除且除法不精確時。 如果兩個操作數都是正數,則
/
運算符的結果是小於代數商的最大整數,並且%
運算符的結果是正數。 如果任一操作數為負,則/
運算符的結果是小於或等於代數商的最大整數還是大於或等於代數商的最小整數是實現定義的,結果的符號也是%
運算符。
floor_div()
使用div()
請求的floor_div()
的計算代碼整潔。
int floor_div(int a, int b)
{
assert(b != 0);
div_t r = div(a, b);
if (r.rem != 0 && ((a < 0) ^ (b < 0)))
r.quot--;
return r.quot;
}
下面代碼中的打印格式是根據示例數據精確定制的。 (在整個過程中使用%4d
和%-4d
會更好,但范圍%-4d
)。 此代碼打印長度為 89 個字符的行加上換行符; 更一般的布局將打印長度為 109 的行。兩者都避免了 SO 上的水平滾動條。
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int floor_div(int a, int b)
{
assert(b != 0);
div_t r = div(a, b);
if (r.rem != 0 && ((a < 0) ^ (b < 0)))
r.quot--;
return r.quot;
}
static void test_floor_div(int n, int d)
{
assert(d != 0);
printf( "%3d/%-2d = %-3d (%3d)", +n, +d, floor_div(+n, +d), +n / +d);
printf("; %3d/%-3d = %-4d (%4d)", +n, -d, floor_div(+n, -d), +n / -d);
if (n != 0)
{
printf("; %4d/%-2d = %-4d (%4d)", -n, +d, floor_div(-n, +d), -n / +d);
printf("; %4d/%-3d = %-3d (%3d)", -n, -d, floor_div(-n, -d), -n / -d);
}
putchar('\n');
}
int main(void)
{
int numerators[] = { 0, 1, 2, 4, 9, 23, 291 };
enum { NUM_NUMERATORS = sizeof(numerators) / sizeof(numerators[0]) };
int denominators[] = { 1, 2, 3, 6, 17, 23 };
enum { NUM_DENOMINATORS = sizeof(denominators) / sizeof(denominators[0]) };
for (int i = 0; i < NUM_NUMERATORS; i++)
{
for (int j = 0; j < NUM_DENOMINATORS; j++)
test_floor_div(numerators[i], denominators[j]);
putchar('\n');
}
return 0;
}
0/1 = 0 ( 0); 0/-1 = 0 ( 0)
0/2 = 0 ( 0); 0/-2 = 0 ( 0)
0/3 = 0 ( 0); 0/-3 = 0 ( 0)
0/6 = 0 ( 0); 0/-6 = 0 ( 0)
0/17 = 0 ( 0); 0/-17 = 0 ( 0)
0/23 = 0 ( 0); 0/-23 = 0 ( 0)
1/1 = 1 ( 1); 1/-1 = -1 ( -1); -1/1 = -1 ( -1); -1/-1 = 1 ( 1)
1/2 = 0 ( 0); 1/-2 = -1 ( 0); -1/2 = -1 ( 0); -1/-2 = 0 ( 0)
1/3 = 0 ( 0); 1/-3 = -1 ( 0); -1/3 = -1 ( 0); -1/-3 = 0 ( 0)
1/6 = 0 ( 0); 1/-6 = -1 ( 0); -1/6 = -1 ( 0); -1/-6 = 0 ( 0)
1/17 = 0 ( 0); 1/-17 = -1 ( 0); -1/17 = -1 ( 0); -1/-17 = 0 ( 0)
1/23 = 0 ( 0); 1/-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0)
9/23 = 0 ( 0); 9/-23 = -1 ( 0); -9/23 = -1 ( 0); -9/-23 = 0 ( 0)
23/1 = 23 ( 23); 23/-1 = -23 ( -23); -23/1 = -23 ( -23); -23/-1 = 23 ( 23)
23/2 = 11 ( 11); 23/-2 = -12 ( -11); -23/2 = -12 ( -11); -23/-2 = 11 ( 11)
23/3 = 7 ( 7); 23/-3 = -8 ( -7); -23/3 = -8 ( -7); -23/-3 = 7 ( 7)
23/6 = 3 ( 3); 23/-6 = -4 ( -3); -23/6 = -4 ( -3); -23/-6 = 3 ( 3)
23/17 = 1 ( 1); 23/-17 = -2 ( -1); -23/17 = -2 ( -1); -23/-17 = 1 ( 1)
23/23 = 1 ( 1); 23/-23 = -1 ( -1); -23/23 = -1 ( -1); -23/-23 = 1 ( 1)
291/1 = 291 (291); 291/-1 = -291 (-291); -291/1 = -291 (-291); -291/-1 = 291 (291)
291/2 = 145 (145); 291/-2 = -146 (-145); -291/2 = -146 (-145); -291/-2 = 145 (145)
291/3 = 97 ( 97); 291/-3 = -97 ( -97); -291/3 = -97 ( -97); -291/-3 = 97 ( 97)
291/6 = 48 ( 48); 291/-6 = -49 ( -48); -291/6 = -49 ( -48); -291/-6 = 48 ( 48)
291/17 = 17 ( 17); 291/-17 = -18 ( -17); -291/17 = -18 ( -17); -291/-17 = 17 ( 17)
291/23 = 12 ( 12); 291/-23 = -13 ( -12); -291/23 = -13 ( -12); -291/-23 = 12 ( 12)
可以通過使用除法和取模來執行地板除法。
沒有理由避免模調用,因為現代編譯器將除法和模法優化為單個除法。
int floor_div(int a, int b) {
int d = a / b;
int r = a % b; /* optimizes into single division. */
return r ? (d - ((a < 0) ^ (b < 0))) : d;
}
“底除法”的余數要么是 0,要么與除數具有相同的符號。
(the proof)
a: dividend b: divisor
q: quotient r: remainder
q = floor(a/b)
a = q * b + r
r = a - q * b = (a/b - q) * b
~~~~~~~~~
^ this factor in [0, 1)
幸運的是,C/C++ 中/
和%
的結果在 C99/C++11 之后被標准化為“向零截斷”。 (在此之前,C 中的庫函數div
和 C++ 中的std::div
扮演着相同的角色)。
我們比較一下“floor 除法”和“truncate 除法”,關注余數的范圍:
"floor" "truncate"
b>0 [0, b-1] [-b+1, b-1]
b<0 [b+1, 0] [b+1, -b-1]
為討論方便:
假設 b>0,不幸的是,r0 在 [-b+1, -1] 中。 然而,我們可以很容易地得到 r:r = r0+b,並且 r 保證在 [1, b-1] 中,它在“floor”范圍內。 對於 b<0 的情況也是如此。
既然我們可以修復余數,我們也可以修復商。 規則很簡單:我們將 b 添加到 r0,然后我們必須從 q0 中減去 1。
作為結尾,在 C++11 中實現“樓層划分”:
void floor_div(int& q, int& r, int a, int b)
{
int q0 = a / b;
int r0 = a % b;
if (b > 0){
q = r0 >= 0 ? q0 : q0 - 1;
r = r0 >= 0 ? r0 : r0 + b;
}
else {
q = r0 <= 0 ? q0 : q0 - 1;
r = r0 <= 0 ? r0 : r0 + b;
}
}
與著名的(a < 0) ^ (b < 0)
方法相比,這種方法有一個優點:如果除數是編譯時常量,則只需進行一次比較即可修復結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.