Deepanjan Mazumdar
Deepanjan Mazumdar

Reputation: 1507

Any predefined type for unsigned 96 bit integer?

I am working on a feature which needs to keep track of a bit-map of length 96. I use this map to program asic below. Is there a standard integer type to hold 96 bits. For 64 bits, we have unsigned long long. Anything similar for 96 bits? Any alternate suggestion welcome as well.

PS: This is Cisco OS based on linux. Language is C.

Upvotes: 2

Views: 6125

Answers (4)

xilpex
xilpex

Reputation: 3237

You should use clang's new feature _ExtInt. It is in clang 11, so you may not have it. Look here for details and usage.

Upvotes: 0

qwertz
qwertz

Reputation: 14792

There is a way to create one variable that size is exact 96 bits: Bitfields in a union or struct.

typedef union { // you can use union or struct here
    __uint128_t i : 96;
} __attribute__((packed)) uint96_t;

uint96_t var;  // new uint96_t variable
var.i = 123;   // set the value to 123 (for example)

This worked for me with gcc. If you test the size of uint96_t with sizeof you should get 12 bytes (12 * 8 = 96).

Upvotes: 3

user1277476
user1277476

Reputation: 2909

I'd probably go with an array of 3 uint's. That should be fast enough, and not a lot more complex.

EG, to set a bit:

wordNo = i / 32
bitNo = i - (32*wordNo)
mask = 2 ** bitNo

array[wordNo] |= mask

...or thereabout.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

GCC has __int128 for 128-bit integers on targets that support it, but nothing for 96 bits specifically.

Upvotes: 3

Related Questions