1

This is an 8 bit architecture, with a word size of 16 bits. I now need to use a 48-bit integer variable. My understanding is that libm implements 8, 16, 32, 64 bit operations (addition, multiplication, signed and unsigned).

So in order to make calculations, I must store the value in a 64-bit signed or unsigned integer. Correct?

If so, what is there to prevent general routines from being used? For example, for addition:

  1. start with the LSB of both variables
  2. add them up
  3. if more bytes are available continue, otherways goto ready
  4. shift both variables 1 byte to the right
  5. goto 1)
CassOnMars
  • 6,153
  • 2
  • 32
  • 47
Vorac
  • 8,726
  • 11
  • 58
  • 101

2 Answers2

3

libm implements the routines for the standard sizes of types, and the compiler chooses the right one to use for expression.

If you want to implement your own types, you can. If you want to use the usual operators, then you have to get into the compilation process to get the compiler to choose yours.

You could implement the operations as functions, say add(int48_t, int48_t), but then the compiler won't be able to do optimizations like constant folding, etc.

So, there is nothing stopping you from implementing your own custom compiler, but is it really necessary? Do you really need to save that space? If so, then go for it!

UncleO
  • 8,299
  • 21
  • 29
2

That is correct, saving a couple of bits is (in almost all cases) not worth the trouble of implementing your own logic.

Von Lion
  • 748
  • 4
  • 22
  • 1
    What is to prevent writing of general math routines, just as pupils are taught in school, i.e. {start with the LSB of the two variables; add them up; if more bytes are avaible, remember the carry and do the same.} – Vorac Mar 04 '13 at 09:50
  • Operations on entire words in a CPU are done in 1 clock cycle, if you want to go bit-by-bit, you'd easily spend 50 fold as much time doing calculations :-) – Von Lion Mar 04 '13 at 10:02
  • Basically, the CPU [instruction set](http://en.wikipedia.org/wiki/Instruction_set) works on words – Von Lion Mar 04 '13 at 10:06
  • Ok, my word size is 2 bytes. 48 bits is 6 bytes long. Why no procedure to work on it? – Vorac Mar 04 '13 at 15:33
  • Because you are an exception :-) Most people are fine with a multitude of word-size for their integers. Nothing will stop you from implementing your own logic, but it would most probably be (much) slower than the 'normal' way of doing it. – Von Lion Mar 04 '13 at 16:43