Skip to content

Alternative for minimum and maximum integer value constants #212

Open
@chqrlie

Description

@chqrlie

The c2 module defines the following constants:

i8 min_i8 = -128;
i8 max_i8 = 127;
u8 min_u8 = 0;
u8 max_u8 = 255;
i16 min_i16 = -32768;
i16 max_i16 = 32767;
u16 min_u16 = 0;
u16 max_u16 = 65535;
i32 min_i32 = -2147483647-1;
i32 max_i32 = 2147483647;
u32 min_u32 = 0;
u32 max_u32 = 4294967295;
i64 min_i64 = -9223372036854775807-1;
i64 max_i64 = 9223372036854775807;
u64 min_u64 = 0;
u64 max_u64 = 18446744073709551615;

#if (ast.getWordSize() == 4)

i32 min_isize = -2147483647-1;
i32 max_isize = 2147483647;
u32 min_usize = 0;
u32 max_usize = 4294967295;

#else

i64 min_isize = -9223372036854775807-1;
i64 max_isize = 9223372036854775807;
u64 min_usize = 0;
u64 max_usize = 18446744073709551615;

#endif

This poses multiple problems:

  • to use them, one must import the c2 module, which prevents the definition of variables called c2, a rather common name if you already have c and c1...
  • they are constants, but the name does not start with an uppercase letter
  • the name is not very intuitive: here are some common equivalents in other languages:
    • C: INT_MIN, INT32_MIN...
    • C++: std::numeric_limits<int32_t>::min(), ...
    • Java: Int.MIN_VALUE, ...
    • C#: Int32.MinValue, ...
    • go: math.MinInt32, ...
    • Rust: i32::MIN, ...

The solution used in Rust is quite appealing and would translate in C2 as i32.MIN, etc. These constants would be always defined along a number of type functions for each builtin type such as:

i32 i32.abs(i32 v);
i32 i32.min(i32 a, i32 b);  // for lack of a more general alternative i32 i32.min(...)
i32 i32.max(i32 a, i32 b); // same

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions