c - How to determine integer types that are twice the width as `int` and `unsigned`? -
values of intermediate multiplication typically need twice number of bits inputs.
// example int foo(int a, int b, int carry, int rem) { int2x c; // type twice wide @ `int` c = (int2x)a * b + carry; return (int) (c % rem); }
considering potential padding, (which appears limit sizeof()
usefulness) , non-2`s complement integers (which limits bit dibbling), ...
does following create needed type?
if not, how code @ least reasonable solution, if not entirely portable?
#include <limits.h> #include <stdint.h> #if long_max/2/int_max - 2 == int_max typedef long int2x; typedef unsigned long unsigned2x; #elif llong_max/2/int_max - 2 == int_max typedef long long int2x; typedef unsigned long long unsigned2x; #elif intmax_max/2/int_max - 2 == int_max typedef intmax_t int2x; typedef uintmax_t unsigned2x; #else #error int2x/unsigned2x not available #endif
[edit]
qualify:"always", if long
, long long
, intmax_t
, not work ok #error
.
want know if @ least 1 of long
, long long
, or intmax_t
work, int2x
correctly typed?
notes: above assumes xxx_max
odd power-of-2 minus 1. maybe assumption? above works on @ least 2 platforms, hardly great portability test.
the assumption *_max constants of form (2^n)-1
valid. see 6.2.6 representations of types, , particularly 6.2.6.2 integer types, representations of unsigned integer types , positive values of signed integer types defined pure binary, yielding maximum 1 less power of two.
Comments
Post a Comment