473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

32x32 and 64x64 signed integer multiplication

Hi group,

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?

Thanks in advance,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi
Nov 14 '05 #1
17 7973
Christopher Dyken <fo****@uio.no> wrote:
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.


What about:
- for each signed number, check whether it's signed, check whether these
signs are equal, and remember this fact.
- set both sign bits to +.
- multiply as if they're unsigned.
- check whether the result is too large for a signed number, and if so,
do what you've decided to do on overflow.
- if the sign bits were equal, leave the number as it is. If they
weren't, set its sign bit.

Hardest part is probably deciding what to do on overflow. Crashing is
harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
error flag is probably best.

Richard
Nov 14 '05 #2
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| Christopher Dyken <fo****@uio.no> wrote:
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

| What about:
| - for each signed number, check whether it's signed, check whether these
| signs are equal, and remember this fact.
| - set both sign bits to +.
| - multiply as if they're unsigned.
| - check whether the result is too large for a signed number, and if so,
| do what you've decided to do on overflow.
| - if the sign bits were equal, leave the number as it is. If they
| weren't, set its sign bit.

| Hardest part is probably deciding what to do on overflow. Crashing is
| harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
| error flag is probably best.

Hi Richard,

I have no problem limiting the input values (I define the interval
myself), and thus, I can be sure that no overflows occour. So this is
not an issue.

As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I've an intuitive feeling
that it should be possible to do this without any conditionals.

Regards,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi
Nov 14 '05 #3
Christopher Dyken wrote:

rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| Christopher Dyken <fo****@uio.no> wrote:
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

| What about:
| - for each signed number, check whether it's signed, check whether these
| signs are equal, and remember this fact.
| - set both sign bits to +.
| - multiply as if they're unsigned.
| - check whether the result is too large for a signed number, and if so,
| do what you've decided to do on overflow.
| - if the sign bits were equal, leave the number as it is. If they
| weren't, set its sign bit.

| Hardest part is probably deciding what to do on overflow. Crashing is
| harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
| error flag is probably best.

Hi Richard,

I have no problem limiting the input values (I define the interval
myself), and thus, I can be sure that no overflows occour. So this is
not an issue.

As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I've an intuitive feeling
that it should be possible to do this without any conditionals.

Regards,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi


Pipeline stalls (because of branches) are machine dependent. Some cpu'sh
ave two pipelines, one for each branch, so it isn't an issue. I myself
prefer to use decisionless code, if at all possible. You might want
to see my article "Avoid Decisions!" in Computers in Physics ca. 1990
or 1991, which discusses the general idea. If you are doing it in
assembly it becomes easier, of course, but it is possible to avoid
(some) branches in high-level C.

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything and thereby take away
our free will and that share of glory that rightfully belongs
to us." -- N. Machiavelli, "The Prince".
Nov 14 '05 #4
Christopher Dyken wrote:

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?


Your grade III arithmetic text book?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
Christopher Dyken <fo****@uio.no> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| Christopher Dyken <fo****@uio.no> wrote:
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

| What about:
| - for each signed number, check whether it's signed, check whether these
| signs are equal, and remember this fact.
| - set both sign bits to +.
| - multiply as if they're unsigned.
| - check whether the result is too large for a signed number, and if so,
| do what you've decided to do on overflow.
| - if the sign bits were equal, leave the number as it is. If they
| weren't, set its sign bit.

As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I've an intuitive feeling
that it should be possible to do this without any conditionals.


Well, reading the sign bits might be nothing but bitwise manipulations.
The rest certainly _can_ be done conditionless if multiplies are cheap.
For example:

#define SIGN_BIT ((unsigned type)CHAR_BIT*sizeof(signed type)-1)
signed type s_mult(signed type x, signed type y)
{
unsigned type ux,uy, sbx,sby, signbit;
signed type result;

ux=x; uy=y;
sbx=ux>>SIGN_BIT; sby=uy>>SIGN_BIT;
signbit=sbx^sby;
ux*=1-2*sbx; uy*=1-2*sby;
result=u_mult(ux, uy);
result*=1-2*signbit;

return result;
}

This does assume that your 32-bit type is or behaves as a normal C
integer, including the conversion of negative signed values to unsigned;
and the #define of SIGN_BIT assumes no trap bits, so if you have those,
you'll have to #define your own sign position. Oh, and it requires
<limits.h>, of course.

Richard
Nov 14 '05 #6
CBFalconer <cb********@yahoo.com> writes:

| Christopher Dyken wrote:

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?

| Your grade III arithmetic text book?

Unfortunately, I don't have this for reference, but either the
educational standards in Norway are particulary weak, or you went to
Mensa-camp or something as a kid, since I cannot recall learning
anything about casting between unsigned and signed ints as well as
signed binary numbers in ground school.

Sarcasm aside, the problem is not to expand (a*2^16+b)*(c*2^16+d) to
ac*2^32 + (ad+bc)*2^16 + bd, but how to program this correctly using
32-bits signed integers in C.
Regards,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi
Nov 14 '05 #7
Christopher Dyken wrote:
CBFalconer <cb********@yahoo.com> writes:
| Christopher Dyken wrote:

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?

| Your grade III arithmetic text book?

Unfortunately, I don't have this for reference, but either the
educational standards in Norway are particulary weak, or you went to
Mensa-camp or something as a kid, since I cannot recall learning
anything about casting between unsigned and signed ints as well as
signed binary numbers in ground school.

Sarcasm aside, the problem is not to expand (a*2^16+b)*(c*2^16+d) to
ac*2^32 + (ad+bc)*2^16 + bd, but how to program this correctly using
32-bits signed integers in C.


I don't know how Norway got into this, but what do you need to
know other than the multiplication table 1 x 1 = 1 and 1 x 0 = 0
and 0 x 0 = 0, together with the fact that like signs yield
positive products and unlike signs yield negative products. You
don't even have to worry about carries until you add partial
products. Get the data structure and algorithm right and the code
will fall out.

Tips: you can often improve things by breaking the operation up
and using results that conveniently fit into registers. Then a
final addition operation combines them all. Read Knuth.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #8
CBFalconer <cb********@yahoo.com> writes:
| Christopher Dyken wrote:
CBFalconer <cb********@yahoo.com> writes:
| Christopher Dyken wrote:
>>
>> I'm trying to implement two routines to handle 32x32-bits and
>> 64x64-bits signed integer multiplication on a 32 bits machine in C. It
>> easy to find descriptions of non-signed multiplication, however, I
>> haven't found any good descriptions for the signed counterpart.
>>
>> Does any of you have a good reference for this topic?
>

| Your grade III arithmetic text book?

Unfortunately, I don't have this for reference, but either the
educational standards in Norway are particulary weak, or you went to
Mensa-camp or something as a kid, since I cannot recall learning
anything about casting between unsigned and signed ints as well as
signed binary numbers in ground school.

Sarcasm aside, the problem is not to expand (a*2^16+b)*(c*2^16+d) to
ac*2^32 + (ad+bc)*2^16 + bd, but how to program this correctly using
32-bits signed integers in C.

| I don't know how Norway got into this, but what do you need to

You referenced my grade III arithmetic book, and since I'm Norwegian,
and thus a product of the Norwegian School system, Norway got into
this. :-P
| know other than the multiplication table 1 x 1 = 1 and 1 x 0 = 0
| and 0 x 0 = 0, together with the fact that like signs yield
| positive products and unlike signs yield negative products. You
| don't even have to worry about carries until you add partial
| products. Get the data structure and algorithm right and the code
| will fall out.

So what you suggest is to implement the bit-by-bit multiplication and
then try to lump together operations afterwards?
| Tips: you can often improve things by breaking the operation up
| and using results that conveniently fit into registers. Then a
| final addition operation combines them all. Read Knuth.

I browsed through Knuth (art of programming) at the library some days
ago. However, I couldn't find anything on signed multiplication (found
quite some stuff on unsigned, though). I think it was one of the early
editions.

Cheers,
Chris

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi
Nov 14 '05 #9
CBFalconer <cb********@yahoo.com> scribbled the following:
Christopher Dyken wrote:
CBFalconer <cb********@yahoo.com> writes:
| Your grade III arithmetic text book?

