陳履軒
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # sse2neon Contribution Team Sharing ###### tags: sse2neon --- ## What's sse2neon? ---- ### So what's intrinsic in computer science? - Intrinsic, or formally, intrinsic functions, is functions with their implementations are handled specially by the compiler [^8]. - Usually, intrinsic implements high-performance code. [^8]: https://en.wikipedia.org/wiki/Intrinsic_function ---- ### What's SSE and NEON? - SSE (Streaming SIMD Extensions) is a SIMD instruction set extension on x86. - NEON is a SIMD instruction set extension on Arm Cortex-A and Arm Cortex-R series of processors. - You use SSE/NEON for digital signal processing, graphics processing, and string matching, etc. ---- ### Get back to the point, what's sse2neon? - `sse2neon` is a translator of SSE intrinsics to Arm NEON, shortening the time needed to get an Arm working program that then can be used to extract profiles and to identify hot paths in the code. [^1] - There are a lot of (open-source) projects that have adopted `sse2neon` for Arm/Aarch64 support including Apple M1 CPU. [^1]: https://github.com/DLTcollab/sse2neon --- ## My contribution - `_rdtsc` ---- ### `_rdtsc` - Read the current value of the processor’s time-stamp counter. - https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_rdtsc - Usually used for precise-time performance measurement. --- ## Interaction with Open Source Community (taking sse2neon as example) ---- ### What abilities you need - passion - C fundamentals (function pointer, macro, etc.) - compiler - SIMD fundamentals (optional) - Linux kernel fundamentals ---- ### How to make a contribution - GitHub issues - #good first issue - Talk to maintainers ---- ### How to let your work being reviewed - Read contribution guide, including: - Git commit messages - Thumb-up rules of writing C programs ---- ### Should I prepare test cases? - Of course! For unit test and CI/CD - Thumb-up rules of writing test cases - reflect the target of testing - generator (macro) to shorten lines and generalization --- ## Terms ---- ### Arm Coprocessors Arm processors can be extended with a number of coprocessors to perform non-standard calculations and to avoid having to do these calculations in software. Coprocessors are available for memory management, floating point operations, debugging, media, cryptography, ... [^4] [^4]: https://low-level.readthedocs.io/en/latest/arch/arm/ ---- ### privilege/exception level - Same term, while different name in Armv7/Armv8. - In Arm, you can set privilege level [^7] to let user not to use certain registers. - In Armv7-A, user mode Performance Monitoring Unit can be accessed by user only if the privilege is set. - Therefore, you can only use kernel API (syscall) if you are not permitted to access the registers. [^7]: https://developer.arm.com/documentation/102412/0102/Privilege-and-Exception-levels ---- ### `__asm__ __volatile__("" ::: "memory");` - It creates a compiler level memory barrier forcing optimizer to not re-ordering memory accesses across the barrier [^6]. [^6]: https://stackoverflow.com/questions/14950614/working-of-asm-volatile-memory --- ## The road of implementation ---- ### Armv8-A ```c __asm__ __volatile__("mrs %0, cntvct_el0" : "=r"(val)); ``` - Read the value from `CNTVCT_EL0` (Counter-timer Virtual Count register). [^2] [^2]: https://developer.arm.com/documentation/ddi0595/2021-03/AArch64-Registers/CNTVCT-EL0--Counter-timer-Virtual-Count-register ---- ### Armv7-A ```c // Read the user mode Performance Monitoring Unit (PMU) // User Enable Register (PMUSERENR) access permissions. __asm__ __volatile__("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren)); if (pmuseren & 1) { // Allows reading PMUSERENR for user mode code. __asm__ __volatile__("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset)); if (pmcntenset & 0x80000000UL) { // Is it counting? __asm__ __volatile__("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr)); // The counter is set up to count every 64th cycle return (uint64_t) (pmccntr) << 6; } } // Fallback to syscall (gettimeofday) as we can't enable PMUSERENR in user mode. struct timeval tv; gettimeofday(&tv, NULL); return (uint64_t) (tv.tv_sec) * 1000000 + tv.tv_usec; ``` ---- ### Armv7-A (cont.) - Read `PMCCNTR` register of a co-processor `p15` (not an actual co-processor, just an entry point for CPU functions) to obtain cycle counts. [^3] - `PMCCNTR` is only available to an unprivileged application if: 1. Unprivileged `PMCCNTR` reads are alowed: Bit 0 of `PMUSERENR` register must be set to 1. 2. `PMCCNTR` is actually counting cycles: Bit 31 of `PMCNTENSET` register must be set to 1. - If the above situation happens, use system call to get system time instead. [^3]: https://stackoverflow.com/a/40455065 ---- ### test case ```cpp uint64_t start = _rdtsc(); for (int i = 0; i < 100000; i++) __asm__ __volatile__("" ::: "memory"); uint64_t end = _rdtsc(); return end > start ? TEST_SUCCESS : TEST_FAIL; ``` - Use `__asm__ __volatile__("" ::: "memory");` to avoid compiler optimization which will eliminate the no-op for loop. --- ## Q&A --- ## Appendix ---- ### syscall -> vsyscall -> vsdo [^5] - To use some service related to OS, we can use syscall. - However, syscall requires context switch which will slow down the overall performance. Therefore, vsyscall is created to reduce the latency. - Yet vsyscall requires predictable address space, which violates the ASLR. Hence, we create vsdo for recuding the latency as well as randomizing the address space. [^5]: https://hackmd.io/@sysprog/linux-vdso <style> .reveal { font-size: 32px; } </style> <!-- ### improvement 1. explain what is intrinsic 2. what is SSE and NEON 3. give an example when you would use SSE/NEON ### Jill's feedback - 10 min max - no one knows what the heck are you talking - Armv7-A implementation - just talk about normal and syscall - maybe no need to talk about the difference of Armv7-A and Armv8-A - maybe no need to talk bout syscall term - put in appendix/backup slide - assembly go to appendix - "memory" -> sleep for a while -->

    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