我要求在 C 中提供与此 C++ 宏等效的代码(不一定是宏,也可以是函数)
下面是代码在 C++ 伪代码中的使用方式,它实际上可能用宏编译,但宏在 C 中不起作用。不能在 C 中使用模板。
我不太明白它是不是像%
模数一样工作?
int v47; // ecx@78
bool v48; // sf@78
unsigned char v49; // of@78
int v79; // [sp+18h] [bp-5438h]@68
v47 = *(unsigned int *)(v2 + 65292);
v49 = __OFSUB__(v79 + 1, v47);
v48 = v79++ + 1 - v47 < 0;
char *playerra; // [sp+24h] [bp-6D0h]@20
int v26; // esi@32
unsigned __int8 v28; // of@32
v28 = __OFSUB__(playerra + 1, v26);
v27 = (signed int)&(playerra++)[-v26 + 1] < 0;
bool v10; // sf@24
unsigned __int8 v11; // of@24
int v54; // [sp+14h] [bp-508h]@19
v11 = __OFSUB__(v54 + 1, 40);
v10 = v54++ - 39 < 0;
对于 C 它是这样声明的
// For C, we just provide macros, they are not quite correct.
#define __OFSUB__(x, y) invalid_operation // Generate overflow flag for (x-y)
宏定义如下。
// overflow flag of subtraction (x-y)
template<class T, class U> int8 __OFSUB__(T x, U y)
{
if ( sizeof(T) < sizeof(U) )
{
U x2 = x;
int8 sx = __SETS__(x2);
return (sx ^ __SETS__(y)) & (sx ^ __SETS__(x2-y));
}
else
{
T y2 = y;
int8 sx = __SETS__(x);
return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2));
}
}
用这个
// sign flag
template<class T> int8 __SETS__(T x)
{
if ( sizeof(T) == 1 )
return int8(x) < 0;
if ( sizeof(T) == 2 )
return int16(x) < 0;
if ( sizeof(T) == 4 )
return int32(x) < 0;
return int64(x) < 0;
}
然后用这个
typedef char int8;
typedef signed char sint8;
typedef unsigned char uint8;
typedef short int16;
typedef signed short sint16;
typedef unsigned short uint16;
typedef int int32;
typedef signed int sint32;
typedef unsigned int uint32;
typedef __int64 int64;
typedef __int64 sint64;
typedef unsigned __int64 uint64;
最佳答案
由于您的问题中有一个具体案例,因此似乎对模板没有任何需求。所以我在这里所做的就是用 int
替换类型名,并且我还删除了一半的 OFSUB,因为你的参数类型在大小上是匹配的。
// overflow flag of subtraction (x-y)
int8 __OFSUB__(int x, int y)
{
int y2 = y;
int8 sx = __SETS__(x);
return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2));
}
// sign flag
int8 __SETS__(int x)
{
if ( sizeof(int) == 1 )
return int8(x) < 0;
if ( sizeof(int) == 2 )
return int16(x) < 0;
if ( sizeof(int) == 4 )
return int32(x) < 0;
return int64(x) < 0;
}
关于c++ - IDA PRO 将 C++ 代码转换为 C 代码 __OFSUB__ 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23169119/