persenaama wrote:
Here's a brute-force peasant algorithm implementation:
uint32 mul(uint32 a, uint32 b)
{
uint32 v = 0;
for ( ; b; )
{
v += a & (0 - (b & 1));
a <<= 1;
b >>= 1;
}
return v;
}
It multiplies two 32 bit values, if you want 64 bit result you need to
write 64 bit addition and use "uint64" for v. Easiest way to do that is
something like this, since you don't have 64 bit integers:
struct uint64
{
uint32 v[2];
// .. operators you need here..
};
How you going to use the 64 bit result is up to you.. if you want
signed, too, it is fairly trivial.
This is not really answering your question, which is a bit mystry what
the problem is. Choosing the right types for compiler/language built-in
* operator? Or how to store and broadcast the user supplied
non-statically typed values to the built-in * operator?
By now, you should already have the code. I'm 99% sure that you do, so
the thread is closed? ;-o
I'm sorry, I didn't check this thread at all yesterday and that's why I
didn't respond to your posts. I don't have the code yet. Let me explain
the problem to you once more: I have a dialog box on which the user
select data types for 3 variables and an operation to be performed on
those variables. The data types I have are 8, 16 and 32 bit signed and
unsigned integers and floats and doubles; total of 8. That leads to
8*8*8 permutations if I was to code it with switch case or if else
statements. This of course is unacceptable and so what I do for now is
restrict the user to 2 data type options only.
I have some library functions which will return a void * to the 3
variables and I can get their data types from another function which
will return an enum of the data type. It is then my job to type cast
the void * to the appropriate data type. What I would like to have is
some mechanism that lets data types be decided at run time and a
general template like thing (I don't know much C++ so I'm just throwing
out terms I've learned in the past few days) that will let me do the
operation using the appropriate data type.
Someone mentioned double dispatch in this thread and I looked into that
but unfortuantely it is a little too complicated for me to understand
without delving deeper into C++. I've started doing that, bought a
couple of books and reading tutorials online but you guessed correctly
that there isn't much time allocated to this in the project and so I'm
doing this on my own time; so it's slow going. However, once I develop
this i could use it whenever I'm faced with a problem of this kind and
so it would be rather useful to get it working.
Also, this is part of my work, not a school project; so you're not
helping me do my homework :-)