Feddit
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
RSS Bot@lemmy.bestiver.seMB to Hacker News@lemmy.bestiver.seEnglish · 11 days ago

Superoptimizing an FPU Emulator

purplesyringa.moe

external-link
message-square
0
link
fedilink
1
external-link

Superoptimizing an FPU Emulator

purplesyringa.moe

RSS Bot@lemmy.bestiver.seMB to Hacker News@lemmy.bestiver.seEnglish · 11 days ago
message-square
0
link
fedilink
Superoptimizing an FPU emulator
purplesyringa.moe
external-link
Superoptimization is the process where a compiler automatically finds the optimal sequence for a loop-free sequence of instructions. – Wikipedia Compilers are good at making mechanical transformations that make code faster. Unfortunately, most of them are not as good at non-trivial reasoning, so they can’t invent a completely novel approach to solving your problem. When designing a library for floating-point calculations, I had to implement a function that takes a float f and produces a float corresponding to the lowest set bit in f . For example, if f = 1.01101 ⋅ 2 10 in binary, it would return 0.00001 ⋅ 2 10 = 2 5 . This can be implemented with bit twiddling, but the straightforward algorithm breaks for powers of two, so I had to insert an if: if f32::from_bits(x.to_bits() & !f32::MANTISSA_MASK) == x { // is x a power of two? return x; // x itself is the lowest bit } else { return x - f32::from_bits(x.to_bits() & (x.to_bits() - 1)); // x, minus x without lowest bit } This if really annoyed me. It’s slowed down for the common case, and branches could be mispredicted. I wanted to find a formula that automatically handled powers of two, but try as I might, I couldn’t find anything convincingly optimal. So I made a non-orthodox play.

Comments

alert-triangle
You must log in or register to comment.

Hacker News@lemmy.bestiver.se

hackernews@lemmy.bestiver.se

Subscribe from Remote Instance

You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !hackernews@lemmy.bestiver.se
lock
Community locked: only moderators can create posts. You can still comment on posts.

Posts from the RSS Feed of HackerNews.

The feed sometimes contains ads and posts that have been removed by the mod team at HN.

Source of the RSS Bot

Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 867 users / day
  • 425 users / week
  • 664 users / month
  • 253 users / 6 months
  • 1 local subscriber
  • 5.62K subscribers
  • 35.5K Posts
  • 7.78K Comments
  • Modlog
  • mods:
  • patrick@lemmy.bestiver.se
  • RSS Bot@lemmy.bestiver.se
  • BE: 0.19.11
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org