Ttelmah
Joined: 11 Mar 2010 Posts: 19810
|
|
Posted: Wed Mar 12, 2014 2:08 am |
|
|
You never need to shift for this!....
Look at how the cast32x64 is done. It writes the 32bit value given into the low 32bits of the target, and then writes '0' to the high 32bits. Done through the union. Key nice thing is that this is exactly how it would be done if you did have 64 bit support. Problem is if you just copy the value into a variable, the compiler doesn't realise it has the extra four bytes needing clearing...
If however you want to print a 64bit value, you have to do all the printout arithmetic in 64bit. Not terribly hard, but needs thought.
Where the 64bit form is really useful, is if you are starting with 32bit values, and want to end up with a 32bit result, since then the existing 32bit routines can be used to do the print (accessing 64bitvariable.w[0], which is the low 32bit part), but this can't be used, unless the value _is_ 32bit at the end, otherwise the most important part is being lost!...
Just as a demonstration, to show how this works:
Code: |
void main()
{
int64 test1,test2,b;
int32 fout,a;
//lcd_init(); //printing to serial on my chip
a=343597386;
fout=20000000;
b=cast32x64(10000000); //write the low 32 bits into a 64bit variable
for(;;)
{
test1=mult32x32(fout,a);
test2=div64x64(test1,b);
printf("\f%lu",test2.w[0]); //print the low 32bits
delay_ms(100);
//now to make something different happen
a-=6;
}
}
|
Merrily prints 687194772 on the first pass. Going 'through' a 64bit intermediate value (test1), of 68717947720000000!....
Then keeps going with the reducing 'a' value. |
|