J
James Harris
On a 16-bit C compiler which supports up to 32-bit integers but no larger
how feasible is it to support 64-bit integer values and is there a good way
to do so? Best I can think of is to pass structs around principally because,
AIUI, they can be returned from functions. But is there a better way?
Such a struct would be along the lines of
struct custom64 {
uint32_t low;
uint32_t high;
};
typedef struct custom64 custom64_t;
(For the 64-bit compiler this would instead be "typedef uint64_t
custom64_t;".)
I know I could pass around pointers-to-64-bit-integer but that would
probably result in some cases of needing malloc to store the values and lead
to more complexity. So structs seem better if they would work. I can live
with some inconvenience like not being able to specify literals and not
having printf formats for them.
This is for writing some C where it would be a big help if some of the
source code modules could be compiled to 16-bit object code and to 64-bit
object code and yet still work on 64-bit integers where necessary. So if it
were possible to write the following and just have the above different
definitions of the custom64_t type that would be especially useful.
custom64_t val1, val2;
val1 = func(val2);
I think I'll only need such explicitly long integers in rare cases and won't
need to do much arithmetic on them so it probably wouldn't be too burdensome
to manipulate the few references by callable routines if necessary. In other
words I can live without
val1 - val2
and replace it with
custom64_subtract(val1, val2);
It's early days but I think the main issues will be declaring them and
passing them to and from other functions. Arithmetic would be nice-to-have
but I cannot see any way to do that.
BTW, I should say I'm sure there are extensive libraries for wide number
manipulation but they are not what I want. Something short and simple that
will fit in a few lines would be much preferable to something pre-written
and extensive.
Am I on the right lines? Is there a 'standard' way to do stuff like this in
C?
James
how feasible is it to support 64-bit integer values and is there a good way
to do so? Best I can think of is to pass structs around principally because,
AIUI, they can be returned from functions. But is there a better way?
Such a struct would be along the lines of
struct custom64 {
uint32_t low;
uint32_t high;
};
typedef struct custom64 custom64_t;
(For the 64-bit compiler this would instead be "typedef uint64_t
custom64_t;".)
I know I could pass around pointers-to-64-bit-integer but that would
probably result in some cases of needing malloc to store the values and lead
to more complexity. So structs seem better if they would work. I can live
with some inconvenience like not being able to specify literals and not
having printf formats for them.
This is for writing some C where it would be a big help if some of the
source code modules could be compiled to 16-bit object code and to 64-bit
object code and yet still work on 64-bit integers where necessary. So if it
were possible to write the following and just have the above different
definitions of the custom64_t type that would be especially useful.
custom64_t val1, val2;
val1 = func(val2);
I think I'll only need such explicitly long integers in rare cases and won't
need to do much arithmetic on them so it probably wouldn't be too burdensome
to manipulate the few references by callable routines if necessary. In other
words I can live without
val1 - val2
and replace it with
custom64_subtract(val1, val2);
It's early days but I think the main issues will be declaring them and
passing them to and from other functions. Arithmetic would be nice-to-have
but I cannot see any way to do that.
BTW, I should say I'm sure there are extensive libraries for wide number
manipulation but they are not what I want. Something short and simple that
will fit in a few lines would be much preferable to something pre-written
and extensive.
Am I on the right lines? Is there a 'standard' way to do stuff like this in
C?
James