Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section 3.5.1 "Transient Execution": show an example of cpu speculation that is not based on branch prediction #177

Open
kbeyls opened this issue Feb 22, 2023 · 2 comments
Labels
content New content for the book

Comments

@kbeyls
Copy link
Member

kbeyls commented Feb 22, 2023

See TODO in text.

@kbeyls kbeyls added the content New content for the book label Feb 22, 2023
@koutheir
Copy link
Contributor

Microarchitectural branches can also participate in speculation. The following function, for example, divides a floating-point constant by its input, and that input could be zero:

double divide_a_value(double a) {
    return 3.0 / a;
}
; AArch64 disassembly
divide_a_value(double):
        fmov    d1, #3.00000000
        fdiv    d0, d1, d0
        ret
  • If that input value is zero, then the result of the division will be Not a Number (NaN), and the CPU might trap. Trapping causes a saving of the execution state, then a branch to a trap handler.
    On POSIX systems, the trap can cause the handler of the SIGFPE signal to be executed.
  • If that input value is not zero, then the division can proceed without traps.

@koutheir
Copy link
Contributor

koutheir commented Jul 15, 2024

Computing addresses may take a few instructions to perform (so, multiple cycles). When loading a value from a computed address for the second time, the CPU may speculate that a computed address used to load a value remains the same, allowing it to start loading the value, this second time, from the previous address, before the actual address, that should be used, is computed. Once the address computation is finished, it is compared to the speculated address, and if it is the same, then the speculatively-loaded value can be used directly. Otherwise, a new load is started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content New content for the book
Projects
None yet
Development

No branches or pull requests

2 participants