Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: _BitInt(N) Date: Mon, 24 Nov 2025 05:06:33 -0800 Organization: None to speak of Lines: 34 Message-ID: <87a50b4m1y.fsf@example.invalid> References: <10dajlh$ko3c$1@dont-email.me> <10fus62$hl69$1@solani.org> <10fv2dm$3can9$1@paganini.bofh.team> <10fv40v$1f7a2$1@dont-email.me> <20251123170654.000056a9@yahoo.com> <10g18hq$28nc2$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 24 Nov 2025 13:06:34 +0000 (UTC) Injection-Info: dont-email.me; posting-host="497198ffef236bf75350d11e6379045b"; logging-data="2535543"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+N7dinc7ObAsIliE6WW/6w" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:ygxlvL5Zp52EIxpjT7mGAq9aILQ= sha1:gns9rNrF4qohIf5V+tqRzCjEPuI= Xref: csiph.com comp.lang.c:395411 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.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */