Engineer’s Bookshelf
Comment
Max the Magnificent
Party Pooper :-)
Jerry.Brittingham
Having been "close to the metal" for over 30 years, I have to agree with Ross93.
Book Review: Hacker’s Delight by Henry S. Warren, Jr.
Clive Maxfield
4/5/2012 10:49 AM EDT
First and foremost, this book is NOT about hacking in the modern sense of finding out weaknesses in a computer system and exploiting them. In the early days of computers, a hacker was someone who delighted in subtle programming tricks and small algorithms that could be used to make their computer code “tighter” and more efficient.
Hacker's Delight really is a great book. Just to give you a feel for it, it’s the first book of this type that I’ve ever seen on Amazon in which every reviewer (and there are a bunch of them) has awarded five stars.
On the one hand, the tricks in this book are becoming less relevant as computational resources – in the form of memory and performance – increase. Many of today’s programmers work at a high level of abstraction and are conceptually a long way from the “machine”. Also, in many cases, a slight inefficiency in the code is of less importance than getting the application out of the door in a timely manner.
On the other hand, if you are working on embedded systems with limited
performance and memory resources, and if you are working “close to the
metal”, then the tips and tricks in this book are for you.
Most of the time the author assumes a 32-bit machine and describes his tips and tricks in a generic computer algebra; more complex tricks are presented in C; but all of these techniques could be easily adapted to machines of different word lengths (8-bits, 16-bits, 64-bits, etc.) and/or implemented in assembly language if required.
As a simple tempting taster, let’s consider the very first page of Chapter 2: Basics (Chapter 1 is the introduction where the author defines things like the notations to be used and the syntax of his computer algebra).
Note that in the following discussions:
– x = Arithmetic negation
~x = Bitwise NOT (ones complement)
x & y = Bitwise AND
x | y = Bitwise OR
x ^ y = Bitwise Exclusive OR
OK, the first part of the first page reads as follows:
I haven’t finished yet. Remember that we are still on the very first page of the main body of the book. At the bottom of the page is one more formula to form a mask that identifies the rightmost 1-bit and the trailing 0’s, producing all 1’s if x = 0 (e.g., 01011000 => 00001111). This formula is as simple as its companions shown above … I’ll leave it as a little teaser for you to play with.
As one reviewer on Amazon said “If you approach optimization as a puzzle, wherein the solution is its own reward, this book is indeed a compendium of delights.”
I totally agree, and I highly recommend this book to anyone who is interested in knowing the "tricks of the trade." This is of especially interest to embedded software developers working with limited resources, but it is also of interest to any software developer who takes satisfaction in making his or her code as “tight” and efficient as possible.
If you found this article to be interest, visit Microcontroller / MCU Designline where – in addition to my blogs on all sorts of "stuff" (also check out my Max's Cool Beans blog) – you will find the latest and greatest design, technology, product, and news articles with regard to all aspects of designing and using microcontrollers.
Also, you can obtain a highlights update delivered directly to your inbox by signing up for my weekly newsletter – just Click Here to request this newsletter using the Manage Newsletters tab (if you aren't already a member you'll be asked to register, but it's free and painless so don't let that stop you [grin]).
Last but certainly not least, make sure you check out all of the discussions and other information resources at MicrocontrollerCentral.com, including blogs by yours truly.
Hacker's Delight really is a great book. Just to give you a feel for it, it’s the first book of this type that I’ve ever seen on Amazon in which every reviewer (and there are a bunch of them) has awarded five stars.
On the one hand, the tricks in this book are becoming less relevant as computational resources – in the form of memory and performance – increase. Many of today’s programmers work at a high level of abstraction and are conceptually a long way from the “machine”. Also, in many cases, a slight inefficiency in the code is of less importance than getting the application out of the door in a timely manner.
On the other hand, if you are working on embedded systems with limited
performance and memory resources, and if you are working “close to the
metal”, then the tips and tricks in this book are for you. Most of the time the author assumes a 32-bit machine and describes his tips and tricks in a generic computer algebra; more complex tricks are presented in C; but all of these techniques could be easily adapted to machines of different word lengths (8-bits, 16-bits, 64-bits, etc.) and/or implemented in assembly language if required.
As a simple tempting taster, let’s consider the very first page of Chapter 2: Basics (Chapter 1 is the introduction where the author defines things like the notations to be used and the syntax of his computer algebra).
Note that in the following discussions:
– x = Arithmetic negation
~x = Bitwise NOT (ones complement)
x & y = Bitwise AND
x | y = Bitwise OR
x ^ y = Bitwise Exclusive OR
OK, the first part of the first page reads as follows:
Manipulating Rightmost Bits
Use the following formula to turn off the rightmost 1-bit in a word, producing 0 if none (e.g., 01011000 => 01010000):x & (x – 1)
This may be used to determine if an unsigned integer is a power of 2 or is a 0; apply the formula followed by a 0-test on the result.
Similarly, the following formula can be used to test if an unsigned integer is of the form 2n – 1 (including all 0 or all 1’s):x & (x + 1)
Use the following formula to isolate the rightmost 1-bit, producing 0 if none (e.g., 01011000 => 00001000):x & (–x)
Use the following formula to isolate the rightmost 0-bit, producing 0 if none (e.g., 10100111 => 00001000):~x & (x + 1)
Use one of the following formulas to form a mask that identifies the trailing 0’s, producing all 1’s if x = 0 (e.g., 01011000 => 00000111):~x & (x – 1), or
~(x | –x), or
(x & –x) – 1
The first formula has some instruction-level parallelism
I haven’t finished yet. Remember that we are still on the very first page of the main body of the book. At the bottom of the page is one more formula to form a mask that identifies the rightmost 1-bit and the trailing 0’s, producing all 1’s if x = 0 (e.g., 01011000 => 00001111). This formula is as simple as its companions shown above … I’ll leave it as a little teaser for you to play with.
As one reviewer on Amazon said “If you approach optimization as a puzzle, wherein the solution is its own reward, this book is indeed a compendium of delights.”
I totally agree, and I highly recommend this book to anyone who is interested in knowing the "tricks of the trade." This is of especially interest to embedded software developers working with limited resources, but it is also of interest to any software developer who takes satisfaction in making his or her code as “tight” and efficient as possible.
If you found this article to be interest, visit Microcontroller / MCU Designline where – in addition to my blogs on all sorts of "stuff" (also check out my Max's Cool Beans blog) – you will find the latest and greatest design, technology, product, and news articles with regard to all aspects of designing and using microcontrollers.
Also, you can obtain a highlights update delivered directly to your inbox by signing up for my weekly newsletter – just Click Here to request this newsletter using the Manage Newsletters tab (if you aren't already a member you'll be asked to register, but it's free and painless so don't let that stop you [grin]).
Last but certainly not least, make sure you check out all of the discussions and other information resources at MicrocontrollerCentral.com, including blogs by yours truly.
Navigate to related information


antedeluvian
4/5/2012 2:42 PM EDT
I don't know if the adjunct web site is mentioned in the latest edition, but just in case-
http://hackersdelight.org/
Has additional info, and errata.
Sign in to Reply
Ross93
4/6/2012 9:56 AM EDT
As a caveat, I haven’t read the book, so this observation is based solely on the examples given.
As a firmware engineer, I can appreciate the cleverness and elegance of these examples, however, in my opinion the ability to be able to read and maintain this would be a nightmare without (and probably with) extensive comments.
Ross
Sign in to Reply
Max the Magnificent
4/11/2012 4:58 PM EDT
Party Pooper :-)
Sign in to Reply
nkkav
4/7/2012 5:00 PM EDT
Hi Clive,
thanks to the code from this book, I had shaped up a constant division routine generator: http://sourceforge.net/projects/kdiv/
The algorithms for such divisions work with the multiplicative inverse. For an x/N division, you have to multiply x with a representation of 1/N, followed by some adjustment steps. In any case, no more than 8 or 9 cycles are required for a 32-bit divide on small RISC targets (based on my own experimentation).
Even the 64-bit multiply with 1/N can be avoided, if you reduce this constant multiplication via any of the known methods.
Cheers,
Nikolaos Kavvadias
PS: Guess I'll be ordering a hardcopy from Amazon
PS2: http://libdivide.com does the same, producing SSE2 code.
Sign in to Reply
Jerry.Brittingham
4/10/2012 3:12 AM EDT
Having been "close to the metal" for over 30 years, I have to agree with Ross93.
Sign in to Reply