Unfortunately, I don't have this for reference, but either the
educational standards in Norway are particulary weak, or you went to
Mensa-camp or something as a kid, since I cannot recall learning
anything about casting between unsigned and signed ints as well as
signed binary numbers in ground school.
I don't know how Norway got into this, but what do you need to
know other than the multiplication table 1 x 1 = 1 and 1 x 0 = 0
and 0 x 0 = 0, together with the fact that like signs yield
positive products and unlike signs yield negative products. You
don't even have to worry about carries until you add partial
products. Get the data structure and algorithm right and the code
will fall out.


Christopher Dyken appears to be Norwegian.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It's time, it's time, it's time to dump the slime!"
- Dr. Dante
Nov 14 '05 #10
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I've an intuitive feeling
that it should be possible to do this without any conditionals.

| Well, reading the sign bits might be nothing but bitwise manipulations.
| The rest certainly _can_ be done conditionless if multiplies are cheap.
| For example:

[... code omitted ...]
| This does assume that your 32-bit type is or behaves as a normal C
| integer, including the conversion of negative signed values to unsigned;
| and the #define of SIGN_BIT assumes no trap bits, so if you have those,
| you'll have to #define your own sign position. Oh, and it requires
| <limits.h>, of course.

Thanks, that looks interesting! Hmmm, I wonder where the break-even
point between conditionals and multiplications is; well only some
timing will answer that. :-)

Regards,
Chris.

--
email address available at http://www.ifi.uio.no/~erikd/index.cgi
Nov 14 '05 #11
Christopher Dyken <fo****@uio.no> wrote in message news:<x6*************@lachesis.uio.no>...

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.


If you mimic twos complement, then you can just use the unsigned
multiplication methods. BTW, don't assume specific widths for integer
types.

--
Peter
Nov 14 '05 #12
CBFalconer <cb********@yahoo.com> wrote in message news:<40***************@yahoo.com>...
Christopher Dyken wrote:

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?


Your grade III arithmetic text book?


[And Richard called _me_ churlish! ;-]

Granted the question wasn't really suitable for clc since it leaned
towards algorithms and a request for links (rather than discussing ISO
C directly), nonetheless, this response isn't exactly constructive.

Programming is not limited to people who 'did well at maths in
school'. Not every computational trick is 'intuitively obvious from
first principles'. Like most languages, C hides the tedious issues of
implementation of digit arithmetic (e.g. long is still 32+ bits on 8
and 16-bit machines).

Why do you think extending integer arithmetic is 'elementary'?

--
Peter
Nov 14 '05 #13
Christopher Dyken wrote:
CBFalconer <cb********@yahoo.com> writes:
| Christopher Dyken wrote:
CBFalconer <cb********@yahoo.com> writes:
| Christopher Dyken wrote:
>>
>> I'm trying to implement two routines to handle 32x32-bits and
>> 64x64-bits signed integer multiplication on a 32 bits machine in C. It
>> easy to find descriptions of non-signed multiplication, however, I
>> haven't found any good descriptions for the signed counterpart.
>>
>> Does any of you have a good reference for this topic?
>
| Your grade III arithmetic text book?

Unfortunately, I don't have this for reference, but either the
educational standards in Norway are particulary weak, or you went to
Mensa-camp or something as a kid, since I cannot recall learning
anything about casting between unsigned and signed ints as well as
signed binary numbers in ground school.

Sarcasm aside, the problem is not to expand (a*2^16+b)*(c*2^16+d) to
ac*2^32 + (ad+bc)*2^16 + bd, but how to program this correctly using
32-bits signed integers in C.

| I don't know how Norway got into this, but what do you need to

You referenced my grade III arithmetic book, and since I'm Norwegian,
and thus a product of the Norwegian School system, Norway got into
this. :-P

| know other than the multiplication table 1 x 1 = 1 and 1 x 0 = 0
| and 0 x 0 = 0, together with the fact that like signs yield
| positive products and unlike signs yield negative products. You
| don't even have to worry about carries until you add partial
| products. Get the data structure and algorithm right and the code
| will fall out.

So what you suggest is to implement the bit-by-bit multiplication and
then try to lump together operations afterwards?

| Tips: you can often improve things by breaking the operation up
| and using results that conveniently fit into registers. Then a
| final addition operation combines them all. Read Knuth.

I browsed through Knuth (art of programming) at the library some days
ago. However, I couldn't find anything on signed multiplication (found
quite some stuff on unsigned, though). I think it was one of the early
editions.


