c2nim icon indicating copy to clipboard operation
c2nim copied to clipboard

c2nim fails to remove "LL" suffixes on types

Open omentic opened this issue 3 years ago • 1 comments

c2nim correctly identifies the size of integers appended with LL or similar, but fails to remove those suffixes in the generated code.

Here is an example:

static inline double wl_fixed_to_double(wl_fixed_t f) {
	union {
		double d;
		int64_t i;
	} u;
	u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
	return u.d - (3LL << 43);
}
proc wl_fixed_to_double*(f: wl_fixed_t): cdouble {.inline.} =
  type
    INNER_C_UNION_wayland-util_616 {.bycopy, union.} = object
      d: cdouble
      i: int64_t

  var u: INNER_C_UNION_wayland-util_616
  u.i = ((1023LL'i64 + 44LL'i64) shl 52) + (1LL'i64 shl 51) + f
  return u.d - (3LL'i64 shl 43)

omentic avatar Jul 29 '22 03:07 omentic

I've ran into the same issue while evaluating c2nim performance for embedding into Futharks macro translation. This test case from Futhark:

#define TEST_PLAIN_INT                     (123)
#define TEST_PLAIN_LONG                    (-2147483648L)
#define TEST_PLAIN_LONGLONG                (-9223372036854775808LL)

#define TEST_PLAIN_FLOAT_1                 (123.123f)
#define TEST_PLAIN_FLOAT_2                 (.123f)
#define TEST_PLAIN_FLOAT_3                 (123.f)
#define TEST_PLAIN_FLOAT_4                 (-.0f)
#define TEST_PLAIN_FLOAT_5                 (-0.f)
#define TEST_PLAIN_FLOAT_6                 (0.0f)

#define TEST_PLAIN_DOUBLE_1                (123.0)

#define TEST_PLAIN_LONG_DOUBLE_1           (100.0L)

Turns into this by c2nim:

const
  TEST_PLAIN_INT* = (123)
  TEST_PLAIN_LONG* = (-2147483648'i32)
  TEST_PLAIN_LONGLONG* = (-9223372036854775808'i64)
  TEST_PLAIN_FLOAT_1* = (123.123f)
  TEST_PLAIN_FLOAT_2* = (0.123f)
  TEST_PLAIN_FLOAT_3* = (123.f)
  TEST_PLAIN_FLOAT_4* = (-0.0f)
  TEST_PLAIN_FLOAT_5* = (-0.f)
  TEST_PLAIN_FLOAT_6* = (0.0f)
  TEST_PLAIN_DOUBLE_1* = (123.0)
  TEST_PLAIN_LONG_DOUBLE_1* = (100.0L)

So it works for L and LL for integer values, but fails on f and L on floats.

It does seem that the original issue with leaving the LL when adding 'i64 is gone though.

PMunch avatar Jan 22 '25 16:01 PMunch