Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

boeysue

Full Member level 3
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
Tell us your compiler version and show us your non-working code so we can help you.

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
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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
Joined
Mar 3, 2004
Messages
37
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
317
union {
unsigned long x;
unsigned long y;
};

this is a 64-bit integer is it right?
 

bilgekaan

Full Member level 2
Joined
Nov 18, 2004
Messages
125
Helped
15
Reputation
30
Reaction score
1
Trophy points
1,298
Location
Turkiye
Activity points
1,300
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
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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....
Thanks for answering.
 

boeysue

Full Member level 3
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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
Joined
Dec 31, 1999
Messages
125
Helped
8
Reputation
16
Reaction score
8
Trophy points
1,298
Location
Poland
Activity points
1,006
Look at the:
There is definition of <inttypes.h> header file.
 

boeysue

Full Member level 3
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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.

Added after 7 minutes:

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.

Added after 12 minutes:

mmm..
the machine is i686.//uname -m
compiler is gcc-2.96.//man gcc
and how to get more information in command?

thanks for help ...
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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).

... addition ...

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!

I don't understand "how to get more information in command?". Maybe you are asking about -Wall. I run the compiler like this:
gcc -Wall myprogram.c
 

boeysue

Full Member level 3
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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
    Helpful Answer Positive Rating

boeysue

Full Member level 3
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
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.
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176

boeysue

Full Member level 3
Joined
Dec 23, 2004
Messages
188
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,523
Does there exit any detailed document about gcc's tools?thanks for help :sm9:
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top