---
tags: Linux Kernal
---
# 2023q1 Homework2 (quiz2)
contributed by < `POCHUN-CHEN` >
> [第 2 週測驗題](https://hackmd.io/@sysprog/linux2023-quiz2)
## 測驗 `1`
### 解析
### 延伸問題
1. 解釋程式碼原理,並用 __builtin_clzl 改寫
>int __builtin_clz (unsigned int x)
Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
int __builtin_clzl (unsigned long)
Similar to __builtin_clz, except the argument type is unsigned long.
2. 在 Linux 核心原始程式碼找出類似的使用案例並解釋
3. 當上述 clz 內建函式已運用時,編譯器能否產生對應的 x86 指令?
>提示: 可執行 cc -O2 -std=c99 -S next_pow2.c 並觀察產生的 next_pow2.s 檔案,確認 bsrq 指令 (表示 “Bit Scan Reverse”)
---
## 測驗 `2`
### 解析
### 延伸問題
1. 解釋上述程式碼運作原理
2. 嘗試使用 `__builtin_{clz,ctz,ffs}` 改寫,並改進 mod $10^9+7$ 的運算
---
## 測驗 `3`
### 解析
### 延伸問題
1. 解釋上述程式碼運作原理,比較 SWAR 和原本的實作效能落差
2. 在 Linux 核心原始程式碼找出 UTF-8 和 Unicode 相關字串處理的程式碼,探討其原理,並指出可能的改進空間
---
## 測驗 `4`
### 解析
### 延伸問題
1. 解釋上述程式碼運作原理
2. 在 Linux 核心原始程式碼找出上述 bitmask 及產生器,探討應用範疇
>參見 Data Structures in the Linux Kernel