我正在尝试为一个项目重新实现一些coreutils,我经常看到TYPE_MINIMUM(一些int),但是我看不到它是在哪里定义的,也看不到它的任何用法。我不确定它是在make过程中生成的,还是故意的。有什么想法吗?
我已经包含了函数所需的所有头文件,在调用TYPE_MINIMUM进行验证之前,一切都会正常进行。
正在使用的文件的完整源代码:https://github.com/coreutils/coreutils/blob/master/src/who.c
static const char *idle_string (time_t when, time_t boottime)
{
static time_t now = TYPE_MINIMUM (time_t);
if (now == TYPE_MINIMUM (time_t))
time (&now);
if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
{
int seconds_idle = now - when;
if (seconds_idle < 60)
return " . ";
else
{
static char idle_hhmm[IDLESTR_LEN];
/* FIXME-in-2018: see if this assert is still required in order
to suppress gcc's unwarranted -Wformat-length= warning. */
assert (seconds_idle / (60 * 60) < 24);
sprintf (idle_hhmm, "%02d:%02d",
seconds_idle / (60 * 60),
(seconds_idle % (60 * 60)) / 60);
return idle_hhmm;
}
}
return (" old ");
}error: ‘TYPE_MINIMUM’ was not declared in this scope发布于 2019-07-25 22:21:45
这个宏的实现可以在Coreutils "intprops.h" file中找到。
从该文件中:
/* The maximum and minimum values for the integer type T. */
#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
#define TYPE_MAXIMUM(t) \
((t) (! TYPE_SIGNED (t) \
? (t) -1 \
: ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))其中,TYPE_WIDTH以位为单位给出类型的大小:
/* The width in bits of the integer type or expression T.
Do not evaluate T.
Padding bits are not supported; this is checked at compile-time below. */
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)它通过取一个类型的最大值的补码来获得该类型的最小值。它可以通过以下两种方式获得最大值:
虽然你的问题并不是真的需要它,但我想看看那个签名的案例。让我们以int16_t为例。
如果我们提前处理三元和TYPE_WIDTH,并忽略额外括号以确保更多的安全性,TYPE_MAXIMUM(int16_t)将扩展为:
(int16_t) ((((int16_t) (1 << 14)) - 1) * 2 + 1)然后:
(int16_t) ((((int16_t) 0x4000) - 1) * 2 + 1)然后:
(int16_t) (((int16_t) 0x3FFF) * 2 + 1)然后:
(int16_t) ((int16_t) 0x7FFE + 1)然后:
(int16_t) ((int16_t) 0x7FFE + 1)这就是:
(int16_t) 0x7FFFTYPE_MAXIMUM是一种有效地设置除高位以外的所有位的方法,而不依赖于带符号的溢出,这会产生未定义的行为,因此不能使用。
此TYPE_MAXIMUM方法确实假设给定的任何有符号类型都不使用这样的模型,在该模型中,最大有符号值不是由所有位表示,而是由设置的最高位表示。
TYPE_MINIMUM(int16_t)将反转这些位以获得(int16_t) 0x8000。
这个来自最大值方法的TYPE_MINIMUM假设任何给定的有符号类型都不使用符号和幅度算术,或者不使用任何其他模型,其中最大保持值的补码不是最小值。
有了对签名表示的这两个限制,在this table中给出的可以与TYPE_MINIMUM宏一起使用的表示是一补和二补。
在实践中,这可能永远不会是一个问题,几乎所有的东西都使用有符号整数的2的补码表示。
https://stackoverflow.com/questions/57202578
复制相似问题