64-bit integer operations (+, -, *, /)

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
For a new project I need to implement a set of routines to work with 64-bit integers (only basic operations: +, -, *, /).
Any ideas, solutions, etc?
PS: NO floats (double) allowed! :wink:

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm--Carl Sagan might be impressed:"billions and billions" isn't good enough for you. :)

Almost all multi-byte arithmetic primitives are extensible to more bytes. + & - should be failrly straightforward; twin longs might even work well if you can capture the carry/borrow in between.

Another up-front decision is: unsigned only? signed and unsigned? mixed?

Sounds like fun.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Another up-front decision is: unsigned only? signed and unsigned? mixed?

Unsigned only.
For unsigned long the max. is: 4,294,967,295
In (certain) countries with high level of inflation this level is overflowed... The accounting systems often deals with large amounts, over 5 billions (or even more!).
BTW: I wonder if it's possible to implement it in pure C (no asm)...

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I wonder if it's possible to implement it in pure C (no asm)...

Possible, but almost surely not efficient. C has no concept of carry/borrow so you need to virtually do each operation twice. I suppose you >>could<< manufacture the carry/borrow by doing 8 bits at a time, but use 16-bit arithmetic and look at the higher "overflow" bit of the result.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Stanley

What compiler are you using? WinAvr respectively AVR-GCC does have support for 64 bit arithmetics, just define your vars as "signed long long" respectively "unsigned long long"

Regards, Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Peter wrote:
What compiler are you using? WinAvr respectively AVR-GCC does have support for 64 bit arithmetics, just define your vars as "signed long long" respectively "unsigned long long"

Hi Peter!
... ouch, 10 points go straight to the gcc...
Unfortunately, I'm using CV that doesn't have such "long long"-vars... :cry:
[But I don’t feel like migrating right now…]

BTW: Does IAR have 64-bit integer operations?

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You and I are obviously paying too much for our compilers, Stanley. ;)

[Note to EW: Round 48 of Compiler Wars.]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
You and I are obviously paying too much for our compilers, Stanley. ;)

:lol: I'll ask Pavel for a discount! :lol:
Well... 150 is a decent price for a pretty good compiler like CV.
Now seriously: Pavel has done a very good job, really!
Can't ask the compiler to do all the job for me... you know what I mean.

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

Last Edited: Tue. Nov 29, 2005 - 11:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:twisted: Peace for this beautiful world of C-Compilers... :twisted:

I would just give 7 points for the gcc 64 bit arithmetics because of some lack in speed! AFAIK, IAR has also 64 Bit Arithmetics, I guess evidently faster!

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
AFAIK, IAR has also 64 Bit Arithmetics, I guess evidently faster!

I suppose it's also declared as: long long var_name;

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, Ok. But I'll go easy. :)

AFAIK, both IAR and GCC has an implementation of 64-bit integers in C. Yes, you specify "long long", or, if you #include , then you can use the types: uint64_t, int64_t.

I can't speak about the other compilers, as I just don't know. You could probably visit their websites for more information.

Eric Weddington

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, I know it's not nice... it is called treason...
But, at least for this project, I will have to migrate from CV to a compiler that has the built-in 64-bit integers... (God, I hate underscores... but this is off-topic anyway).
My budget is... [clearing my throat...] so IAR is out of discussion.

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
You could probably visit their websites for more information.

BTW: take a look at this!
http://www.iar.com/index.php?sho...
Can you believe it?!? 68% !!!
I think there must be a mistake... :?

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

- It all depends on the applications that you are testing.
- Note that they did not declare what compiler options that they are using.
- They did not give out links to the source code of the tests so independent tests can be done.
- They are comparing against AVR GCC 3.4.1, when really the state of the art is a much later version.

However, it is interesting to note that they even took the time to do the comparison. It must mean that AVR GCC is popular enough to warrant their time to take a look at it. ;)

Eric

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The 'compiler x generates $BIGNUM$% more code than compiler y' is always dangerous and most often marketing.
When I write c code for a particular project I choose constructs that can be efficiently implemented in the processor in question (no variable shifts if the processor doesn't have a variable-count-shift, for example) and that the chosen compiler can implement efficiently. I learn from the compiler's output. In other words, if IAR adapted/wrote the code in question, they will, conciously or not, have written it in a way that their compiler can translate efficiently. GCC has a few cases where it DOES generate lots of code if you aren't guiding it along gently with a crowbar, and if they consistently hit those because their compiler is good at it, yes, they could get +68%.

I've seen a programmer fill an ATMega169's 16k code space with ~1000 lines of C, and subsequently modified the C so it was less than 4k and ran 6x faster. That was with IAR, the programmer in question just didn't understand what the AVR could and couldn't efficiently do and what constructs the compiler liked. Visual C/x86, yes, IAR/AVR, no.