home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,088 of 243,242   
   Michael S to Keith Thompson   
   Re: _BitInt(N)   
   24 Nov 25 15:27:36   
   
   From: already5chosen@yahoo.com   
      
   On Mon, 24 Nov 2025 05:06:33 -0800   
   Keith Thompson  wrote:   
      
   > David Brown  writes:   
   > [...]   
   > > Yes, exactly.  At the call site, the size of the _BitInt type is   
   > > always a known compile-time constant, so it can easily be passed on.   
   > > Thus :   
   > >   
   > > 	_BitInt(N) x;   
   > > 	_BitInt(M) y;   
   > > 	_BitInt(NM) z = x * y;   
   > >   
   > > can be implemented as something like :   
   > >   
   > > 	__bit_int_signed_mult(NM, (unsigned char *) &z,   
   > > 			N, (const unsigned char *) &x,   
   > > 			M, (const unsigned char *) &y);   
   >   
   > That looks like it's supposed to avoid overflow (I'm assuming NM is N   
   > + M), but it wouldn't work.  The type of a C expression is almost   
   > always determined by the expression itself, regardless of the context   
   > in which it appears. The type of x * y is _BitInt(max(N, M)), not   
   > _BitInt(N+M), so it can overflow even if the full result would fit   
   > into z.   
   >   
   > You can do this instead (not tested):   
   >   
   >     _BitInt(N) x;   
   >     _BitInt(M) y;   
   >     _Bit_Int(N+M) z = (_BitInt(N+M))x * y;   
   >   
   > (I'm assuming N+M is sufficient, but I might have missed an off-by-one   
   > error somewhere.)   
   >   
      
   You missed nothing. N+M is both sufficient and necessary. The latter   
   because of -(2**(N-1)) * -(2**(M-1)).   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca