「好书推荐」Hacker's Delight Second Edition, ePub, PDF, CHM格式下载以及部分除法优化内容
![Hacker's Delight Second Edition](http://images0.cnblogs.com/blog/293254/201302/16002943-966fc2d51615447bb1deee65ff1f274f.jpg)
Title: Hacker’s Delight (2nd Edition)
Author: Henry S. Warren Page
Pages: 512 pages
Publisher: Addison-Wesley Professional; 2 edition (October 5, 2012)
Language: English
ISBN-10: 0321842685
ISBN-13: 978-0321842688
Book Description:
A collection useful programming advice the author has collected over the years; small algorithms that make the programmer's task easier.
* At long last, proven short-cuts to mastering difficult aspects of computer programming
* Learn to program at a more advanced level than is generally taught in schools and training courses, and much more advanced than can be learned through individual study/experience.
* An instant cult classic for programmers! 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.
These are the timesaving techniques relished by computer hackers--those devoted and persistent code developers who seek elegant and efficient ways to build better software. 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 tricks he has collected from his considerable experience in the worlds of application and system programming. Most of these techniques are eminently practical, but a few are included just because they are interesting and unexpected. The resulting work is an irresistible collection that will help even the most seasoned programmers better their craft.
Topics covered include:
- A broad collection of useful programming tricks
- Small algorithms for common tasks
- Power-of-2 boundaries and bounds checking
- Rearranging bits and bytes
- Integer division and division by constants
- Some elementary functions on integers
- Gray code
- Hilbert's space-filling curve
- And even formulas for prime numbers!
This book is for anyone who wants to create efficient code. Hacker's Delight will help you learn to program at a higher level--well beyond what is generally taught in schools and training courses--and will advance you substantially further than is possible through ordinary self-study alone.
In Hacker’s Delight, Second Edition, Hank Warren once again compiles an irresistible collection of programming hacks: timesaving techniques, algorithms, and tricks that help programmers build more elegant and efficient software, while also gaining deeper insights into their craft. Warren’s hacks are eminently practical, but they’re also intrinsically interesting, and sometimes unexpected, much like the solution to a great puzzle. They are, in a word, a delight to any programmer who is excited by the opportunity to improve.
Extensive additions in this edition include
- A new chapter on cyclic redundancy checking (CRC), including routines for the commonly used CRC-32 code
- A new chapter on error correcting codes (ECC), including routines for the Hamming code
- More coverage of integer division by constants, including methods using only shifts and adds
- Computing remainders without computing a quotient
- More coverage of population count and counting leading zeros
- Array population count
- New algorithms for compress and expand
- An LRU algorithm
- Floating-point to/from integer conversions
- Approximate floating-point reciprocal square root routine
- A gallery of graphs of discrete functions
- Now with exercises and answers
中文介绍
此书中文名叫《高效程序的奥秘》,正如英文介绍所说,本书包含了一系列诱人的编程技巧:省时技术,算法,构建优雅高效软件的技巧,讲述了计算机算术(Computer Arithmetic)的更深层次的、更隐秘的技术。汇集了各种编程的小技巧和算法,2的幂边界和边界检测、位和字节的重排列、整数除法和常量除法、针对整数的基涵义,空间填充曲线、素数公式等。书中介绍了汇编编码技巧以及底层算法的优化,适合程序库、编译器开发人员,逆向工程爱好者研读。
Amazon上读者的高度评价称其为“黑魔法手册”(A Handbook of Black Magic)。
写了这么多好话,我自己都怀疑我写的是软文了,不再废话,为了说明这本书里到底是什么东西,举个栗子。
在Sublime Text 2 Version 2.0.1, Build 2217 中有一段这样的代码:
012FCB50 |. B8 93244992 MOV EAX,92492493 ;Magic Number = (234+ 5) / 7 – 232012FCB55 |. F7E9 IMUL ECX012FCB57 |. 03D1 ADD EDX,ECX012FCB59 |. C1FA 04 SAR EDX,4012FCB5C |. 8BCA MOV ECX,EDX012FCB5E |. C1E9 1F SHR ECX,1F012FCB61 |. 03CA ADD ECX,EDX初看之下,完全不知道这段代码要干什么,平白无故突然冒出来个常数92492493h,于是搜了一下,这才知道其实是编译器把除法进行了优化。因为除法指令的执行周期较长,效率比较低,编译器将其转换成了乘法和加法。
如果把最初ECX中的值用x表示,92492493h用c表示,那么第1,2条语句可以表示为 x*c
ADD EDX,ECX
其中EDX为IMUL指令运算结果的高32位,直接使用EDX相当于右移32位,aka: (x*c)/232 + xSAR算术右移(Shift Arithmetic Right),最高位不变,其余位向右移动,左边空出来的地方用符号位(最高位)补,式子变成了((x*c)/232 + x)/24,暂用N表示。
接下来的两条语句表示为N/231+N = N*(1+1/231)=N*(1+ο) 其中ο是无穷小量,取近似值也就是N
所以上面的语句表示((x*c)/232 + x)/24,化简得x*(c+232)/236
c=92492493h(补码),是个负数,由补码的知识可以知道c+232 的值是92492493h(正数)
所以N式最后可以简化为x*92492493h/236,计算236/92492493h = 236/2454267027=27.9999999918 取近似值得到28
所以这段代码的意思就是把ECX中的值除以28,ECX div 28
由我们简化来的式子我们可以知道,除法优化的基本思路就是把被除数先放大一定倍数(乘以某个常数),然后再进行移位。
至于这个常数怎么得到,书中做了比较详细的说明,并且提供了一个常数表,官网上也有一个专门的Magic Number计算器,可以算出除法优化所需的Magic Number以及位移操作应该移动的位数。
附:除法所需要的Magic Numbers表格
SOME MAGIC NUMBERS FOR W = 32
![10tab01](http://images0.cnblogs.com/blog/293254/201302/16003103-6288a6afba364eb7b674c1307b6727dc.jpg)
SOME MAGIC NUMBERS FOR W = 64
![10tab02](http://images0.cnblogs.com/blog/293254/201302/16003114-3d423cd8c0074b68bb209af4a5d5d000.jpg)
书中有关除7的内容在Chapter 10–3 Signed Division and Remainder by Non-Powers of 2,见下图:
看雪也有高手曾写过类似的文章,比较详细:http://www.pediy.com/kssd/pediy11/116974.html
看到这段我对发明这个方法的人佩服的五体投地(当然我也只是大概翻了翻此书,前面的好评全是翻译别人的。:P ),竟然能想出这种办法来解决除法的问题。可是CPU技术发展这么迅速,以前这么优化确实会快,现在1条除法指令真的会比7条其他指令慢吗?
这本书对搞逆向工程和编译器开发的人确实有用,不接触底层的人看看只能吹牛装逼用了。。。
获取电子档书籍
下载链接:http://pan.baidu.com/share/link?shareid=276700&uk=1009258824 (内有ePub, PDF, CHM三种格式)网上似乎没有CHM格式,这个是我自己拆开ePub做的。
第二版勘误表:http://www.hackersdelight.org/errata2_1.pdf
官网:http://www.hackersdelight.org/ (提供书中代码下载,Magic Number计算器及勘误表等)
第一版在线阅读:http://hackers-delight.org.ua/