Hacker News new | past | comments | ask | show | jobs | submit login
Hacker’s Delight (2012) (books.google.com)
170 points by kjhughes on Aug 7, 2022 | hide | past | favorite | 37 comments



This is a really great book for anyone interested in compiler development, JIT compilation of higher-level languages, reverse engineering, or anything that involves understanding or generating assembly instructions. This is not a book about hacking from the perspective of breaking into systems or finding and exploiting vulnerabilities in software. The term Hacker in the title is a reference to an early meaning of the word that referred to someone who liked to tinker and understand how things worked.

When I was doing a lot of reverse engineering and vulnerability research work this book gave some really great insight into some of the compiler optimizations that I would come across. Highly recommended for anyone trying to take their skills to the next level.


You might be thinking about a different book? Hacker's Delight is mostly about bit fiddling (counting bits, finding the longest string of 1-bits, multiplication and division, Gray code, CRC, the famous approximate reciprocal square root) and such.


These are just the kinds of things that an optimizing compiler can use, so would be quite useful for reverse engineering the product of an optimizing compiler to figure out what it is doing.


yes. the book could definitively be named differently. I skimmed through the free pages and I came to the same conclusion. Probably a great book, but not for me since I am a higher level developer that benefit from the outcome of books like these.


People on a site called Hacker News complaining about the name of a book called Hacker's Delight.


Kids today and their "analysis of history from the present perspective". Sheesh!


See also Knuth's TAOCP section 7.1.3 "Bitwise Tricks & Techniques" which makes references to this book Hacker's Delight and covers some similar ground, in Knuth's unique style. (Published as part of Vol 4A in 2011, earlier as Volume 4 Fascicle 1 in 2009, and earlier draft available online as pre-fascicle 1A: https://cs.stanford.edu/~knuth/fasc1a.ps.gz ) (The first edition of Hacker's Delight is from 2002. HAKMEM is from 1972.)

Independent recommendation here: https://twitter.com/pervognsen/status/1253135922553425921

> The bitwise techniques book will shock you because it's basically Hacker's Delight: Knuth Edition and it is very, very good and useful.


The section on computing magic numbers for integer division is quite useful if you find yourself implementing a vectorized version of an algorithm that calls for integer division or integer modulus, since these instructions typically don't exist


Note that some fairly big hardware didn't have them either.

Itanium and DEC Alpha didn't have integer divide IIRC.

Cheap RISC-V also probably won't.


Compilers have improved a lot, to the point where you don't need to read this book for many of the tricks. For example, if you divide by a constant in gcc, you can see the tricks from this book being applied if you look at the disassembly.

Still, this is a standard textbook for little tricks. Knowing these kind of tricks used to be enough to make you a hacker in the original sense. I greatly enjoyed reading it and still recommend it even today.

It really is a delight for hackers.


This is not only about optimisation, but often it provides you the idiom for coding something.

E.g.: 'is x a power of two'? How to write this in a programming language? However unoptimised: how to actually code this test?

HD tells you the idiom: (x & (x-1)) == 0, for any x != 0.


In gcc 9.3, it seems like it stops applying this optimization after maybe 30 constants. Pretty weird.


I have copies of both the first and second editions of Hacker’s Delight. The second edition is mostly the same, but with a few additions and corrections. (Also printed on thicker paper with a smaller text area so it looks 2.2x thicker for 1.1x the content.)

I use it occasionally for things like optimizing tolower() https://news.ycombinator.com/item?id=31900872 and for popcount hacks https://dotat.at/prog/qp or for quickly updating a log-linear histogram https://dotat.at/@/2022-07-15-histogram.html


Beautiful book. But it's full of little mistakes. See the errata:

https://ptgmedia.pearsoncmg.com/images/9780321842688/Errata/...

There are numerous other bugs that were found and submitted, but for unknown reasons don't yet appear in the errata listing above.


This book has been used in quite a few coding interview circa 2012 when I started interviewing for my first job. That kind of ruined the joy of cracking hacky coding problem of the book. Sigh


If you haven't already seen it check this out along similar lines:

http://aggregate.org/MAGIC/

What else do you have?


I have my physical copy of this around somewhere


Mine taunts me in plain view from my bookshelf. It's a cool book but I have little use for what it provides so I haven't been able to dig into it much.


I use mine on occasion. It's particularly useful when targeting architectures that don't have modern intrinsics for special operations.

It's worth noting that some of the solutions aren't optimal for modern devices with conditional moves. In some cases you can get equal or better performance with straightforward code than some of the stuff in the book that goes out of its way to avoid branching control flow.


It has a pretty cool niche as a set of toy problems to verify cryptography tooling and I've found some of the recipes inside useful inspiration for constant time programming and other constrained domains. It's not as useful as it used to be for exactly the reasons you mentioned though.


Ha ha, this falls firmly into the "I wish I needed to reference this more" category for me as well.


Same. But sometimes I like skimming it just as a distraction. There's something so satisfying about seeing these tricks I'm far too stupid to come up with on my own. A couple of times, I've had some breakthroughs when stuck on an unrelated problem, while reading this book, because the "psychedelic hackery" sort of leads me down an alley I wouldn't otherwise have thought of, due to some superficial similarity.


When I was doing low-level optimisation for games (lots of SIMD), I was using mine quite frequently.

Now that I'm doing more application development it's sitting on my bookshelf looking neglected.


TDLR: Author systematically collected little programming tricks, thematically organized them, and clearly explains them.


Bioshock!


A fine book with a very misleading name. The book is about basic computer science and algorithms.


It's not misleading, it's just an older meaning of the word "hacker" and one that still exists in the name of this site. Hacker's delight is a spiritual successor to the HAKMEM memo from the 70s. Guy Steele even wrote the foreword.


The term hacker has a long and storied history. A hacker is someone who makes hacks. A hack is a thing, typically a piece of code, which must be appreciated for its aesthetic beauty, regardless of whether it's particularly useful. [This term was obviously originally used sarcastically]. People strove to become hackers. It was a badge of honor.

In the 1980s the term "hacker" was possibly conflated with "cracker" by stupids in the media, and came to mean a software pirate or kid who broke into the Pentagon. But the proper meaning still hasn't been lost with the graybeards among us.


> In the 1980s the term "hacker" was possibly conflated with "cracker" by stupids in the media,

I think it's not necessarily due to stupidity that media outlets eschewed the term "cracker."


Reminded me of Smokers Delight by Nightmares on Wax: https://www.last.fm/music/Nightmares%20on%20Wax/Smokers%20De...


And Rapper's Delight before that.


And Turkish Delight before that


I hear all of you about how it's not misleading, but I must admit that when I clicked on this link I was expecting a different kind of book.


Nice book but I feel the title should be something like principles of computer science or something of that sort.


Programming Tricks Close to the Metal seems more appropriate.


This is a nice title! At the very least a new book should be written to use it.


That’s even less descriptive.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: