Home > 源码分析 > log2的快速计算法

log2的快速计算法

May 9th, 2013

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: log2的快速计算法

从erl_mseg.c中摘抄的:

static const int debruijn[32] = {

0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};

#define LOG2(X) (debruijn[((Uint32)(((X) & -(X)) * 0x077CB531U)) >> 27])

供大家参考!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: 源码分析 Tags: ,
  1. May 9th, 2013 at 16:44 | #1

    LOG2(1025) 能处理么?

  2. May 9th, 2013 at 16:46 | #2

    LOG2(1025)c能处理吗?

  3. fly2best
    May 9th, 2013 at 20:58 | #3

    能给出来源吗?

    Yu Feng Reply:

    从erl_mseg.c中摘抄的:
    https://github.com/erlang/otp/blob/master/erts/emulator/sys/common/erl_mseg.c

  4. zht
    May 10th, 2013 at 08:16 | #4

    只能是整数吧?

  5. Nick
    May 10th, 2013 at 16:35 | #5

    呵呵 这段代码出自http://graphics.stanford.edu/~seander/bithacks.html,以及Quake III
    其用来计算一个32位整数右边连续的0有多少个
    最牛逼的就是它用的那个整数0x077CB531U

    展开成2进制后比较牛逼

  6. newwayy
    May 31st, 2013 at 13:35 | #6

    (X) & -(X) 得到的是二进制末尾1所表示的数, 宏的最后结果也只能得到末尾1所在的位置。LOG2(奇数) === 0.

  7. fly2best
    June 17th, 2013 at 08:43 | #7

    matrix67大牛有一篇文章分析了原理:
    神秘常量复出!用0x077CB531计算末尾0的个数
    http://www.matrix67.com/blog/archives/3985

Comments are closed.