int neg;
unsigned long a, b;
whatever p; /* something that can hold a 64 bit product */
....
neg = (a < 0L) ^ (b < 0L));
p = product(labs(a), labs(b)); /* however you are doing it */
/* test p for overflow */
if (neg) p = negate(p);

you build product and negate.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #14
Mac
On Tue, 17 Feb 2004 15:14:47 +0100, Christopher Dyken wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| Christopher Dyken <fo****@uio.no> wrote:
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

| What about:
| - for each signed number, check whether it's signed, check whether these
| signs are equal, and remember this fact.
| - set both sign bits to +.
| - multiply as if they're unsigned.
| - check whether the result is too large for a signed number, and if so,
| do what you've decided to do on overflow.
| - if the sign bits were equal, leave the number as it is. If they
| weren't, set its sign bit.

| Hardest part is probably deciding what to do on overflow. Crashing is
| harsh. Ignoring is perhaps sufficient in many circumstances. Setting an
| error flag is probably best.

Hi Richard,

I have no problem limiting the input values (I define the interval
myself), and thus, I can be sure that no overflows occour. So this is
not an issue.

As I see it, this strategy involves some if/else-tests, which can harm
the pipeline throughput (or am I wrong?) . I've an intuitive feeling
that it should be possible to do this without any conditionals.


The turnaround on usenet can be kind of slow. It is worth your while to
compose messages with care. If you had a requirement for not using
conditionals, you should have mentioned it in your original post.
Regards,
Chris.

Mac

Nov 14 '05 #15
Christopher Dyken <fo****@uio.no> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
| This does assume that your 32-bit type is or behaves as a normal C
| integer, including the conversion of negative signed values to unsigned;
| and the #define of SIGN_BIT assumes no trap bits, so if you have those,
| you'll have to #define your own sign position. Oh, and it requires
| <limits.h>, of course.

Thanks, that looks interesting! Hmmm, I wonder where the break-even
point between conditionals and multiplications is; well only some
timing will answer that. :-)


Quite. Be prepared for the difference to be smaller than the measurement
error, too.

Richard
Nov 14 '05 #16
Christopher Dyken <fo****@uio.no> wrote in message news:<x6*************@lachesis.uio.no>...
Hi group,

I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.

Does any of you have a good reference for this topic?

Thanks in advance,
Chris.


The limitation of 32X32 in C is that the result is limited to 32
bits (the lower half result). The correct result is 64 bits. The full
64 bit result can only be obtained by breaking the operands into 16
bit sections and performing partial multiplies and long long sums (64
bit) to obtain the full 64 bit result. This increases the execution
time compared to the 32x32 -> 32 bit result.

Bill Hanna
Nov 14 '05 #17

(snip)
Sarcasm aside, the problem is not to expand (a*2^16+b)*(c*2^16+d) to
ac*2^32 + (ad+bc)*2^16 + bd, but how to program this correctly using
32-bits signed integers in C.


John R. Ehrman: Correction to ``logical'' arithmetric on computers with
two's complement binary arithmetic. Commun. ACM 13(11): 697-698 (1970)

John R. Ehrman: "Logical" arithmetic on computers with two's complement
binary arithmetic. Commun. ACM 11(7): 517-520 (1968)

-- glen

Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tom Goulet | last post by:
Hello, My question basically is: What is the opposite of the following? | "%08X" % -1 I want to convert a string of hexadecimal characters to the signed integer they would have been before...
2
by: REH | last post by:
If the is_modulo field of the numeric_limits class is true for signed integer types, can I assume that overflow for such types is defined behavior? If so, is the behavior the same regardless of...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
8
by: Alex Fraser | last post by:
Can negating a non-negative signed integer value ever overflow? Put another way, can it be true that (mathematically) INT_MIN > -INT_MAX, LONG_MIN > -LONG_MAX etc? I know that typically it can't...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
9
by: helPlease | last post by:
hello !! Can anybody provide me an algorithm or C code for long integer multiplication.It's urgent.
4
by: Mamluk Caliph | last post by:
The following code executes as I would expect on gcc: #include <inttypes.h> #include <stdio.h> int main(int argc, char **argv){ uint16_t apa = 10000; uint16_t kaka = 7000;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.