Question

I'm asking for a equivalent to this C++ macro in C (doesn't have to be a macro could be a function)

Here is how the code is used in a C++ pseudo code which actually probably compiles with the macro, but the macro doesn't work in C. Can't use templates in C.

I didn't quite understand it does it work like % modulus?

  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;

For C it's declared like this

// For C, we just provide macros, they are not quite correct.
#define __OFSUB__(x, y) invalid_operation // Generate overflow flag for (x-y)

Here is how the macro is defined.

// 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));
  }
}

Uses this

// 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;
}

And that uses this

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;
Was it helpful?

Solution

As you have one specific case in your question, there doesnt seem to be any demand for templates. So all I did here was replacing the typenames with ints and I also removed half of OFSUB as your parameter types are matching in size.

// 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top