VVang
    • 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
    # Assignment2: RISC-V Toolchain (GCD) ###### tags: `RISC-V` > Rewrite assignment from [陳柏廷](https://hackmd.io/@Max-Chen/SJ4sTXoBD) Since `__modsi3` is undefined in compiler. I'll try another algorithm(Binary GCD) for GCD. ```c= #include <stdio.h> int GCD (int x, int y) { int xy_odd_ornot; int i = 0; while (x ^ y) { xy_odd_ornot = (x & 1) & (y & 1); if (!xy_odd_ornot) { i += (~x & 1) & (~y & 1); x >>= ~x & 1; y >>= ~y & 1; } else { int z = x - y; x = y; y = (z ^ (z>>31)) - (z>>31); } } return x << i; } void _start() { int res = GCD(24, 16); } ``` ## -O3 `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib GCD.c -o GCD` ``` ./emu-rv32i GCD >>> Execution time: 3300 ns >>> Instruction count: 99 (IPS=30000000) >>> Jumps: 8 (8.08%) - 2 forwards, 6 backwards >>> Branching T=2 (16.67%) F=10 (83.33%) ``` ##### :::spoiler `riscv-none-embed-objdump -d GCD` ```cpp= 00010054 <GCD>: 10054: 00000313 li t1,0 10058: 06b50463 beq a0,a1,100c0 <GCD+0x6c> 1005c: 40b507b3 sub a5,a0,a1 10060: 41f7d613 srai a2,a5,0x1f 10064: 00b56733 or a4,a0,a1 10068: 00b576b3 and a3,a0,a1 1006c: 00f64633 xor a2,a2,a5 10070: fff54893 not a7,a0 10074: fff5c813 not a6,a1 10078: fff74713 not a4,a4 1007c: 01f7d793 srli a5,a5,0x1f 10080: 0016f693 andi a3,a3,1 10084: 0018f893 andi a7,a7,1 10088: 00187813 andi a6,a6,1 1008c: 00177713 andi a4,a4,1 10090: 00f607b3 add a5,a2,a5 10094: 00069e63 bnez a3,100b0 <GCD+0x5c> 10098: 41155533 sra a0,a0,a7 1009c: 4105d5b3 sra a1,a1,a6 100a0: 00e30333 add t1,t1,a4 100a4: fab51ce3 bne a0,a1,1005c <GCD+0x8> 100a8: 00651533 sll a0,a0,t1 100ac: 00008067 ret 100b0: 00058513 mv a0,a1 100b4: feb78ae3 beq a5,a1,100a8 <GCD+0x54> 100b8: 00078593 mv a1,a5 100bc: fa1ff06f j 1005c <GCD+0x8> 100c0: 00008067 ret 000100c4 <_start>: 100c4: 01000693 li a3,16 100c8: 01800713 li a4,24 100cc: 40d70633 sub a2,a4,a3 100d0: fff6c793 not a5,a3 100d4: 00e6f533 and a0,a3,a4 100d8: 41f65813 srai a6,a2,0x1f 100dc: fff74593 not a1,a4 100e0: 0017f793 andi a5,a5,1 100e4: 00157513 andi a0,a0,1 100e8: 00c84833 xor a6,a6,a2 100ec: 0015f593 andi a1,a1,1 100f0: 01f65613 srli a2,a2,0x1f 100f4: 40f6d7b3 sra a5,a3,a5 100f8: 00051c63 bnez a0,10110 <_start+0x4c> 100fc: 40b75733 sra a4,a4,a1 10100: 00e78663 beq a5,a4,1010c <_start+0x48> 10104: 00078693 mv a3,a5 10108: fc5ff06f j 100cc <_start+0x8> 1010c: 00008067 ret 10110: 00c807b3 add a5,a6,a2 10114: 00d78863 beq a5,a3,10124 <_start+0x60> 10118: 00068713 mv a4,a3 1011c: 00078693 mv a3,a5 10120: fadff06f j 100cc <_start+0x8> 10124: 00008067 ret ``` ::: ##### :::spoiler `riscv-none-embed-readelf -h GCD` ``` ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x100c4 Start of program headers: 52 (bytes into file) Start of section headers: 696 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 1 Size of section headers: 40 (bytes) Number of section headers: 6 Section header string table index: 5 ``` ::: ##### `riscv-none-embed-size GCD` ``` text data bss dec hex filename 212 0 0 212 d4 GCD ``` ## -Os `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -Os -nostdlib GCD.c -o GCD` ``` ./emu-rv32i GCD >>> Execution time: 5200 ns >>> Instruction count: 106 (IPS=20384615) >>> Jumps: 16 (15.09%) - 7 forwards, 9 backwards >>> Branching T=7 (53.85%) F=6 (46.15%) ``` ##### :::spoiler `riscv-none-embed-objdump -d GCD` ```cpp= 00010054 <GCD>: 10054: 00000713 li a4,0 10058: 00b51663 bne a0,a1,10064 <GCD+0x10> 1005c: 00e51533 sll a0,a0,a4 10060: 00008067 ret 10064: 00b577b3 and a5,a0,a1 10068: 0017f793 andi a5,a5,1 1006c: 02079c63 bnez a5,100a4 <GCD+0x50> 10070: 00b567b3 or a5,a0,a1 10074: fff7c793 not a5,a5 10078: 0017f793 andi a5,a5,1 1007c: 00f70733 add a4,a4,a5 10080: fff54693 not a3,a0 10084: fff5c793 not a5,a1 10088: 0016f693 andi a3,a3,1 1008c: 0017f793 andi a5,a5,1 10090: 40f5d7b3 sra a5,a1,a5 10094: 40d555b3 sra a1,a0,a3 10098: 00058513 mv a0,a1 1009c: 00078593 mv a1,a5 100a0: fb9ff06f j 10058 <GCD+0x4> 100a4: 40b50533 sub a0,a0,a1 100a8: 41f55793 srai a5,a0,0x1f 100ac: 00a7c7b3 xor a5,a5,a0 100b0: 01f55513 srli a0,a0,0x1f 100b4: 00a787b3 add a5,a5,a0 100b8: fe1ff06f j 10098 <GCD+0x44> 000100bc <_start>: 100bc: 01000593 li a1,16 100c0: 01800513 li a0,24 100c4: f91ff06f j 10054 <GCD> ``` ::: ##### ##### :::spoiler `riscv-none-embed-readelf -h GCD` ``` ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x100bc Start of program headers: 52 (bytes into file) Start of section headers: 600 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 1 Size of section headers: 40 (bytes) Number of section headers: 6 Section header string table index: 5 ``` ::: ##### `riscv-none-embed-size GCD` ``` text data bss dec hex filename 116 0 0 116 74 GCD ``` | | O0 | O1 | O2 | O3 | Os | | -------- | -------- | -------- | -------- | -------- | -------- | | **Ex. time** | 10200ns | 4500ns | 3800ns | 3300ns | 4900ns | | **IC** | 240 | 94 | 115 | 99 | 106 | | **Jumps** | 16 (6.67%) 8for, 8back | 8 (6.96%) 1for, 7back | 8 (6.96%) 1for, 7back | 8 (8.08%) 2for, 6back | 16 (15.09%) 7for, 9back | | **Branching** | T=7(53.85%) F=6(46.15%) | T=2(15.38%) F=11(84.62%) | T=6(46.15%) F=7(53.85%) | T=2(16.67%) F=10(83.33%) | T=7(53.85%) F=6(46.15%) | > The fluctuating range of Ex. time is violent, it won't be used for reference.

    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