A collection useful programming advice the author has collected over the years; small algorithms that make the programmer's task easier.
Computer programmers are often referred to as hackers -- solitary problem solvers engrossed in a world of code as they seek elegant solutions to building better software. While many view these unique individuals as "madmen," the truth is that much of the computer programmer's job involves a healthy mix of arithmetic and logic. In Hacker's Delight, veteran programmer Hank Warren shares the collected wisdom -- namely tips and tricks -- from his considerable experience in the world of application development. The resulting work is an irresistible collection that will help even the most seasoned programmers better their craft.
Henry S. Warren Jr. has had a 40-year career with IBM, spanning the computer field from the IBM 704 to PowerPC. He has worked on various military command and control systems, and on the SETL project under Jack Schwartz at NYU. Since 1973 he has been in IBM's Research Division at Yorktown Heights, New York. Here he has done compiler and computer architecture work on the 801 computer and its several variants through PowerPC. Presently he is working on the Blue Gene petaflop computer project. He received his Ph.D. in Computer Science from the Courant Institute at New York University in 1980.
Click below for Sample Chapter(s) related to this title:
Sample Chapter 3
Instruction Set and Execution Time Model.
Manipulating Rightmost Bits.
Addition Combined with Logical Operations.
Inequalities among Logical and Arithmetic Expressions.
Absolute Value Function.
Shift Right Signed from Unsigned.
Transfer of Sign.
Decoding a “Zero Means 2**n” Field.
Condition Code Result of Add, Subtract, and Multiply.
Multibyte Add, Subtract, Absolute Value.
Doz, Max, Min.
Alternating among Two or More Values.
Rounding Up/Down to a Multiple of a Known Power of 2.
Rounding Up/Down to the Next Power of 2.
Detecting a Power-of-2 Boundary Crossing.
Checking Bounds of Integers.
Propagating Bounds through Adds and Subtracts.
Propagating Bounds through Logical Operations.
Counting Leading 0's.
Counting Trailing 0's.
Find First 0-Byte.
Find First String of 1-Bits of a Given Length.
Reversing Bits and Bytes.
Transposing a Bit Matrix.
Compress, or Generalized Extract.
General Permutations, Sheep and Goats Operation.
Rearrangements and Index Transformations.
High-Order Half of 64-Bit Product.
High-Order Product Signed from/to Unsigned.
Multiplication by Constants.
Unsigned Short Division from Signed Division.
Unsigned Long Division.
Signed Division by a Known Power of 2.
Signed Remainder from Division by a Known Power of 2.
Signed Division and Remainder by Non-powers of 2.
Signed Division by Divisors >= 2.
Signed Division by Divisors #= -2.
Incorporation into a Compiler.
Unsigned Division by Divisors >= 1.
Incorporation into a Compiler (Unsigned).
Miscellaneous Topics (Unsigned).
Applicability to Modulus and Floor Division.
Sample Magic Numbers.
Exact Division by Constants.
Test for Zero Remainder after Division by a Constant.
Integer Square Root.
Integer Cube Root.
Base -1 + i.
What is the Most Efficient Base?
Incrementing a Gray Coded Integer.
Negabinary Gray Code.
Brief History and Applications.
A Recursive Algorithm for Generating the Hilbert Curve.
Coordinates from Distance along the Hilbert Curve.
Distance from Coordinates on the Hilbert Curve.
Incrementing the Coordinates on the Hilbert Curve.
Non-recursive Generating Algorithms.
Other Space-Filling Curves.
Comparing Floating-Point Numbers Using Integer Operations.
The Distribution of Leading Digits.
Table of Miscellaneous Values.
Formulas for Other Difficult Functions.
Caveat Emptor: The cost of software maintenance increases with the square of the programmer's creativity.First Law of Programmer Creativity,
This 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-string operations, 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.
Click below to download the Index file related to this title: