contributed by < ccs100203
>
linux2020
reference
根據 c99 6.5.2.4.2
The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point.
可以看出第三行印出位置後,ptr 才會做 + 1,並且在下一次的 output 體現出來。
由於 ++
的 precedence 高於 *
,所以可以理解為 *(ptr++)
可以看出第四行先執行 ptr++
後,會對 0x9b317844 做 dereference,在 printf
結束後再把 ptr + 1
,所以可以看到第五行的 ptr
已經指到了下一個位置。
signal()
void (*handler)(int)
是一個 function pointer,傳入 int 然後 return void.signal(int sig, void (*handler)(int))
是一個 function,傳入 int sig, void (*handler)(int) 並 ruturn 一個 function pointer.void (*fp) (int);
, 可以看出最後我們會得到一個 function pointer.This macro will iterate over a list backwards.
pos
is a loop cursor.head
is the head of list.If we don't use parenthesis, we will suffer some problems.
e.g. list_for_each_prev(pos, head + 1)
If I pass head + 1
into the macro, but I don't use parenthesis. Because of the operator precedence, it's execution order is (head + (1 -> prev))
, and the compiler will report the error.
在 lab0 q_sort() 時有實作過 merge sort。
e.g. Sorted() is a function in python, which uses Timsort as its algorithm, and Timsort is a hybrid algorithm of insertion sort and merge sort.
當我所存取的資料位置不在 4 的倍數上時,processor 會先讀取上半邊的區塊,篩掉不需要的資料,再讀取下半邊的區塊,一樣過濾掉不需要的資料,最後再將兩者整合再一起,完成一次的存取。很明顯的這樣多了一次的操作,於是就會對效能產生影響。
Atomic instruction 是不可分割的,當 processor 執行 atomic instruction 時,如果所需存取的資料存放在不同的 page 上,又其中一個 page 在記憶體裡,而另一個不在。此時會發生 page fault,並且執行 virtual memory management。這樣會導致 atomicity 被破壞,正確性會有疑慮。 e.g. PowerPC 上要求 atomic instruction 存取的資料至少要對齊 4 byte。
參考 Data alignment: Straighten up and fly right
number ^ 1
會是 Toggle,而 number ^ 0
不會有任何改變。(num >> 63)
可以做出全部都是 1 或 0 的 mask
((num >> 63) ^ num)
(num >> 63)
g()
對 global_var
做 dereference,代表 f
內的資料需要保存而不能提前從 stack pop。g()
沒有對 global_var
做 dereference,那麼就有機會做 TCO.將小數點的位置固定,以固定量的 bit 數目表達整數與小數的部分。
優點是運算快速省效能,缺點是表達的範圍很小。
以 IEEE 754 single-precision 為標準,他所能表達的數值範圍比較廣,但缺點是在極大與極小值的精度較差,且計算效率較差。
https://hackmd.io/@cccccs100203/linux2020-quiz3#測驗-4
無失真資料壓縮為: 資料經過壓縮後,資訊不被破壞,還能完全恢復到壓縮前的原樣。
參考 Conditional Execution,藉由設立 Processor flags 去決定下面的指令是否需要執行,此方法可以移除所需要的 branch,並且避免 pipeline stall。
Take the matrix transpose in lab7 of Computer Architecture for example:
It compares two situations: careful locality (cache blocking) and careless locality (naive method)
The careful locality program got a better performance.