403 views
 owned this note
1. 重寫以下merge sort的工具函式,讓branch predictor得以發揮更好的效能,至少在Intel x86架構下發揮作用。改寫後,請一併說明為何得以提昇效能。 提示:while敘述內的條件分支 ```C void merge(int src1[], int src2[], int dst[], int n) { int i1 = 0, i2 = 0, id = 0; while (i1 < n && i2 < n) { if (src1[i1] < src2[i2]) dst[id++] = src1[i1++]; else dst[id++] = src2[i2++]; } while (i1 < n) dst[id++] = src1[i1++]; while (i2 < n) dst[id++] = src2[i2++]; } ``` 2. Suppose a system provides the following atomic hardware instruction: ```C boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv; } ``` Write C code to implement counting semaphore.