Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should enums be downsized? #149

Open
tehKaiN opened this issue Oct 21, 2021 · 2 comments
Open

Should enums be downsized? #149

tehKaiN opened this issue Oct 21, 2021 · 2 comments

Comments

@tehKaiN
Copy link
Member

tehKaiN commented Oct 21, 2021

Currently, most Amiga compilers represent enums as 32-bit ints. GCC compilers have -fshort-enums switch, but it comes with a tradeoff:

Allocate to an enum type only as many bytes as it needs for the declared range of possible values. Specifically, the enum type is equivalent to the smallest integer type that has enough room.

Warning: the -fshort-enums switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface.

also, -fshort-enums is placed on the most not recommended build flags on one of the blog posts I've found: https://interrupt.memfault.com/blog/best-and-worst-gcc-clang-compiler-flags#-fshort-enum

some things to consider:

  • binary compatibility doesn't affect us - this will obviously break when linking with stuff compiled without that switch, but all ACE-based games don't have this option - usually they come with all required sources and no binary blobs,
  • extra ops to mask out the remaining bytes of the register doesn't affect us, since m68k has .b, .w and .l op variants
  • structs changing sizes doesn't affect us because ACE don't even tries to maintain compatibility
    I'm still kinda torn about this, but I think having small enums is the way to go - should save some ram space in structs

Still, I have no benchmarks to prove that it doesn't truly affect the performance. One thing to consider is that 68000 has 16-bit data bus and it's always slower to transfer 32-bit data between memory and regs.

Needs more research and benchmarks!

@tehKaiN tehKaiN changed the title Should enum be downsized? Should enums be downsized? Oct 21, 2021
@sirwhinesalot
Copy link

Not sure if it helps but I've seen this pattern in some C code in the wild:

typedef uint8_t my_enum_t; enum {
  FOO = 1,
  BAR = 2
  ...
}

Basically you don't use the enum in function arguments, you only ever use the properly sized type.
C++ lets you define the size which is a lot nicer (as does C23 now).

I would say if not the above pattern, -fshort-enums is the way to go.

@tehKaiN
Copy link
Member Author

tehKaiN commented Oct 30, 2022

Looks cool! The only downside is that intellisense will show FOO as part of anonymous enum and there will be a lost opportunity to put proper values in the first place of suggestion list when typing tMyEnum eValue = .

I'll probably stick to C23 stuff falling back to this construct using some macro magic to keep the compatibility with Bebbo compiler. Like:

#if OLD_C
#define SIZED_ENUM(name, backingType) typedef backingType name; enum 
#else
#define SIZED_ENUM(name, backingType) typedef enum name: backingType
#endif

But I'll have to see how vscode's intellisense behaves with that once C23 ships. On the side note, docs for this one C23 feature are very scarce and are only in this one writeup. I guess the topic is still too fresh. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants