Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Jul 2022 19:04:56 +0200
From:      Dimitry Andric <dim@FreeBSD.org>
To:        Marc Veldman <marc@bumblingdork.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Canonical way / best practice for 128-bit integers
Message-ID:  <903B6B84-4AFE-4BB5-94F5-5DFD65BAC1B6@FreeBSD.org>
In-Reply-To: <86CB4C22-9CAB-4578-8F11-962B4B08F756@bumblingdork.com>
References:  <86CB4C22-9CAB-4578-8F11-962B4B08F756@bumblingdork.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_19D6C1EC-0B51-42A1-83A4-80531A847B04
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

On 9 Jul 2022, at 18:26, Marc Veldman <marc@bumblingdork.com> wrote:
>=20
> I=E2=80=99m working on some bluetooth code, and that involves handling =
128-bit uuids.
> There are various ways to handle in the FreeBSD codebase, like in =
sdp.h:
>=20
> /*
> * SDP int128/uint128 parameter
> */
>=20
> struct int128 {
>        int8_t  b[16];
> };
> typedef struct int128   int128_t;
> typedef struct int128   uint128_t;
>=20
> and in sys/dev/random/uint128.h
>=20
> #ifdef USE_REAL_UINT128_T
> typedef __uint128_t uint128_t;
> #define UINT128_ZERO 0ULL
> #else
> typedef struct {
>        /* Ignore endianness */
>        uint64_t u128t_word0;
>        uint64_t u128t_word1;
> } uint128_t;
> static const uint128_t very_long_zero =3D {0UL,0UL};
> #define UINT128_ZERO very_long_zero
> #endif
>=20
> Is there any recommended / standard way to handle 128 bit integers in =
a portable way?

Of course recent compilers have built-in support for __int128_t and
__uint128_t, but it comes with a *lot* of caveats: not supported on all
architectures, definitely never on 32-bit ones, differences between
compilers, and even compiler versions, etc etc.

If you want it portable, the only way is to handle them as two 64 bit
integers, and leave any arithmetic or conversion to library routines.

-Dimitry


--Apple-Mail=_19D6C1EC-0B51-42A1-83A4-80531A847B04
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.2

iF0EARECAB0WIQR6tGLSzjX8bUI5T82wXqMKLiCWowUCYsm1OAAKCRCwXqMKLiCW
o5I3AKDzzb2cukXMkAtdkHeual+eLBrfoACg4amAZVsEloIRb3Ja2XQu9NfOqm8=
=1lVZ
-----END PGP SIGNATURE-----

--Apple-Mail=_19D6C1EC-0B51-42A1-83A4-80531A847B04--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?903B6B84-4AFE-4BB5-94F5-5DFD65BAC1B6>