Hacker's Delight

  • Authors:
  • Henry S. Warren

  • Affiliations:
  • -

  • Venue:
  • Hacker's Delight
  • Year:
  • 2002

Quantified Score

Hi-index 0.00

Visualization

Abstract

From the Book:Caveat Emptor: The cost of software maintenance increases with the square of the programmer's creativity.First Law of Programmer Creativity,Robert D. Bliss, 1992This is a collection of small programming tricks which the author has comeacross over many years. Most of them will work only on computers that representintegers in two's-complement form. Although a 32-bit machine is assumedwhen the register length is relevant, most of the tricks are easily adapted tomachines with other register sizes.This book does not deal with "large" tricks such as sophisticated sorting andcompiler optimization techniques. Rather, it deals with "small" tricks that usuallyinvolve individual computer words or instructions, such as counting thenumber of 1-bits in a word. Such tricks often use a mixture of arithmetic and logicalinstructions.It is assumed throughout that integer overflow interrupts have been maskedoff, so they cannot occur. C, Fortran, and even Java programs run in thisenvironment, but Pascal and ADA users beware!The presentation is informal. Proofs are given only when the algorithm is definitelynot obvious, and sometimes not even then. The methods use computer-arithmetic, "floor" functions, mixtures of arithmetic and logical operations, etc.Proofs in this domain are often difficult and awkward to express.To reduce typographical errors and oversights, many of the algorithms havebeen executed. That is why they are given in a real programming language eventhough it, like every computer language, has some ugly features. For the highlevel language C is used, because it is widely known, it allows the straightforwardmixture of integer and bit-stringoperations, and C compilers are availablethat produce high quality object code.Occasionally machine language is used. It employs a 3-address format, mainlyfor ease of readability. The assembly language used is that of a fictitiousmachine that is representative of today's RISC computers.Branch-free code is favored. This is because on many computers branchesslow down instruction fetching and inhibit executing instructions in parallel.Another problem with branches is that they may inhibit compiler optimizationssuch as instruction scheduling, commoning, and register allocation. That is, thecompiler may be more effective at these optimizations with a program that consistsof a few large basic blocks, rather than many small ones.The code sequences also tend to favor small immediate values, comparisons tozero (rather than to some other number), and instruction-level parallelism.Although much of the code would become more concise by using table lookups(from memory), this is not often mentioned. This is because loads are becomingmore expensive relative to arithmetic instructions, and the table lookup methodsare often not very interesting (although they are often practical). But there areexceptional cases.Finally, I should mention that the term "hacker" in the title is meant in the originalsense of an aficionado of computers—someone who enjoys making computersdo new things, or do old things in a new and clever way. The hacker isusually quite good at his craft, but may very well not be a professional computerprogrammer or designer. The hacker's work may be useful or may be just agame. As an example of the latter, more than one determined hacker has writtena program which, when executed, writes out an exact copy of itself. 1 This is thesense in which we use "hacker." If you're looking for tips on how to break intoother's computers, you won't find them here.H. S. Warren, Jr.February 2002