c - How to load the largest integer possible in one memory operation? -


i'm building small bytecode vm run on variety of platforms including exotic embedded , microcontroller environments.

each opcode in vm can variable length(no more 4 bytes, no less 1 byte). in interpreting opcodes, want create tiny "cache" current opcode. however, due being used on many different platforms, it's hard do.

so, here few examples of expected behavior:

  1. on 8-bit microcontroller 8-bit memory bus, i'd want load 1 byte because it'd take multiple (slow) memory operations load anymore, , in theory, might require 1 byte execute current opcode
  2. on 8086(16-bit), i'd want load 2 bytes because load 1 byte throwing useful data away read later, don't want load more 2 bytes because it'd take multiple operations
  3. on 32-bit arm processor, i'd want load 4 bytes because otherwise we're either throwing data that'd might have read again away, or we're doing multiple operations

i handled assuming unsigned int enough, on 8-bit avr microcontrollers, int defined 16-bit, memory data bus width 8 bit, 2 memory load operations required.

anyway, current ideas:

using uint_fast16_t seems work expected on platforms (32 bits on arm, 16 bits on 8086, 64 bits on x86-64). however, still leaves out avr , other 8-bit microcontrollers.

i thought using uint_fast8_t might work, appear on platforms it's defined being unsigned char, isn't optimal

also, there problem must solved well: unaligned memory access. on x86, isn't going problem(in theory 2 memory operations, it's cached away in hardware), on arm know doing unaligned 32-bit access possibly cost 3 times as single aligned 32-bit load. if address unaligned, want load aligned option , data possible, @ costs avoid memory operation

is there way somehow using magical preprocessor includes or such, or require manually defining optimum cache size before compiling platform?

there no automatic way using types or information provided standard c (in headers such , on).

problems such handled executing , measuring sample code on target platform , using results determine code use in practice. samples might executed during build , built final code or might executed @ start of each program execution , used duration of execution.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -