Skip to content

Commit

Permalink
Publish 'division is hard'
Browse files Browse the repository at this point in the history
  • Loading branch information
purplesyringa committed Aug 24, 2024
1 parent 2146c92 commit c25bd00
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
17 changes: 16 additions & 1 deletion blog/division-is-hard-but-it-does-not-have-to-be/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
<!doctypehtml><html prefix="og: http://ogp.me/ns#"lang=en_US><meta charset=utf-8><meta content=width=device-width,initial-scale=1 name=viewport><title>Division is hard, but it doesn't have to be | purplesyringa's blog</title><link href=../../all.css rel=stylesheet><link href=../../blog.css rel=stylesheet><link href=../../vendor/Temml-Local.css rel=stylesheet><link crossorigin href=https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,100..900;1,100..900&family=Roboto+Mono:ital,wght@0,100..700;1,100..700&family=Roboto:ital,wght@0,400;0,700;1,400;1,700&family=Slabo+27px&display=swap rel=stylesheet><link href=../../fonts/webfont.css rel=stylesheet><link media="screen and (prefers-color-scheme: dark"href=../../vendor/atom-one-dark.min.css rel=stylesheet><link media="screen and (prefers-color-scheme: light"href=../../vendor/atelier-cave-light.min.css rel=stylesheet><link title="Blog posts"href=../../blog/feed.rss rel=alternate type=application/rss+xml><meta content="Division is hard, but it doesn't have to be"property=og:title><meta content=article property=og:type><meta content=https://purplesyringa.moe/blog/division-is-hard-but-it-does-not-have-to-be/og.png property=og:image><meta content=https://purplesyringa.moe/blog/division-is-hard-but-it-does-not-have-to-be/ property=og:url><meta content=dasdasdas. property=og:description><meta content=en_US property=og:locale><meta content="purplesyringa's blog"property=og:site_name><meta content=summary_large_image name=twitter:card><meta content=https://purplesyringa.moe/blog/division-is-hard-but-it-does-not-have-to-be/og.png name=twitter:image><script data-website-id=0da1961d-43f2-45cc-a8e2-75679eefbb69 defer src=https://zond.tei.su/script.js></script><body><header><div class=viewport-container><div class=media><a href=https://github.com/purplesyringa><img src=../../images/github-mark-white.svg></a></div><h1><a href=/>purplesyringa</a></h1><nav><a href=../..>about</a><a class=current href=../../blog/>blog</a><a href=../../sink/>kitchen sink</a></nav></div></header><section><div class=viewport-container><h2>Division is hard, but it doesn't have to be</h2><time>August 24, 2024</time><p>Developers don’t usually divide numbers all the time, but hashmaps often need to compute <a href=https://en.wikipedia.org/wiki/Remainder>remainders</a> modulo a prime. Hashmaps are really common, so fast division is useful.<p>For instance, rolling hashes might compute <code>u128 % u64</code> with a fixed divisor. Compilers just drop the ball here:<pre><code class=language-rust><span class=hljs-keyword>fn</span> <span class="hljs-title function_">modulo</span>(n: <span class=hljs-type>u128</span>) <span class=hljs-punctuation>-></span> <span class=hljs-type>u64</span> {
<!doctypehtml><html prefix="og: http://ogp.me/ns#"lang=en_US><meta charset=utf-8><meta content=width=device-width,initial-scale=1 name=viewport><title>Division is hard, but it doesn't have to be | purplesyringa's blog</title><link href=../../all.css rel=stylesheet><link href=../../blog.css rel=stylesheet><link href=../../vendor/Temml-Local.css rel=stylesheet><link crossorigin href=https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,100..900;1,100..900&family=Roboto+Mono:ital,wght@0,100..700;1,100..700&family=Roboto:ital,wght@0,400;0,700;1,400;1,700&family=Slabo+27px&display=swap rel=stylesheet><link href=../../fonts/webfont.css rel=stylesheet><link media="screen and (prefers-color-scheme: dark"href=../../vendor/atom-one-dark.min.css rel=stylesheet><link media="screen and (prefers-color-scheme: light"href=../../vendor/atelier-cave-light.min.css rel=stylesheet><link title="Blog posts"href=../../blog/feed.rss rel=alternate type=application/rss+xml><meta content="Division is hard, but it doesn't have to be"property=og:title><meta content=article property=og:type><meta content=https://purplesyringa.moe/blog/division-is-hard-but-it-does-not-have-to-be/og.png property=og:image><meta content=https://purplesyringa.moe/blog/division-is-hard-but-it-does-not-have-to-be/ property=og:url><meta content="Developers don’t usually divide numbers all the time, but hashmaps often need to compute remainders modulo a prime. Hashmaps are really common, so fast division is useful.
For instance, rolling hashes might compute u128 % u64 with a fixed divisor. Compilers just drop the ball here:
fn modulo(n: u128) -> u64 {
(n % 0xffffffffffffffc5) as u64
}
modulo:
push rax
mov rdx, -59
xor ecx, ecx
call qword ptr [rip + __umodti3@GOTPCREL]
pop rcx
ret
__umodti3 is a generic long division implementation, and it’s slow and ugly.
I prefer my code the opposite of slow and ugly."property=og:description><meta content=en_US property=og:locale><meta content="purplesyringa's blog"property=og:site_name><meta content=summary_large_image name=twitter:card><meta content=https://purplesyringa.moe/blog/division-is-hard-but-it-does-not-have-to-be/og.png name=twitter:image><script data-website-id=0da1961d-43f2-45cc-a8e2-75679eefbb69 defer src=https://zond.tei.su/script.js></script><body><header><div class=viewport-container><div class=media><a href=https://github.com/purplesyringa><img src=../../images/github-mark-white.svg></a></div><h1><a href=/>purplesyringa</a></h1><nav><a href=../..>about</a><a class=current href=../../blog/>blog</a><a href=../../sink/>kitchen sink</a></nav></div></header><section><div class=viewport-container><h2>Division is hard, but it doesn't have to be</h2><time>August 24, 2024</time><p>Developers don’t usually divide numbers all the time, but hashmaps often need to compute <a href=https://en.wikipedia.org/wiki/Remainder>remainders</a> modulo a prime. Hashmaps are really common, so fast division is useful.<p>For instance, rolling hashes might compute <code>u128 % u64</code> with a fixed divisor. Compilers just drop the ball here:<pre><code class=language-rust><span class=hljs-keyword>fn</span> <span class="hljs-title function_">modulo</span>(n: <span class=hljs-type>u128</span>) <span class=hljs-punctuation>-></span> <span class=hljs-type>u64</span> {
(n % <span class=hljs-number>0xffffffffffffffc5</span>) <span class=hljs-keyword>as</span> <span class=hljs-type>u64</span>
}
</code></pre><pre><code class=language-x86asm><span class=hljs-symbol>modulo:</span>
Expand Down
24 changes: 23 additions & 1 deletion blog/division-is-hard-but-it-does-not-have-to-be/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,29 @@
title: Division is hard, but it doesn't have to be
time: August 24, 2024
intro: |
dasdasdas.
Developers don't usually divide numbers all the time, but hashmaps often need to compute [remainders](https://en.wikipedia.org/wiki/Remainder) modulo a prime. Hashmaps are really common, so fast division is useful.
For instance, rolling hashes might compute `u128 % u64` with a fixed divisor. Compilers just drop the ball here:
```rust
fn modulo(n: u128) -> u64 {
(n % 0xffffffffffffffc5) as u64
}
```
```x86asm
modulo:
push rax
mov rdx, -59
xor ecx, ecx
call qword ptr [rip + __umodti3@GOTPCREL]
pop rcx
ret
```
`__umodti3` is a generic long division implementation, and it's slow and ugly.
I prefer my code the opposite of slow and ugly.
---

