H

Hiroshi Hayashi

h14s.p5r.org · Personal blog · on HN since 2014

Hiroshi Hayashi writes technical posts on low-level math and programming, including the fast inverse square root, IEEE 754, parsing, and varints.

What HN saysCommenters point to his 2012 explanation of 0x5f3759df as the best they have seen. One reader also praises his Forth-based parser work.
20HN people shared it
7linked it in comments
16stories submitted
655story points
1 year agolast shared
2014people per year2026

Most-shared posts

  1. 1
  2. 2
    0x5f3759df
    2 people · 34 points · first shared 2017 · 1 comments on HN
  3. 3
    Shift/Reduce Expression Parsing
    2 people · 21 points · first shared 2014 · 2 comments on HN
  4. 4
    Why I find IEEE 754 frustrating
    1 people · 140 points · first shared 2014 · 58 comments on HN
  5. 5
    Bitwise Division
    1 people · 4 points · first shared 2023 · discussion on HN
  6. 6
    Bigger Better Varints
    1 people · 2 points · first shared 2024 · 1 comments on HN
  7. 7
    0x5f3759df appendix
    1 people · first shared 2017
  8. 8
    Bernoulli
    1 people · first shared 2016
  9. 9
    Punched cards
    1 people · first shared 2018
  10. 10
    Z quads strings
    1 people · first shared 2014
  11. 11
    Z quads conversions
    1 people · first shared 2014

What HN people wrote when they linked it

plesner · 2016-08-01
> L1. There are gaps out there. I had a similar experience. At one point I decided that I wanted to understand, step by step, how Ada Lovelace's Bernoulli program works[1]. There was a few steps that didn't seem right to me but I was sure if there were any bugs in there they would be well-known and mentioned somewhere I could find. This being one of the most iconic and historically significant programs ever written. Indeed I found more than one person claiming that the program was known to be correct. It took a long time before I could believe that it wasn't just me not getting it, there were indeed a few things out of place and apparently there was just no mention of it anywhere on the web or in any of the research into the analytical engine I could get my hands on. [1]: http://h14s.p5r.org/2012/12/bernoulli.html
forgotpwtomain · 2016-03-09
Carmack also started a rocket company at a time when it was much more difficult to do so (there were far fewer government dollars available via contracts), Bezos also has far more capital to put in than Carmack. Not undeservingly Carmack is something of a folk hero here (for example: http://h14s.p5r.org/2012/09/0x5f3759df.html) but I don't think being a 10X programmer necessarily translates into great rocket company CEO and in terms of entrepreneurial and organizational skill Bezos excels (at least based on Amazon's success).
timdierks · 2015-12-22
Here's the best explanation I've seen: http://h14s.p5r.org/2012/09/0x5f3759df.html
trealira · 2025-05-18
Thanks for sharing that. I've actually been learning some Forth recently and had been wondering how a parser or a compiler might actually look using Forth, and you actually already made one, which is pretty cool. I was wondering this because deterministic parsers are often already stack based (LL and LR parsers can both be implemented as imperative programs that manipulate a stack based on the input tokens, or just recursive descent). I still can't really understand everything just from reading the code, but I had assumed it would be some kind of explicitly stack-based state machine precedence parser, like this article describes: https://h14s.p5r.org/2014/10/shiftreduce-expression-parsing-by-douglas… Kind of looks like you did that in expr.fs? Not completely sure, though.
Greek0 · 2020-11-02
Here's another article (from 2012) describing the same calculation: http://h14s.p5r.org/2012/09/0x5f3759df.html
lemoncucumber · 2017-08-16
Be sure to read the appendix, which is an easy-to-miss link at the end: http://h14s.p5r.org/2012/09/0x5f3759df-appendix.html The appendix was by far the most interesting part to me, after reading it I felt like I really got the intuition behind how the trick works.
nwallin · 2025-08-24
1/sqrt(x) is complicated. Imagine instead of computing 1/sqrt(x), imagine instead that you wanted to compute exp_2(-.5 log_2(x)). Also imagine you have an ultra fast way to compute exp_2 and log_2. If you have an ultra fast way to compute exp_2 and log_2, then exp_2(-.5 log_2(x)) is gonna be fast to compute. It turns out you do have an ultra fast way to compute log_2: you bitcast a float to an integer, and then twiddle some bits. The first 8 bits (after the sign bit, which is obviously zero because we're assuming our input is positive) or whatever are the exponent, and the trailing 23 bits are a linear interpolation between 2^n and 2^(n+1) or whatever. exp_2 is the same but in reverse. You can simply convert the integer to floating point, multiply by -.5, then convert back to integer. But multiplying -.5 by x can be applied to a floating point operating directly on its bits, but it's more complicated. You'll need to do some arithmetic, and some magic numbers. So you're bitcasting to an integer, twiddling some bits, twiddling some bits, twiddling some bits, twiddling some bits, and bitcasting to a float. It turns out that all the bit twiddling simplifies if you do all the legwork, but that's beyond the scope of this post. So there you go. You've computed exp_2(-.5 log_2 x). You're done. Now you need to figure out how to apply that knowledge to the inverse square root. It just so happens that 1/sqrt(x) and exp(-.5 log x) are the same function. exp(-.5 log x) =…

Keyboard shortcuts

/Search
j kNext / previous blog
EnterOpen selected blog
vVisit the blog site
fFollow / unfollow
lSwitch grid / list view
dSwitch dark mode
g hHome
g pTop posts
g tTopics
g fFollowing
g lLiked
g yHistory
g qFAQ
EscBack / close