<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
I propose that integer signed overflow always raise an immediate exception.<BR>
Word.T should either raise an exception for unsigned overflow, or maybe no exceptions at all.<BR>
For folks that want silent wraparound, maybe a separate interface like UnsafeInt.<BR>
<BR>
<BR>
I propose that FloatMode's integer features not be the way forward.<BR>
<BR>
<BR>
There are two implementation strategies.<BR>
<BR>
<BR>
- "check the carry flag"<BR>
A processor-specific thing, but possibly something easy for the gcc backend to do.<BR>
And very likely easy for the integrated backend.<BR>
Probably very efficient.<BR>
<BR>
<BR>
- internally generate function calls for every arithmetic operation<BR>
like how sets are implemented<BR>
<BR>
<BR>
Implementing most such functions is easy enough in portable C.<BR>
Or maybe using Modula-3 and interface Word.<BR>
<BR>
<BR>
e.g.<BR>
void add(int a, int b, int* c, int* overflow)<BR>
{<BR>
int d = (a + b);<BR>
/* overflow if input signs are equal and output sign is different;<BR>
if input signs are unequal, overflow is not possible<BR>
positive + positive: expect positive, else overflow<BR>
negative + negative: expect negative, else overflow<BR>
positive + negative: overflow not possible */<BR>
*overflow = ((a < 0) == (b < 0) && (d < 0) != (a < 0));<BR>
*c = d;<BR>
}<BR>
<BR>
<BR>
void sub(int a, int b, int* c, int* overflow)<BR>
{<BR>
int d = (a - b);<BR>
/* overflow if input signs are unequal and output sign is different than first input;<BR>
if input signs are equal, overflow is not possible;<BR>
positive - negative: expect positive, overflow if negative<BR>
negative - positive: expect negative, overflow if positive<BR>
positive - positive, negative - negative: overflow not possible */<BR>
*overflow = ((a < 0) != (b < 0) && (d < 0) != (a < 0));<BR>
*c = d;<BR>
}<BR>
<BR>
#include <limits.h><BR>
<BR>
void mult(int a, int b, int* c, int* overflow)<BR>
{<BR>
/* do operation in higher precision and check if it fits */<BR>
int64 d = (a * (int64)b);<BR>
*c = (int)d;<BR>
*overflow = (d >= INT_MIN && d <= INT_MAX);<BR>
}<BR>
<BR>
/* for interface Word */<BR>
typedef unsigned uint;<BR>
<BR>
void addu(uint a, uint b, uint* c, int* overflow)<BR>
{<BR>
uint d = (a + b);<BR>
/* overflow if output less than either input */<BR>
*overflow = (d < a);<BR>
*c = d;<BR>
}<BR>
<BR>
void subu(uint a, uint b, uint* c, int* overflow)<BR>
{<BR>
uint d = (a - b);<BR>
/* overflow if output greater than first input */<BR>
*overflow = (d > a);<BR>
*c = d;<BR>
}<BR>
<BR>
void multu(uint a, uint b, uint* c, int* overflow)<BR>
{<BR>
/* operate at higher precision and see if it fits */<BR>
uint64 d = (a * (uint64)b);<BR>
*overflow = (d <= UINT_MAX);<BR>
*c = (uint)d;<BR>
}<BR>
<BR>
void multLU(uint64 a, uint64 b, uint64* c, int* overflow)<BR>
{<BR>
/* break it down into smaller operations, not shown, but not difficult */<BR>
}<BR>
<BR>
<BR>
Yes I know this is inefficient, but such is a possible price of portable safety.<BR>
<BR>
<BR>
A hybrid is probably possible if the gcc backend support must be processor specific and we gradually provide the implementations.<BR>
<BR>
<BR>
- Jay<BR><BR><BR><BR><BR><BR> </body>
</html>