Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29242 Discussions

Do we have UNSIGNED arithmentic capability?

WSinc
New Contributor I
918 Views
Hello -

Supose I want to manipulate quantites that are NOT SIGNED.
For example a 1 byteinteger could have a range of 0 to 255.

Multiplying two of those could result in a two byte result from
0 to 255**2. I want to make sure there is no sign conversion when the result is stored.
Likewise for add, subtract, and divide.

Of course, for c=a-b, b cannot be >a, that would give an error indication.
Do we have a library of routines to do that?

Of course there is no problem with non-integer values.

One "klugey" solution would be to convert everything to floating point, then back again.
0 Kudos
6 Replies
mecej4
Honored Contributor III
918 Views
The only current Fortran compiler that I know to support unsigned integer arithmetic is Sun/Oracle Fortran for Solaris/Linux.

I don't see how going through the FPU would help, as there are no unsigned floating point instructions.
0 Kudos
TimP
Honored Contributor III
918 Views
You can obtain unsigned integer arithmetic on a narrower type by masking off the unwanted bits of a wider representation, e.g. by iand(i,z'ff'). Your desire to raise an error for a-b with b>a isn't consistent with usual definition of unsigned arithmetic, but you certainly could define an operator to do that within a wider signed type.
0 Kudos
WSinc
New Contributor I
918 Views
"masking off" would alter the values and give you the wrong answer.

If you wanted to mutlipy 128 times 128, masking those off would give a value of zero, not 16384.

Obviously the result has to be promoted to a higher number of bytes.

One of the really stupid things about this Fortran is that it does not take into account
that a multiply or add might not fit into the same byte space.

So you can get an integer overflow without knowing about it.
0 Kudos
Arjen_Markus
Honored Contributor II
918 Views
I have a module that deals with unsigned integers. (It should be part of my Flibs project at http://flibs.sf.net,
but I see I haven't checked it in yet - will have to correct that omission).

The idea is simple:
- Create a derived type with an integer component
- Define arithmetic operations for that type that implement unsigned arithmetic

I can send it to you if you want (or you can wait until I have put it up in the CVS repository of said project).
You will have to adjust it to support byte-sized integers, though.

Regards,

Arjen
0 Kudos
WSinc
New Contributor I
918 Views
That would be great if you could send it.

Actually, what I am looking for is different integer sizes, so

I could generalize whatever you're doing to 1,2,4, or 8 byte size
integers. 8 byte ones are tricky because of what I said before about
the result being too large.

Or I could have a 16 byte result of a special type.

But alsoi i could just generate an error message if the result down't fit.

Thanks; Bill S
0 Kudos
Arjen_Markus
Honored Contributor II
918 Views
Oops, my mistake: the stuff was already available in CVS. But only for a default-size integer type.
Well, the principle will be the same for other sizes.

Regards,

Arjen
0 Kudos
Reply