目的: 檢驗學員對 bitwise operation 的認知
作答表單 (針對 Linux 核心「實作」課程)
1
延伸〈Linux 核心原始程式碼的整數除法〉和第 7 週測驗 2
,fastdiv (部分程式碼遮蔽) 提出快速除法實作。以下是測試程式碼:
#include <assert.h>
#include <stdio.h>
#include "fastdiv.h"
int main(int argc, char **argv)
{
const uint64_t MAX_NUM = 1 << 16;
const uint64_t MAX_DIV = 1 << 14;
for (uint64_t d = 1U; d < MAX_DIV; ++d) {
div_t fd; /* general method */
fastdiv(&fd, d); /* divide-by-d */
const uint64_t mul = fd.mul, add = fd.add, shift = fd.shift;
div_t fd2; /* bounded method optimization */
fastdiv_bounded(&fd2, d, MAX_DIV); /* divide-by-d */
const uint64_t mul2 = fd2.mul, add2 = fd2.add, shift2 = fd2.shift;
assert(mul2 == 1U || add2 == 0U || shift2 == 0U);
assert(add2 == 0U);
for (uint64_t num = 0; num < MAX_NUM; ++num) {
const uint64_t normal = num / d;
const uint64_t general = (num * mul + add) >> shift;
const uint64_t bounded = (num * mul2) >> shift2;
assert(general == normal);
assert(bounded == normal);
}
}
}
請補完程式碼,使 assert
巨集不會在執行時期觸發錯誤並運作符合預期。作答規範:
AAAA
, BBBB
, CCCC
, DDDD
皆為整數(
和 )
),以最精簡的形式書寫延伸問題:
fastdiv
的效率,並試圖解釋其行為