From: Keith.S.Thompson+u@gmail.com   
      
   bart writes:   
   > On 24/11/2025 09:29, David Brown wrote:   
   >> On 23/11/2025 16:06, Michael S wrote:   
   >>> On Sun, 23 Nov 2025 13:59:59 +0000   
   >>> bart wrote:   
   >   
   >>>> So what is the result type of multiplying values of those two types?   
   >>>>   
   >>>   
   >>> I think, traditional C rules for integer types apply here as well: type   
   >>> of result is the same as type of wider operand. It is arithmetically   
   >>> unsatisfactory, but consistent with the rest of language.   
   >> There is one key difference between the _BitInt() types and other   
   >> integer types - with _BitInt(), there are no automatic promotions to   
   >> other integer types. Thus if you are using _BitInt() operands in an   
   >> arithmetic expression, these are not promoted to "int" or "unsigned   
   >> int" even if they are smaller (lower rank). If you mix _BitInt()'s   
   >> of different sizes, then the smaller one is first converted to the   
   >> larger type.   
   >   
   >>> I think, the Standard is written in such way that implementing _BitInt   
   >>> as an arbitrary precision numbers, i.e. with number of bits held as part   
   >>> of the data, is not allowed.   
   >   
   >> Correct. _BitInt(N) is a signed integer type with precisely N value   
   >> bits. It can have padding bits if necessary (according to the   
   >> target ABI), but it can't have any other information.   
   >>   
   >>> Of course, Language Support Library can be   
   >>> (and hopefully is, at least for gcc; clang is messy a.t.m.) based on   
   >>> arbitrary precision core routines, but the API used by compiler should   
   >>> be similar to GMP's mpn_xxx family of functions rather than GMP's   
   >>> mpz_xxx family, i.e. # of bits as separate parameters from data arrays   
   >>> rather than combined.   
   >>>   
   >> 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;   
   >   
   > So what is NM here; is it N*M (the potential maximum size of the   
   > result), or max(N, M)?   
      
   I made the same mistake in my previous post, but corrected it before   
   posting it. The required size for the product in N+M bits, not N*M.   
   For example, N=32, M=64 -> NM=96.   
      
   [...]   
      
   > How would you write a generic user function that operates on any size   
   > BitInt? For example:   
   >   
   > _BitInt(?) bi_square(_BitInt(?));   
      
   I don't think you can. Each _BitInt(N) type is distinct.   
      
   You could have a function that operates on arguments of type   
   [unsigned] _BitInt(BITINT_MAXWIDTH) and depend on implicit   
   conversions, but that's likely to be horribly inefficient.   
      
   Or you can replace BITINT_MAXWIDTH by the maximum width you happen to   
   need in your application.   
      
   [...]   
      
   --   
   Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com   
   void Void(void) { Void(); } /* The recursive call of the void */   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|