gnu coreutils-4.5.1 factor.c 代码分析2
这几天想自己的目标,想来想去,先把这个包代码读完,当成目标。今天接着分析,我想,不应该把读代码当成任务,应该当成小说一样来品读,因此,应该在读代码的过程中享受到快乐。于是就慢慢重读,慢慢品。
再读它,又有了别一番滋味。
static int
factor (uintmax_t n0, int max_n_factors, uintmax_t *factors)
这个函数大体流程清楚,但其中有一处不明白。
d = 2;
do
{
q = n / d;
while (n == q * d)
{
assert (n_factors < max_n_factors);
factors[n_factors++] = d;
n = q;
q = n / d;
}
d += *(w++);
if (w == WHEEL_END)
w = WHEEL_START;
}
while (d <= q);
其中 d += *(w++); 是什么意思呢?我当时想,此时,d作为因子,不是应该直接
d += 1
为什么要加数组呢??
查看w的定义
unsigned int const *w = wheel_tab;
#include "wheel-size.h" /* For the definition of WHEEL_SIZE. */
static const unsigned int wheel_tab[] =
{
#include "wheel.h"
};
这几天想自己的目标,想来想去,先把这个包代码读完,当成目标。今天接着分析,我想,不应该把读代码当成任务,应该当成小说一样来品读,因此,应该在读代码的过程中享受到快乐。于是就慢慢重读,慢慢品。
再读它,又有了别一番滋味。
static int
factor (uintmax_t n0, int max_n_factors, uintmax_t *factors)
这个函数大体流程清楚,但其中有一处不明白。
d = 2;
do
{
q = n / d;
while (n == q * d)
{
assert (n_factors < max_n_factors);
factors[n_factors++] = d;
n = q;
q = n / d;
}
d += *(w++);
if (w == WHEEL_END)
w = WHEEL_START;
}
while (d <= q);
其中 d += *(w++); 是什么意思呢?我当时想,此时,d作为因子,不是应该直接
d += 1
为什么要加数组呢??
查看w的定义
unsigned int const *w = wheel_tab;
#include "wheel-size.h" /* For the definition of WHEEL_SIZE. */
static const unsigned int wheel_tab[] =
{
#include "wheel.h"
};
#define WHEEL_START (wheel_tab + WHEEL_SIZE)
#define WHEEL_END (wheel_tab + (sizeof wheel_tab / sizeof wheel_tab[0]))
#define WHEEL_END (wheel_tab + (sizeof wheel_tab / sizeof wheel_tab[0]))
其中
static const unsigned int wheel_tab[] =
{
#include "wheel.h"
};
可难住我了,我想,数组中定义宏,有这种用法吗?难道可以注释?不对。 后来,我想,应该是会进行展开,于是关键要找wheel.h中写些什么?
find /usr/include/ -name 'wheel.h' -print
找不到。于是全部找
find / -name 'wheel.h' -print
居然在
coreutils-4.5.1/src/
下找到了,打开一看。原来如此。哈哈
/* The first 4 elements correspond to the incremental offsets of the
first 5 primes (2 3 5 7 11). The 5(th) element is the
difference between that last prime and the next largest integer
that is not a multiple of those primes. The remaining numbers
define the wheel. For more information, see
http://www.utm.edu/research/primes/glossary/WheelFactorization.html. */
1,
2,
2,
4,
2,
4,
2,
4,
6,
2,
6,
4,
看到没,wheel.h中定义了质数的增量,原理我不知道,但明白了,先用2除,没有2的因子后,再用2+1除,没有3的因子后,再用3+2除,至于这种方法的巧妙,那就看网站
http://www.utm.edu/research/primes/glossary/WheelFactorization.html.
不过,我不想细究了。
作者的代码写得真好。
static const unsigned int wheel_tab[] =
{
#include "wheel.h"
};
可难住我了,我想,数组中定义宏,有这种用法吗?难道可以注释?不对。 后来,我想,应该是会进行展开,于是关键要找wheel.h中写些什么?
find /usr/include/ -name 'wheel.h' -print
找不到。于是全部找
find / -name 'wheel.h' -print
居然在
coreutils-4.5.1/src/
下找到了,打开一看。原来如此。哈哈
/* The first 4 elements correspond to the incremental offsets of the
first 5 primes (2 3 5 7 11). The 5(th) element is the
difference between that last prime and the next largest integer
that is not a multiple of those primes. The remaining numbers
define the wheel. For more information, see
http://www.utm.edu/research/primes/glossary/WheelFactorization.html. */
1,
2,
2,
4,
2,
4,
2,
4,
6,
2,
6,
4,
看到没,wheel.h中定义了质数的增量,原理我不知道,但明白了,先用2除,没有2的因子后,再用2+1除,没有3的因子后,再用3+2除,至于这种方法的巧妙,那就看网站
http://www.utm.edu/research/primes/glossary/WheelFactorization.html.
不过,我不想细究了。
作者的代码写得真好。