[comp.sys.mac.programmer] Overflow Checking from within Pasca

liberte@zaphod.ncsa.uiuc.edu (10/04/88)

This is not an answer to your question about doing overflow checking
on longints.  However, the following suggestion may help someone who is
trying to do unsigned integer arithmetic in LSP.  

My need was to do arithmetic on the unsigned integer components of
colors.  There is no way to declare unsigned integers, and in a recent
conversation with Symantec support people I learned that there is no
plan to support unsigned integers in the next version of Pascal.  

An integer value that has the high bit set is
considered negative.  One bit pattern in particular is an illegal value
which you cannot do arithematic on: 32768=$8000.  However, you can cast
it to a longint, but then the sign is extended.  The simple solution is
to zero out the upper word of the longint using BitAnd with $0000FFFF
as an argument.   Then do your arithmetic and finally select the low
word of the result with LoWord.

This all works.  But there is one final problem.  You cannot assign the
value 32768 back into an integer variable.  I have no solution to this
problem other than turning overflow checking off.  But unfortunately, I
discovered that I cannot turn off this overflow checking, even if I
turn it off for the whole project.  Is this a bug?

In my case, I can check for this illegal value while it is still
longint and subtract one before taking the LoWord.  But this is
an ugly hack, and LSP is broken when it comes to unsigned integers.

Dan LaLiberte
National Center for Supercomputing Applications
liberte@ncsa.uiuc.edu

singer@endor.harvard.edu (Rich Siegel) (10/04/88)

In article <900006@zaphod> liberte@zaphod.ncsa.uiuc.edu writes:
>This all works.  But there is one final problem.  You cannot assign the
>value 32768 back into an integer variable.  I have no solution to this
>problem other than turning overflow checking off.  But unfortunately, I
>discovered that I cannot turn off this overflow checking, even if I
>turn it off for the whole project.  Is this a bug?

	Overflow checking checks for integer overflow during computations,
not during assignments. If you want to assign a large unsigned value
to an integer variable, turn off the "R" compile option, or use {$R-}.

>In my case, I can check for this illegal value while it is still
>longint and subtract one before taking the LoWord.  But this is
>an ugly hack, and LSP is broken when it comes to unsigned integers.

	No, Lightspeed Pascal is *not* "broken" when it comes to
unsigned integer. Pascal simply has no provisions for supporting
unsigned arithmetic, as C does. In any pascal, the only way to work
with unsigned values is to disable overflow and range checking.

>Dan LaLiberte

		--Rich




Rich Siegel
Staff Software Developer
THINK Technologies Division, Symantec Corp.
Internet: singer@endor.harvard.edu
UUCP: ..harvard!endor!singer
Phone: (617) 275-4800 x305

Any opinions stated in this article do not necessarily reflect the views
or policies of Symantec Corporation or its employees.

barry@reed.UUCP (Barry Smith) (10/06/88)

In article <429@husc6.harvard.edu> singer@endor.UUCP (Rich Siegel) writes:
>... Pascal simply has no provisions for supporting
>unsigned arithmetic, as C does. In any pascal, the only way to work
>with unsigned values is to disable overflow and range checking.
>
>		--Rich

Pascal allows specification of "unsigned integers", although there is no
specification of the mapping to the underlying hardware, of course. The
declaration "var I: 0..65535" will declare an unsigned range of integers,
ahd the declaration "type unsigned = 0..65535" specifies a new type that
should, in a conforming implementation, range-check properly.

There are several Pascal compilers that will perform the obvious mapping
to the underlying hardware, although Lightspeed Pascal is apparently not
one of these.  (Nor is MPW Pascal, alas.)

Barry Smith
Blue Sky Research

liberte@zaphod.ncsa.uiuc.edu (10/11/88)

>	Overflow checking checks for integer overflow during computations,
>not during assignments. If you want to assign a large unsigned value
>to an integer variable, turn off the "R" compile option, or use {$R-}.

Wait a second.. You say I should turn off Overflow checking with the
Range checking option?  Of course, I should have guessed.  But in that case
what does {$V-} do?

Using R-V-, I still cannot turn off overflow checking for the following
expression: 128 * 256
But, in a way, I was glad I couldnt because it was my error to not
cast to a longint.  The only reason I had tried to turn
off this overflow check was because it was part of an unsigned integer
assignment.  (The smallest gradularity for changing overflow checking is
the line since one cannot put comments in the middle of lines.)

While I am at it, it is not possible to put comments in the Observe window
to affect how that code is compiled.  Thus I cannot easily debug code
that depends on turning off range checking, for example.  A case in point,
cSpecArray is declared as an Array [0..0] of ColorSpec because it is
of variable length.  Aside from the difficulty of using this in code,
the Observe window does not allow any other index but 0 which is not
very useful.

Dan LaLiberte