From nobody Sat Jul 9 17:04:56 2022 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 8FC1A1CF8D44 for ; Sat, 9 Jul 2022 17:05:04 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LgGjX2dnTz3l9L; Sat, 9 Jul 2022 17:05:04 +0000 (UTC) (envelope-from dim@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657386304; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=j1Ya2znd0KcrMAuuxI66AMspJ4gWyPjdIOMkdhzMYlQ=; b=fsGbWboUW8TWTQg4M2P8EWmrWHjNQxic8uhKiUVNpa2FM3GSIMgn7HeMBjijzArUwzH2mC NPk3flGjIxwXGPx90d8icOq4sX+eWTfRtN1/HhbhmjlRTpwNpu59usvlJqLhINPWlDf4KE SKKIT/4lsy1Tq2zHEmtS8uXoCkJHOTKa0YrKYh/e5pgTX9GEgglkuxjcjpG2VcQU7aK2n6 iQRpa+qhsaQJaKw50OUxsO8ltv8YMQZsIK6/eKsxrt31U1XaN6bIF+hq05pJlTwlKyIVhB IXPdBGWGhfbqyTDX3O5mR7j97hO8YnicZoBaumMZxGbR4a02Cw7lJIziXI6tyw== Received: from tensor.andric.com (tensor.andric.com [87.251.56.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "tensor.andric.com", Issuer "R3" (verified OK)) (Authenticated sender: dim) by smtp.freebsd.org (Postfix) with ESMTPSA id 4LgGjX0ytQzt19; Sat, 9 Jul 2022 17:05:04 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from smtpclient.apple (longrow.home.andric.com [192.168.0.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id BE5AC59198; Sat, 9 Jul 2022 19:05:02 +0200 (CEST) Content-Type: multipart/signed; boundary="Apple-Mail=_19D6C1EC-0B51-42A1-83A4-80531A847B04"; protocol="application/pgp-signature"; micalg=pgp-sha1 List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) Subject: Re: Canonical way / best practice for 128-bit integers From: Dimitry Andric In-Reply-To: <86CB4C22-9CAB-4578-8F11-962B4B08F756@bumblingdork.com> Date: Sat, 9 Jul 2022 19:04:56 +0200 Cc: freebsd-hackers@freebsd.org Message-Id: <903B6B84-4AFE-4BB5-94F5-5DFD65BAC1B6@FreeBSD.org> References: <86CB4C22-9CAB-4578-8F11-962B4B08F756@bumblingdork.com> To: Marc Veldman X-Mailer: Apple Mail (2.3696.100.31) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657386304; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=j1Ya2znd0KcrMAuuxI66AMspJ4gWyPjdIOMkdhzMYlQ=; b=iaiII6zWBcwzKNNbdwxuzk0upIDWjaYKB5o13NKeaivAp4ZiulP8oZtLjBQm0tWuZARhfS PJyBE3+rfYk46fv4z+/5qsiH5iOr2vbfJGTxWT/mnYqImf7ZzaFME0Q8EgQWBL6VdQHDlD 8U10xVAlkOzivpq/aeFIavOleKzljKlZtufFDfu29/9Fx7bIEwwmGN1vNNC5RQtTvWSetd dzp54SnCzgJitIVMB9eSS2FKQZEVrPAtoD4pD14jOugp8wjaCRDb32W81QxDF9VDuLdIhg fVMDqWhe8a3njPIZyp1eJB7ruysxlmrkM6aYMJ4yi2lHrIjBitXly2XcoHTB8w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1657386304; a=rsa-sha256; cv=none; b=w5Mvo7UyBXume93lmeHWmzcpnQS839d8601uaAk8RInr9EF0kfU3F4hDRYmSsrlqv+MBy7 etxx5NFZtAt+UbaZwkSUv8c3YeOLhD9r/cWQ4BFT5d3sJOF02gtykI5A/9E4SH/7PFoB0F joanCopudCzHfKG+6vAfRW0X2rg1yTprWi6rcsWDcVSVouZwivPPGhjwezvGTUj4VO6YKB waazaZixMSeGyRFKTbQpoUcGo9SEWtyLUDSyRIkWYD2stzx/mo1ACWCRvWQg8ne14AXfMI tU95et1KryUg9j3uxnx5txj3jAn1QU6+MbwSPb0B0FtU7WHGpqYHgJwTH3/48g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N --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 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--