I am trying to define 4-bit, 20-bit, 24-bit and 36-bit unsigned integers in C in simplest way.
- No bitfiels
- Portability is not an issue
- Memory space is not an issue either
- C99 and later is fine
I am trying to define 4-bit, 20-bit, 24-bit and 36-bit unsigned integers in C in simplest way.
Assuming what you said in comments below your question, you're facing several problems:
uint4_t
, you'll need a uint8_t
and a uint20_t
will need a uint32_t
. Then, in case of underflow/overflow, you'll set your emulated CPU flags accordingly.bool
(8 bits). So, your uint4_t
will become, internally, a char[4]
, so you'll be able to either access individual bits AND to manipulate it as a single element. But for uint36_t
, it will starts to be quite "fun" to use and you'll face some other problems on your host machine, like cache being invalidated too often and alignment problems. And you'll need to write all basic operations for these types, anyway, you won't be able to rely on C ones (unlike what is possible when using the shortest type above, see first point).CPU emulation is not the easiest thing to do, and trying to compare performances of an emulated CPU to another emulated CPU is usually totally irrelevant. It's somewhat possible because you try to compare variants of the same CPU, but you'll need a talented hardware engineer to give you all execution times for all emulated instructions before trying to do anything else.