李俊德
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 進階電腦系統理論與實作-第一週 ## 課程筆記 1. C語言中 Addition/subtraction operator 的優先度大於 bitwise shift operator 的優先度 2. - [x] 以位元運算子實作 x == y 等價的表示 直覺認為答案是︰==!(x ^ y)== 但用到 logical operator `not "!"`不知道算嗎。 `!(x ^ y)`和`(x ^ y)!=0`的行為不同嗎,相同的話那這樣寫有意義嗎 解答︰這題的意義是熟悉硬體行為 3. `x * 31` 等同 `(x << 5) - x` `x * (-6)` 等同 `(~((x << 3)-(X << 1)))+1` 4. clz應用 - **在浮點數除法的優化** ==在 32-bit float point 除法中,需要走訪全部 32-bit 的數值做運算,可以利用CLZ判斷並跳過前面為0的位元即可加速== - **乘法的溢位偵測** ==由clz(x) + clz(y)可看出是否溢位== - **計算以2為底數的對數值** ==log2(x)= 31-clz(x) [取 lowerbound (floor log2(x))] log2(x)= 32-clz(x) [取 upperbound (ceiling log2(x))]== 5. concurrency 同時執行不相關的任務 6. parallelism 同時執行一樣的任務 7. printf 16進位數值:printf("%x", x); 8. - [x] c語言中 ``` char s[16]; memcpy(s, "0123456789abcde", sizeof s); printf("%s\n", s); printf("%x\n", s); has type ‘char *’ printf("%x\n", &s); has type ‘char (*)[16]’ printf("%c\n", *s); correct printf("%c\n", *(&s)); incorrect printf("%c\n", **(&s)); correct ``` 疑問︰s 和 &s 印出來結果為什麼一樣。 答案︰ One neat feature of C is that, in most places, when you use the name array again, you will actually be using a pointer to its first element (in C terms, &array[0]). This is called “decaying”: the array “decays” to a pointer. Most usages of array are equivalent to if array had been declared as a pointer. There are, of course, cases that aren't equivalent. One is assigning to the name array by itself (array = …)—that's illegal. Another is passing it to the sizeof operator. The result will be the total size of the array, not the size of a pointer (for example, sizeof(array) using the array above would evaluate to (sizeof(int) = 4) × 3 = 12 on a current Mac OS X system). This illustrates that you are really handling an array and not merely a pointer. In most uses, however, array expressions work just the same as pointer expressions. So, for example, let's say you want to pass an array to printf. You can't: When you pass an array as an argument to a function, you really pass a pointer to the array's first element, because the array decays to a pointer. You can only give printf the pointer, not the whole array. (This is why printf has no way to print an array: It would need you to tell it the type of what's in the array and how many elements there are, and both the format string and the list of arguments would quickly get confusing.) Decaying is an implicit &; array == &array == &array[0]. In English, these expressions read “array”, “pointer to array”, and “pointer to the first element of array” (the subscript operator, [], has higher precedence than the address-of operator). But in C, all three expressions mean the same thing. (They would not all mean the same thing if “array” were actually a pointer variable, since the address of a pointer variable is different from the address inside it—thus, the middle expression, &array, would not be equal to the other two expressions. The three expressions are all equal only when array really is an array.) 9. - [ ] 為什麼可以執行 ``` #include <stdio.h> int main() { return (*******puts)("Hello"); } ``` 找時間去看規格書 答案︰ - C99 [ 6.3.2.1 ] A function designator is an expression that has function type - Except when it is the operand of the sizeof operator or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’. - * is Right associative operator - C99 [6.5.1 ] It is an lvalue, a function designator, or avoid expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or avoid expression. - C99 [6.5.3.2-4] The unary * operator denotes indirection. If the operand points to a function, the result is a function designator 10編譯器優化造成的問題 ``` int main() { long double d; char s[sizeof d]; memcpy(&d, "0123456789abcde", sizeof d); memcpy(s, &d, sizeof s); printf("%s\n", s); } gcc -std=c11 -pedantic -Wall -Wextra -O0 memcpy.c gcc -std=c11 -pedantic -Wall -Wextra -O3 memcpy.c 某些版本編譯器可能造成結果不同 ``` 11. 好的程式碼 - [ Correct ] : 做到預期的行為了嗎?能夠處理各式邊際狀況嗎?即便其他人修改程式碼後,主體的行為仍符合預期嗎? - [ Secure ] : 面對各式輸入條件或攻擊,程式仍可正確運作嗎? - [ Readable ] : 程式碼易於理解和維護嗎? - [ Elegant ] : 程式碼夠「美」嗎?可以簡潔又清晰地解決問題嗎? - [ Altruist ] : 除了滿足現有的狀況,軟體在日後能夠重用嗎?甚至能夠抽離一部分元件,給其他專案使用嗎? 12. bit-wise operator 避免 overflow ``` (x+y)/2 從硬體加法器實作來思考 x+y: (x&y)<<1 //進位 (x^y) //相加 (x+y)/2: (((x&y)<<1) + (x^y)) >> 1 (x&y) + ((x^y) >> 1) ``` 13. 偉大 Git commit message 的七條規則 1. 用一行空白行分隔標題與內容 2. 限制標題最多只有 50 字元 3. 標題開頭要大寫 4. 標題不以句點結尾 5. 以祈使句撰寫標題 6. 內文每行最多 72 字 7. 用內文解釋 what 以及 why vs. how 14. clz迴圈版本 ``` int clz(uint32_t x) { int n = 32, c = 16; do { uint32_t y = x >> c; if (y) { n -= c; x = y; } c >>= 1; } while (c); return (n - x); } ``` 這樣會有一個問題,就是可以利用執行時間長短來猜測程式行為,像是 0x80000000 和 0x00000001 的執行時間會差很多,利用大量數值分佈來猜測可能會有資訊安全的隱憂。所以下面改用常數時間的clz版本 ``` int clz(uint32_t x) { int n=0; if ((x & 0xFFFF0000) == 0){ n = n + 16; x = x << 16;} //16 left bits are 0 if ((x & 0xFF000000) == 0){ n = n + 8; x = x << 8;} // 8 left bits are 0 if ((x & 0xF0000000) == 0){ n = n + 4; x = x << 4;} // 4 left bits are 0 if ((x & 0xC0000000) == 0){ n = n + 2; x = x << 2;} // 2 left bits are 0 if ((x & 0x80000000) == 0){ n = n + 1; x = x << 1;} // 1 left bits are 0 if ((x & 0x80000000) == 0){ n = n + 1; x = x << 1;} // 1 left bits are 0 return n; } ``` ## 作業題目 - [ternary](https://hackmd.io/s/Sym0UAk9Z) - [phonebook](https://hackmd.io/s/HJJUmdXsZ) - [clz](https://hackmd.io/s/Hyl9-PrjW) ## 我的作業連結 - [ternary](https://hackmd.io/AwYwnARgrCAmCGBaKYAsZGoMwA4BMiEWAZgGyICMFApgOxVjzXVjFA==) ## 參考資料 - [課程說明投影片](https://docs.google.com/presentation/d/1-6ZlaL3vd-a54omTpcTQPudbpxz-eqisyd7PY4e_Ctc/edit#) - [如何寫一個 Git Commit Message](https://blog.louie.lu/2017/03/21/%E5%A6%82%E4%BD%95%E5%AF%AB%E4%B8%80%E5%80%8B-git-commit-message/#rules02) - [CLZ application and improvement](https://hackmd.io/s/S153n-G1g) - [Everything you need to know about pointers in C](http://boredzo.org/pointers/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully