# How to write a C code in 64-bit fixed point?

Status
Not open for further replies.

#### boeysue

##### Full Member level 3
How to write a 64-bit fixed point C code in unix ?Because in unix ,the operation of 64-bit fixed point is not right...So can anyone tell me how to write a 64-bit fixed point operation in C code!thanks for help!

#### echo47

64-bit integer syntax tends to be non-portable on 32-bit compilers. Which compiler are you using, and what problem are you seeing? Post a small program that demonstrates the problem.

This works in MinGW GCC 3.4.2, but the long long, LL, and %I64d may be different on your compiler:
Code:
#include <stdio.h>
int main(void)
{
long long a=1122334455667788LL, b=1000;
printf("product=%I64d\n", a*b);
return 0;
}

product=1122334455667788000

#### boeysue

##### Full Member level 3
64-bit long long
the operation with shift and + seems to be wrong.
Because I use the server to change the 32-bit directly to 64-bit,It seems to be saturated on 2^16.
So I just think the ansi didn't support the operation of 64-bit operation.
By the way,What is the answer to help me of the shift/add operation on 64-bit!
Thanks a lot.

#### echo47

What do you mean, use server to change 32-bit to 64-bit??

ANSI doesn't specify the number of bits. That's compiler dependent.

#### boeysue

##### Full Member level 3
Actually I just do a cic decimation filter just as my decimation is 128 and my stage is 5th order,then I see the output of the bitstream stick to 2^16.
By the way,How to use the cicdecim command on matlab?The IWL/OWL setting seems strange ,maybe I get the wrong idea about it.
plz help me..

#### echo47

I've never used that MATLAB filter. Type this command to see lots of info and examples:
doc mfilt.cicdecim

#### boeysue

##### Full Member level 3
Actually I use +1/-1 as input ,and setting IWL = 1,there is many warning message.
But if I use IWL = 2,the output bitstream seams error..
So I make confused of the introduction of mathworks.

#### muko

##### Member level 1
union {
unsigned long x;
unsigned long y;
};

this is a 64-bit integer is it right?

#### bilgekaan

##### Full Member level 2
union {
unsigned long x;
unsigned long y;
};

this is a 64-bit integer is it right?

no unsigned long x and y resides in the same place . size of struct is equal to

sizeof longest data type .

sizeof(union { unsigned long x; unsigned long y; }) = sizeof( unsigned long );

you can define like this

typedef struct
{
unsigned long upperPart;
unsigned long lowerPart;
} int_64_Bits;

but you cannot use this structure directly in arihtmetic operations. you mast define custom operators or functions

#### boeysue

##### Full Member level 3
mmm..
Do you mean it need to take apart to two 32-bit part?
But how to do the operation of add and multiply?
Especially multiply....

#### boeysue

##### Full Member level 3
How to use two part in unix without assembly?
Because the carry-out bit after the operation of addition,It is the problem I couldn't understand.
Plz help.

#### arturt134

##### Full Member level 2
Look at the:
There is definition of <inttypes.h> header file.

#### boeysue

##### Full Member level 3
Can you express more clearly?
Which parameter or variables did it use for that?
Because I print the __WORDSIZE ,it is 32.I dont use it for extending for 64-bit operation.Thanks for that.

this seems problem...
long long x=0x01<<33;
printf("%x\n",x);

But I don't know why the following is correct:
long long x=0x01;

printf("%x\n",x<<33);

Thanks for help lorh..
It seems to bother me so long long time.

By the way,the compiler I used is gcc 2.96.

mmm..
the machine is i686.//uname -m
compiler is gcc-2.96.//man gcc

thanks for help ...

#### echo47

Boeysue, at last you are giving us some information! But it's still not enough.

If you compile that with the -Wall option (full warnings), gcc should warn you that your printf has inconsistent int format and argument type. Your %x tells printf to assume an int argument (apparently 32-bits in your compiler). Try %llx or %I64x instead. I can't guess the exact syntax, because it depends on your operating system and precise compiler version (type gcc --version).

Ok, i686 means it's a Pentium box. 32-bit integers as you determined. I haven't used that exact compiler, but %llx is a good candidate. Try it!

gcc -Wall myprogram.c

#### boeysue

##### Full Member level 3
this seems problem...
long long x=0x01<<33;
printf("%x\n",x);

But I don't know why the following is correct:
long long x=0x01;

printf("%x\n",x<<33);

Actually,In long long x=0x01<<33;//<---- this happens to be wrong...
the warning message is left shift is more than width of the type.
But when i print sizeof long long it is 8. So I am confused of this.

#### echo47

long long x=0x01<<33;
No, 0x01 is a 32-bit int on your compiler, so shifting it by 33 bits gives you trouble. You will have better luck with this:
long long x=0x01LL<<33;

What did this output on your compiler?
long long x=0x01;
printf("%x\n",x<<33);

I would expect "0" or maybe "2" but not the desired result "200000000"

I'm guessing that this will produce the expected result "200000000":
long long x=0x01;
printf("%llx\n",x<<33);

### boeysue

Points: 2

#### boeysue

##### Full Member level 3
mmm...
That's right...It helps me not to doing many nonuseless thing for work.Because I see many document not to recommand to write more than 32 bit code.So the above discussion helps me much more ,thanks.

#### boeysue

##### Full Member level 3
Does there exit any detailed document about gcc's tools?thanks for help :sm9: