Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t5RGt-0065kd-9P for pgsql-hackers@arkaria.postgresql.org; Mon, 28 Oct 2024 15:03:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t5RGr-007LH5-99 for pgsql-hackers@arkaria.postgresql.org; Mon, 28 Oct 2024 15:03:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t5RGq-007LGL-Qs for pgsql-hackers@lists.postgresql.org; Mon, 28 Oct 2024 15:03:05 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t5RGo-003HvG-K0 for pgsql-hackers@lists.postgresql.org; Mon, 28 Oct 2024 15:03:03 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-20cceb8d8b4so24723815ad.1 for ; Mon, 28 Oct 2024 08:03:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730127782; x=1730732582; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=XeSIuRJg9/EVrexZKPh6CjbkgeiFcnRm7bZj2w9igCA=; b=mAsjqVkEotF+LWkIu5Rd3FLDAF4Y9+r+g/godky+b8Zc48JKwBnaVf6Nw74h8StG+k 5e2zuCcUuMX0bZS/k5yI+Epid4vBR3MyADfCaiWkEaNvU3zvaRvt9EWknlIIungvzgUC NTlkZg04IdNsyC2eJOgWVdUK63U2JzIYV5NFS1/POGmV2vffcy4pu7bB513Br7LErJQA bA4xyDCOGgWnN4qdBH4/5jDGQ9taBh3TcM5IlMD/NGdSGmonapHexLlhNaFBuJ7KK7I6 Rfhh8lKr3cNiil/AMT1aokQK3z2TYkUoTUDrZ5xE+HlsCgku+d28/O9We9Yqluj8m1Kw N/PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730127782; x=1730732582; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XeSIuRJg9/EVrexZKPh6CjbkgeiFcnRm7bZj2w9igCA=; b=j+LQsO8/uQZIjdPjfEhrOyqD7VMWSVp3mgx5l4yKPG6eYiCm4jZkbShZHxWCd0p1/T tBOcrgjLkZVizHAvTuafuPLHdIx+4RV2GfbV7VO0kZ6lj6aEeFSpZs1f2RocMxRikXUo MOzlVLQd/KjCEBxBMGesHcawfFDLvWKWQzgS48XgFVJjwwwUIrePl6Qc9q8mae2X1VrD smCEVaHlZbaDsPo/dpLNiJNuA+dWwiRmvribO/RNKiBr8pzLzfIvyFEoFQzI/7Dvzbvf bkMS0EkoWuoLksfqX4xj1ukWn8oM9xcKJh/cqp0G3lTDF207dO1JxObgAjBj/vP2c0jL YO4w== X-Forwarded-Encrypted: i=1; AJvYcCWJ3q8gHrjit3gl//Pfp5Yu9D5966Bo+F2UWRndeF3cD7xWt0lOnLZ4L7fVCw8Rl+XUeZiDA/kUyxnriYy3@lists.postgresql.org X-Gm-Message-State: AOJu0Yy06ghVlLlUOtGNPkS/ybSK5f5a75hayTZwUQKds+jqMIn8JISq avm/Nfqr/GWHk84M46eGwMAWlKKf4d6NRKW85cF46ty1sZ/PHot7jrz1BW6QUt358On8ZY1Ksyr YBMbfIrYSq0WEIjTMZjwu1FkAw7c= X-Google-Smtp-Source: AGHT+IG7VFNLLRT0XfXohTAekqK12LVudZvhv+BbeSNDH/F2r3JZOJO/D9SW5o4etN3PkABG+4hFFCzboXB+v2g3opc= X-Received: by 2002:a17:902:ec87:b0:20b:831f:e8f7 with SMTP id d9443c01a7336-210e860f2f6mr1683105ad.11.1730127780156; Mon, 28 Oct 2024 08:03:00 -0700 (PDT) MIME-Version: 1.0 References: <3023d29e-0fe7-4bec-9028-7f3c0102e437@eisentraut.org> In-Reply-To: From: Ranier Vilela Date: Mon, 28 Oct 2024 12:02:48 -0300 Message-ID: Subject: Re: define pg_structiszero(addr, s, r) To: Heikki Linnakangas Cc: Bertrand Drouvot , Peter Eisentraut , pgsql-hackers@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000c0a02b06258ac301" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c0a02b06258ac301 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Em seg., 28 de out. de 2024 =C3=A0s 11:33, Heikki Linnakangas escreveu: > On 18/09/2024 21:57, Bertrand Drouvot wrote: > > On Wed, Sep 18, 2024 at 10:03:21AM +0200, Peter Eisentraut wrote: > >> On 18.09.24 06:16, Bertrand Drouvot wrote: > >>> +#define pg_structiszero(addr, s, r) > \ > >>> + do { > \ > >>> + /* We assume this initializes to zeroes */ > \ > >>> + static const s all_zeroes; > \ > >>> + r =3D (memcmp(addr, &all_zeroes, sizeof(all_zeroes)) =3D= =3D 0); > \ > >>> + } while (0) > > Not new with this patch, but do we guarantee padding bytes to be zeros? > > How about this instead: > > static inline bool > pg_is_all_zeros(const char *p, size_t len) > { > for (size_t i =3D 0; i < len; i++) > { > if (p[i] !=3D 0) > return false; > } > return true; > } > > It seems to me that this way is more optimized. static inline bool is_all_zeros(const char *p, size_t len) { for (size_t i =3D len; i >=3D 0; i--) { if (p[i] !=3D 0) return false; } return true; } main: sub rsp, 24 lea rdx, [rsp + 12] lea rcx, [rsp + 16] lea rdi, [rip + .L.str] lea rsi, [rsp + 8] xor eax, eax call __isoc99_scanf@PLT lea rdi, [rip + .L.str.1] xor esi, esi xor eax, eax call printf@PLT xor eax, eax add rsp, 24 ret best regards, Ranier Vilela --000000000000c0a02b06258ac301 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Em seg., 28 de out. de 2024 =C3=A0s 11:33, Heikki Linnakangas <hlinnaka@iki.fi> escreveu:
On 18/09/2024 21:57, Ber= trand Drouvot wrote:
> On Wed, Sep 18, 2024 at 10:03:21AM +0200, Peter Eisentraut wrote:
>> On 18.09.24 06:16, Bertrand Drouvot wrote:
>>> +#define pg_structiszero(addr, s, r)=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 \
>>> +=C2=A0 =C2=A0do {=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 \
>>> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* We assume this in= itializes to zeroes */=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 \
>>> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0static const s all_z= eroes;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 \
>>> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0r =3D (memcmp(addr, = &all_zeroes, sizeof(all_zeroes)) =3D=3D 0);=C2=A0 =C2=A0 =C2=A0 =C2=A0\=
>>> +=C2=A0 =C2=A0} while (0)

Not new with this patch, but do we guarantee padding bytes to be zeros?

How about this instead:

static inline bool
pg_is_all_zeros(const char *p, size_t len)
{
=C2=A0 =C2=A0for (size_t i =3D 0; i < len; i++)
=C2=A0 =C2=A0{
=C2=A0 =C2=A0 =C2=A0if (p[i] !=3D 0)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return false;
=C2=A0 =C2=A0}
=C2=A0 =C2=A0return true;
}

It seems to me that this way is more optimized.
=

static inl= ine bool
is_all_ze= ros(const char *p, size_t len)
{
= =C2=A0 for (size_t i =3D len; i >=3D 0; i--)
=
=C2=A0 {
=C2=A0 =C2=A0 if (p[i] !=3D 0)<= /div>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return false;
= =C2=A0 }
=C2=A0 return true;
}

main:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 sub =C2=A0 =C2=A0 rsp<= span style=3D"color:rgb(0,0,0)">, 24
=C2=A0 =C2=A0 =C2=A0= =C2=A0 lea =C2=A0 =C2=A0 rdx, [rsp + 12= ]
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 lea =C2=A0 =C2=A0 rcx, [rsp + 16]=
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 lea =C2=A0 =C2=A0 r= di, [rip + .L.str]
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 lea =C2=A0 =C2=A0 rsi, [rsp + 8]<= /span>
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 xor =C2=A0 =C2=A0 e= ax, eax
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 call<= span style=3D"color:rgb(0,0,0)"> =C2=A0 =C2=A0__isoc99_scanf@PLT
=C2=A0 =C2=A0 =C2=A0 =C2=A0 lea =C2=A0 =C2=A0 rdi= , [rip + .L.str.1]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 xor =C2=A0 =C2=A0 esi, esi
=C2=A0 =C2=A0 =C2=A0 =C2=A0 xor =C2=A0= =C2=A0 eax, eax
=C2=A0 =C2=A0 =C2=A0 =C2=A0= call =C2=A0 =C2=A0print= f@PLT
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 xor =C2=A0 =C2=A0 eax, eax
=C2=A0 =C2=A0 =C2=A0 =C2=A0 add<= /span> =C2=A0 =C2=A0 rsp, 24
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ret

best regards,
Ranier Vilela
--000000000000c0a02b06258ac301--