- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
I don't see how going through the FPU would help, as there are no unsigned floating point instructions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
"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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Well, the principle will be the same for other sizes.
Regards,
Arjen

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page