Developers don't usually divide numbers all the time, but hashmaps often need to compute [remainders](https://en.wikipedia.org/wiki/Remainder) modulo a prime. Hashmaps are really common, so fast division is useful.
Expand Down
27 changes: 26 additions & 1 deletion blog/feed.rss
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,36 @@
<copyright>Alisa Sireneva, CC BY</copyright>
<managingEditor>[email protected] (Alisa Sireneva)</managingEditor>
<webMaster>[email protected] (Alisa Sireneva)</webMaster>
<lastBuildDate>Fri, 23 Aug 2024 09:15:10 GMT</lastBuildDate>
<lastBuildDate>Sat, 24 Aug 2024 10:34:36 GMT</lastBuildDate>
<docs>https://www.rssboard.org/rss-specification</docs>
<ttl>60</ttl>
<atom:link href="https://purplesyringa.moe/blog/feed.rss" rel="self" type="application/rss+xml" />

<item>
<title>Division is hard, but it doesn&#39;t have to be</title>
<link>https://purplesyringa.moe/blog/./division-is-hard-but-it-does-not-have-to-be/</link>
<description>Developers don’t usually divide numbers all the time, but hashmaps often need to compute remainders modulo a prime. Hashmaps are really common, so fast division is useful.
For instance, rolling hashes might compute u128 % u64 with a fixed divisor. Compilers just drop the ball here:
fn modulo(n: u128) -> u64 {
(n % 0xffffffffffffffc5) as u64
}

modulo:
push rax
mov rdx, -59
xor ecx, ecx
call qword ptr [rip + __umodti3@GOTPCREL]
pop rcx
ret

__umodti3 is a generic long division implementation, and it’s slow and ugly.
I prefer my code the opposite of slow and ugly.</description>
<author>[email protected] (Alisa Sireneva)</author>

<guid>https://purplesyringa.moe/blog/./division-is-hard-but-it-does-not-have-to-be/</guid>
<pubDate>Sat, 24 Aug 2024 00:00:00 GMT</pubDate>
</item>

<item>
<title>I sped up serde_json strings by 20%</title>
<link>https://purplesyringa.moe/blog/./i-sped-up-serde-json-strings-by-20-percent/</link>
Expand Down
Loading

0 comments on commit c25bd00

Please sign in to comment.