Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"You shouldn't make my toaster angry." -- Household security explained in "Johnny Quest"


devel / comp.lang.c / Check if all 256 bits are clear or set ?

SubjectAuthor
* Check if all 256 bits are clear or set ?skybuck2000
+* Re: Check if all 256 bits are clear or set ?skybuck2000
|`* Re: Check if all 256 bits are clear or set ?skybuck2000
| `* Re: Check if all 256 bits are clear or set ?skybuck2000
|  `- Re: Check if all 256 bits are clear or set ?skybuck2000
+* Re: Check if all 256 bits are clear or set ?Stefan Ram
|`* Re: Check if all 256 bits are clear or set ?Peter 'Shaggy' Haywood
| `* Re: Check if all 256 bits are clear or set ?Ben Bacarisse
|  +* Re: Check if all 256 bits are clear or set ?Peter 'Shaggy' Haywood
|  |+* Re: Check if all 256 bits are clear or set ?Ben Bacarisse
|  ||+* Re: Check if all 256 bits are clear or set ?Tim Rentsch
|  |||+- Re: Check if all 256 bits are clear or set ?Ben Bacarisse
|  |||`- Re: Check if all 256 bits are clear or set ?Peter 'Shaggy' Haywood
|  ||`* Re: Check if all 256 bits are clear or set ?Tim Rentsch
|  || `* Re: Check if all 256 bits are clear or set ?Ben Bacarisse
|  ||  `- Re: Check if all 256 bits are clear or set ?Tim Rentsch
|  |`- Re: Check if all 256 bits are clear or set ?skybuck2000
|  `- Re: Check if all 256 bits are clear or set ?Bonita Montero
`* Re: Check if all 256 bits are clear or set ?Bonita Montero
 `* Re: Check if all 256 bits are clear or set ?Bonita Montero
  `* Re: Check if all 256 bits are clear or set ?Bonita Montero
   `* Re: Check if all 256 bits are clear or set ?Bonita Montero
    `* Re: Check if all 256 bits are clear or set ?Bonita Montero
     `* Re: Check if all 256 bits are clear or set ?Ben Bacarisse
      +* Re: Check if all 256 bits are clear or set ?Bonita Montero
      |+* Re: Check if all 256 bits are clear or set ?Ben Bacarisse
      ||`* Re: Check if all 256 bits are clear or set ?Bonita Montero
      || +* Re: Check if all 256 bits are clear or set ?Ben Bacarisse
      || |`- Re: Check if all 256 bits are clear or set ?Bonita Montero
      || `* Re: Check if all 256 bits are clear or set ?Scott Lurndal
      ||  `* Re: Check if all 256 bits are clear or set ?Bonita Montero
      ||   `* Re: Check if all 256 bits are clear or set ?Bonita Montero
      ||    `- Re: Check if all 256 bits are clear or set ?Bonita Montero
      |`* Re: Check if all 256 bits are clear or set ?Bonita Montero
      | `* Re: Check if all 256 bits are clear or set ?Manfred
      |  `* Re: Check if all 256 bits are clear or set ?Bonita Montero
      |   `* Re: Check if all 256 bits are clear or set ?Manfred
      |    `- Re: Check if all 256 bits are clear or set ?Bonita Montero
      `- Re: Check if all 256 bits are clear or set ?Bart

Pages:12
Check if all 256 bits are clear or set ?

<e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18781&group=comp.lang.c#18781

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:94:: with SMTP id o20mr8482163qtw.169.1635468475264;
Thu, 28 Oct 2021 17:47:55 -0700 (PDT)
X-Received: by 2002:a05:6214:e6c:: with SMTP id jz12mr7502188qvb.28.1635468475182;
Thu, 28 Oct 2021 17:47:55 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 28 Oct 2021 17:47:54 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=84.25.28.171; posting-account=np6u_wkAAADxbE7UBGUIOm-csir6aX02
NNTP-Posting-Host: 84.25.28.171
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
Subject: Check if all 256 bits are clear or set ?
From: skybuck2...@hotmail.com (skybuck2000)
Injection-Date: Fri, 29 Oct 2021 00:47:55 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 55
 by: skybuck2000 - Fri, 29 Oct 2021 00:47 UTC

I have a data structure in one of my applications which is an array of 32 bytes.

So basically 256 bits.

The code needs to check if all bits are empty or if all bits are full. So basically all clear or all set.

Currently this data structure uses an additional counter of 1 byte to smartly keep track of when a bit is set or cleared individually and it acts upon it's conclusion =D
(secret code =D)

So basically this code uses 1 branch to check the counter, and sometimes it does a little decrementing and sometimes a little incrementing.

So the code itself is pretty efficient.

There is ofcourse a slight little "problem" / "inefficiency" with this technique it consumes 1 additional byte.

So 1 out of 33 bytes. Maybe there are other implicit penalties where this 33 data structure is misaligned because of this 33 byte structure.

So reducing this structure to just 32 bytes could not only safe memory but it might also increase performance a little bit, to maybe a lot ?

So at least memory wise this would give a 3% reduction of memory consumption.

Performance wise I don't know.

Currently checking 256 bits with code with be much worse.

Possibilities:

1. Check each bit individually, this would be pretty stupid and slow.
2. Check each byte, so 32 comparisons, still pretty slow.
3. Check each word, so 16 comparisons, a bit better.
4. Check each longword, so 8 comparisons, a lot better but still slow.
5. Check each quadword, so 4 comparisons, only applicable in 64 bit application.

On some systems 64 bit integer compare can still take two clock cycles.

Is there something better and faster ?

Is there a 128 bit compare in a single clock or two ?

Is there a 256 bit compare in a single clock or two ?

Just wondering ! ;)

Anyway in case such an instruction would exist for example:

if Are256BitsClear( DataStructure )

if Are256BitsSet( DataStructure )

then the counter could most likely/ofcourse be removed =D
and the data structure would be a nice 32 byte fit and a bit more efficient.

Perhaps it might even make sense to use 4 comparisons if the alignment somehow gives it more performance then the overhead of 4 comparisons lol.

Bye,
Skybuck.

Re: Check if all 256 bits are clear or set ?

<173fe931-a575-4665-b676-a12820c32365n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18782&group=comp.lang.c#18782

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:1e95:: with SMTP id c21mr8685221qtm.412.1635469323713;
Thu, 28 Oct 2021 18:02:03 -0700 (PDT)
X-Received: by 2002:a37:802:: with SMTP id 2mr6562232qki.308.1635469323557;
Thu, 28 Oct 2021 18:02:03 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 28 Oct 2021 18:02:03 -0700 (PDT)
In-Reply-To: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.25.28.171; posting-account=np6u_wkAAADxbE7UBGUIOm-csir6aX02
NNTP-Posting-Host: 84.25.28.171
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <173fe931-a575-4665-b676-a12820c32365n@googlegroups.com>
Subject: Re: Check if all 256 bits are clear or set ?
From: skybuck2...@hotmail.com (skybuck2000)
Injection-Date: Fri, 29 Oct 2021 01:02:03 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 62
 by: skybuck2000 - Fri, 29 Oct 2021 01:02 UTC

I was just scanning the internet/googling it and I came across this, which is a pretty neat trick to start with lol:

https://github.com/holiman/uint256/blob/master/uint256.go

Particularly this code, written in go, I have never programmed in go but I can still read/understand some of it:

// Cmp compares z and x and returns:
//
// -1 if z < x
// 0 if z == x
// +1 if z > x
//
func (z *Int) Cmp(x *Int) (r int) {
// z < x <=> z - x < 0 i.e. when subtraction overflows.
d0, carry := bits.Sub64(z[0], x[0], 0)
d1, carry := bits.Sub64(z[1], x[1], carry)
d2, carry := bits.Sub64(z[2], x[2], carry)
d3, carry := bits.Sub64(z[3], x[3], carry)
if carry == 1 {
return -1
}
if d0|d1|d2|d3 == 0 {
return 0
}
return 1
}

The interesting part is in:

if d0|d1|d2|d3 == 0 {

Apperently what this code does is it retrieves 4x64 bit quantities.

OR-s them together.

And then checks the result of that with a single branch.

So at the assembler/instruction/compiler generated code level this would probably result in something like:
mov register1, 64 bits from array[0]
mov register2, 64 bits from array[1]
mov register3, 64 bits from array[2]
mov register4, 64 bits from array[3]
or register1, register2
or register1, register3
or register1, register4
jump if zero/jump if not zero.

Something like that, so this could be pretty efficient ! at least "branch times" are avoided/costly comparisons.
The jump zero flag instruction could also be pretty efficient, even more efficient than a cmp, because the or instruction may already have set it.

The only slight downside to this would be 7 extra instructions taking up some instruction cache.

All in all not to bad for a first google ! ;) This might be quite quick actually ! LOL and might be worth it.

And the nicest part about it is it doesn't require any special assembler or avx or mmx or sse instructions... hmmmm...

(And ofcourse the compiler has to be slightly efficient at generating this exact assembler code, but it can probably do that ! ;) maybe it has even some more tricks
up it's sleeve.)

Can you do better than this though ? Wondering...

Bye,
Skybuck.

Re: Check if all 256 bits are clear or set ?

<79fb12bb-40a7-4336-81fd-97ae4a68b8ccn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18783&group=comp.lang.c#18783

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:393:: with SMTP id j19mr8837762qtx.166.1635469951569;
Thu, 28 Oct 2021 18:12:31 -0700 (PDT)
X-Received: by 2002:ac8:5e53:: with SMTP id i19mr8597180qtx.226.1635469951423;
Thu, 28 Oct 2021 18:12:31 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 28 Oct 2021 18:12:31 -0700 (PDT)
In-Reply-To: <173fe931-a575-4665-b676-a12820c32365n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.25.28.171; posting-account=np6u_wkAAADxbE7UBGUIOm-csir6aX02
NNTP-Posting-Host: 84.25.28.171
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <173fe931-a575-4665-b676-a12820c32365n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <79fb12bb-40a7-4336-81fd-97ae4a68b8ccn@googlegroups.com>
Subject: Re: Check if all 256 bits are clear or set ?
From: skybuck2...@hotmail.com (skybuck2000)
Injection-Date: Fri, 29 Oct 2021 01:12:31 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 31
 by: skybuck2000 - Fri, 29 Oct 2021 01:12 UTC

This would make a fine implementation for Are256BitsClear.

This leaves Are256BitsSet still to be implemented...

Hmm can the same trick be done for AllSet ?

Probably with AND and checking for FF also decimally known as 255 :)

So for Are256BitsSet this code may work:

mov register1, 64 bits from array[0]
mov register2, 64 bits from array[1]
mov register3, 64 bits from array[2]
mov register4, 64 bits from array[3]
and register1, register2
and register1, register3
and register1, register4

jump if zero/jump if not zero.

^ Not sure about the jump if not zero instruction... this would probably be invalid.

It needs to be 255 or nothing at all.

My initially thought was to maybe negate it or something, turn 255 specific to zero somehow...

Maybe subtract 255 from it to see if it's zero... if not then it's not full.

Hmmm ?

Bye for now,
Skybuck.

Re: Check if all 256 bits are clear or set ?

<bits-20211029021300@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18784&group=comp.lang.c#18784

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: 29 Oct 2021 01:19:51 GMT
Organization: Stefan Ram
Lines: 19
Expires: 1 Dec 2021 11:59:58 GMT
Message-ID: <bits-20211029021300@ram.dialup.fu-berlin.de>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de Hm1Z5GSCG0KUBGgsDLsaMQKETTSpePQcjKX31qgnldTzVb
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 29 Oct 2021 01:19 UTC

skybuck2000 <skybuck2000@hotmail.com> writes:
>The code needs to check if all bits are empty or if all bits
>are full. So basically all clear or all set.

I'd set up two constant arrays, kinda like

uint32_t const f = 0xffffffff;
uint32_t const a0[ 8 ]={ 0, 0, 0, 0, 0, 0, 0, 0, };
uint32_t const a1[ 8 ]={ f, f, f, f, f, f, f, f, };

, and then use memcmp with those arrays:

if( !memcmp( ( void * )&x, ( void * )&a0, sizeof( x )))
puts( "x is all zero" );

. And only do more work /if/ I then learned that
this was too slow.

Re: Check if all 256 bits are clear or set ?

<b2e35ae2-3810-48da-91dc-24c4449d8359n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18785&group=comp.lang.c#18785

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4044:: with SMTP id i4mr6813897qko.301.1635473905069;
Thu, 28 Oct 2021 19:18:25 -0700 (PDT)
X-Received: by 2002:a37:a3c6:: with SMTP id m189mr6491394qke.233.1635473904905;
Thu, 28 Oct 2021 19:18:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 28 Oct 2021 19:18:24 -0700 (PDT)
In-Reply-To: <79fb12bb-40a7-4336-81fd-97ae4a68b8ccn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.25.28.171; posting-account=np6u_wkAAADxbE7UBGUIOm-csir6aX02
NNTP-Posting-Host: 84.25.28.171
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<173fe931-a575-4665-b676-a12820c32365n@googlegroups.com> <79fb12bb-40a7-4336-81fd-97ae4a68b8ccn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b2e35ae2-3810-48da-91dc-24c4449d8359n@googlegroups.com>
Subject: Re: Check if all 256 bits are clear or set ?
From: skybuck2...@hotmail.com (skybuck2000)
Injection-Date: Fri, 29 Oct 2021 02:18:25 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 66
 by: skybuck2000 - Fri, 29 Oct 2021 02:18 UTC

I found it fun to scan the internet to see if there is anybody that has/needs these same methods.

I came across unity documentation:

https://docs.unity3d.com/Packages/com.unity.render-pipelines.core@7.2/api/UnityEngine.Rendering.BitArray256.html

Describing

allTrue

True if all bits are 1.
Declaration

public bool allTrue { get; }

and it took a while to find the exact source code but it's here:

https://github.com/needle-mirror/com.unity.render-pipelines.core/blob/master/Runtime/Utilities/BitArray.cs

public bool allTrue => data1 == ulong.MaxValue && data2 == ulong.MaxValue && data3 == ulong.MaxValue && data4 == ulong.MaxValue;

from this class to give it a bit more context:
/// <summary>
/// Bit array of size 256.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray256 : IBitArray
{
[SerializeField]
ulong data1;
[SerializeField]
ulong data2;
[SerializeField]
ulong data3;
[SerializeField]
ulong data4;

/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 256u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data1 == 0uL && data2 == 0uL && data3 == 0uL && data4 == 0uL;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data1 == ulong.MaxValue && data2 == ulong.MaxValue && data3 == ulong.MaxValue && data4 == ulong.MaxValue;
/// <summary>Returns the bit array in a human readable form.</summary>

So let's go back to this code:

public bool allTrue => data1 == ulong.MaxValue && data2 == ulong.MaxValue && data3 == ulong.MaxValue && data4 == ulong.MaxValue;

It's a bit funny to see both solutions for allFalse and AllTrue the naming is nice, in my case you could think of it as AllEmpty and AllFull hint hint about secret code lol.

Anyway

At least the allFalse code could be written a bit more efficient HAHA ! =D
as already explored.

But can the allTrue code be written more efficient as well ? I think so...

At least the and's could be done on the data itself

(data1 && data2 && data3 && data4) == ulong.MaxValue
3's and's
1 comparison to max value.

Bye for now,
Skybuck.

Re: Check if all 256 bits are clear or set ?

<bd9377cd-e10a-4b0f-8caa-2a0a07d0f166n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18804&group=comp.lang.c#18804

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5e0a:: with SMTP id h10mr10951267qtx.195.1635509793076;
Fri, 29 Oct 2021 05:16:33 -0700 (PDT)
X-Received: by 2002:a05:620a:448e:: with SMTP id x14mr8572118qkp.459.1635509792931;
Fri, 29 Oct 2021 05:16:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 29 Oct 2021 05:16:32 -0700 (PDT)
In-Reply-To: <b2e35ae2-3810-48da-91dc-24c4449d8359n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.25.28.171; posting-account=np6u_wkAAADxbE7UBGUIOm-csir6aX02
NNTP-Posting-Host: 84.25.28.171
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<173fe931-a575-4665-b676-a12820c32365n@googlegroups.com> <79fb12bb-40a7-4336-81fd-97ae4a68b8ccn@googlegroups.com>
<b2e35ae2-3810-48da-91dc-24c4449d8359n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bd9377cd-e10a-4b0f-8caa-2a0a07d0f166n@googlegroups.com>
Subject: Re: Check if all 256 bits are clear or set ?
From: skybuck2...@hotmail.com (skybuck2000)
Injection-Date: Fri, 29 Oct 2021 12:16:33 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 748
 by: skybuck2000 - Fri, 29 Oct 2021 12:16 UTC

I have created a "professional" github page, so you guys can download the example source code properly and quickly using GIT and don't have to hassle with newsgroup crappy messaging formats:

TData and TLink optimization project

https://github.com/SkybuckFlying/TDataTLinkOptimizationProject

The aim of the project is to:

Optimize the IsEmpty and IsFull routines for TData and TLink.
Optimize the Empty and Full routines for TData and TLink.
Implement WIN64 platform support by implementing BitSet and BitReset.

YES BABY ! =D It's 2021 ! Time to start sharing code a little bit more professionally and effectively and actually good usuable.

However in case you don't know how to use GIT and still want to have a look at the source code then I will post it here anyway,

we can also discuss further ideas for optimization on newsgroups, I am particularly interested in any high speed assembler assistence for x86 and/or x64 instruction set !

unit UnitTDataTLink;

{

TestProgram/Optimization Project for TData and TLink

version 0.02 created on 29 october 2021 by Skybuck Flying

TData contains an array of 32 bytes, in other words 256 bits.
TLink contains an array of 256 pointers.

The mission of this project is to optimize the IsEmpty and IsFull functions
to run as fast as possible on modern x86/x64 processors.

Bonus would be to optimize Empty and Full routines, however beware
the TLink structure uses special pointer values for empty and full.

The verification routines do not need to be optimized and should not
be optimized and are only there to verify any optimized code.

I will sure this code in github for easy downloading and uploading.

Would be cool to get some help with optimizing this code.

The original code used a mCount : byte; member to optimize the code
however this increases the TData structure to 33 bytes which could
be causing misalignment and consumes 3% more memory.

The TLink structure would also fit better if mCount is illiminated.

Both structures contained a mCount : byte variable and have been removed.

Now the same functionality to check for empty and full should be implemented
with as fast as code as possible without using any extra member fields
for the structures to keep them memory efficient like this.

Perhaps these link functions can be optimized with AVX-512 or so ?

}

{

version 0.03 created on 29 october 2021 by Skybuck Flying

Console program turned into multi-device-application project for
multi-platform support.

Project and Source files seperated into seperate folders to avoid
file clutter.

Only WIN32 platform currently implemented.

Other platforms possible thanks to multi-device-project/applicaation.

For some reason it was not possible to add platforms to a console project.

So I modified code so it can work with a multi-device-application, this
added a lot of platforms.

WIN64 platform support is desired !

The rest would be a bonus !

}

interface

uses
Classes; // for TStrings

type
TData = packed record
mData : packed array[0..31] of byte;

procedure Empty;
procedure Full;

function IsEmpty : boolean;
function IsFull : boolean;

// debug routines
function VerifyIsEmpty : boolean;
function VerifyIsFull : boolean;
function VerifyOne : boolean;
function VerifyRandomButOne : boolean;
procedure VerifyAll;
end;

TLink = packed record
mLink : packed array[0..255] of pointer;

procedure Empty;
procedure Full;

function IsEmpty : boolean;
function IsFull : boolean;

// debug routines
function VerifyIsEmpty : boolean;
function VerifyIsFull : boolean;
function VerifyOne : boolean;
function VerifyRandomButOne : boolean;
procedure VerifyAll;
end;

procedure MainTestProgram( ParaOutput : TStrings );

implementation

uses
SysUtils; // for IntToStr

const
ConstDataTestLoops = 1000000; // one million ! =D
ConstLinkTestLoops = 100000; // one hundred thousand ! =D

var
const_empty : pointer;
const_full : pointer;

var
vOutput : TStrings;

procedure OutputMessage( ParaString : string );
begin
vOutput.Add( ParaString );
end;

// only works on win32 platform, need win64 platform versions !

{$ifdef WIN32}
// unlocked versions (multi-threading-unsafe)
procedure BitReset( ParaMemory : pointer; ParaBitIndex : integer );
asm
btr [eax], edx
end;

procedure BitSet( ParaMemory : pointer; ParaBitIndex : integer );
asm
bts [eax], edx
end;

// locked versions (multi-threading-safe)
procedure LockedBitReset( ParaMemory : pointer; ParaBitIndex : integer );
asm
LOCK btr [eax], edx
end;

procedure LockedBitSet( ParaMemory : pointer; ParaBitIndex : integer );
asm
LOCK bts [eax], edx
end;
{$endif}

{$ifdef WIN64}
// unlocked versions (multi-threading-unsafe)
// optimize me ?
procedure BitReset( ParaMemory : pointer; ParaBitIndex : integer );
begin
// to be implemented
ERROR NOT IMPLEMENTED
end;

procedure BitSet( ParaMemory : pointer; ParaBitIndex : integer );
begin
// to be implemented
ERROR NOT IMPLEMENTED
end;

// not really required for this test program but would be nice to have a 64 bit version of this as well.
procedure LockedBitReset( ParaMemory : pointer; ParaBitIndex : integer );
begin

end;

procedure LockedBitSet( ParaMemory : pointer; ParaBitIndex : integer );
begin

end;
{$endif}

{$ifdef WIN32}
procedure TData.Empty;
type
TUInt32Array = packed array[0..7] of UInt32;
begin
TUInt32Array(mData)[0] := 0;
TUInt32Array(mData)[1] := 0;
TUInt32Array(mData)[2] := 0;
TUInt32Array(mData)[3] := 0;
TUInt32Array(mData)[4] := 0;
TUInt32Array(mData)[5] := 0;
TUInt32Array(mData)[6] := 0;
TUInt32Array(mData)[7] := 0;
end;
{$endif}

{$ifdef WIN64}
procedure TData.Empty;
type
TUInt64Array = packed array[0..3] of UInt64;
begin
TUInt64Array(mData)[0] := 0;
TUInt64Array(mData)[1] := 0;
TUInt64Array(mData)[2] := 0;
TUInt64Array(mData)[3] := 0;
end;
{$endif}

{$ifdef WIN32}
procedure TData.Full;
type
TUInt32Array = packed array[0..7] of longword;
begin
// 11223344
TUInt32Array(mData)[0] := $FFFFFFFF;
TUInt32Array(mData)[1] := $FFFFFFFF;
TUInt32Array(mData)[2] := $FFFFFFFF;
TUInt32Array(mData)[3] := $FFFFFFFF;
TUInt32Array(mData)[4] := $FFFFFFFF;
TUInt32Array(mData)[5] := $FFFFFFFF;
TUInt32Array(mData)[6] := $FFFFFFFF;
TUInt32Array(mData)[7] := $FFFFFFFF;
end;
{$endif}

{$ifdef WIN64}
procedure TData.Full;
type
TUInt64Array = packed array[0..3] of UInt64;
begin
// 1122334455667788
TUInt64Array(mData)[0] := $FFFFFFFFFFFFFFFF;
TUInt64Array(mData)[1] := $FFFFFFFFFFFFFFFF;
TUInt64Array(mData)[2] := $FFFFFFFFFFFFFFFF;
TUInt64Array(mData)[3] := $FFFFFFFFFFFFFFFF;
end;
{$endif}

{$ifdef WIN32}
function TData.IsEmpty : boolean;
type
TUInt32Array = packed array[0..7] of longword;
var
vResult : longword;
begin
result := False;

vResult :=
TUInt32Array(mData)[0] or
TUInt32Array(mData)[1] or
TUInt32Array(mData)[2] or
TUInt32Array(mData)[3] or
TUInt32Array(mData)[4] or
TUInt32Array(mData)[5] or
TUInt32Array(mData)[6] or
TUInt32Array(mData)[7];

if vResult = 0 then
begin
result := True;
end;
end;
{$endif}

{$ifdef WIN64}
function TData.IsEmpty : boolean;
type
TUInt64Array = packed array[0..3] of UInt64;
var
vResult : UInt64;
begin
result := False;

vResult :=
TUInt64Array(mData)[0] or
TUInt64Array(mData)[1] or
TUInt64Array(mData)[2] or
TUInt64Array(mData)[3];

if vResult = 0 then
begin
result := True;
end;
end;
{$endif}

{$ifdef WIN32}
function TData.IsFull : boolean;
type
TUInt32Array = packed array[0..7] of longword;
var
vResult : UInt64;
begin
result := False;

vResult :=
TUInt32Array(mData)[0] and
TUInt32Array(mData)[1] and
TUInt32Array(mData)[2] and
TUInt32Array(mData)[3] and
TUInt32Array(mData)[4] and
TUInt32Array(mData)[5] and
TUInt32Array(mData)[6] and
TUInt32Array(mData)[7];

// 11223344
if vResult = $FFFFFFFF then
begin
result := True;
end;
end;
{$endif}

{$ifdef WIN64}
function TData.IsFull : boolean;
type
TUInt64Array = packed array[0..3] of UInt64;
var
vResult : UInt64;
begin
result := False;

vResult :=
TUInt64Array(mData)[0] and
TUInt64Array(mData)[1] and
TUInt64Array(mData)[2] and
TUInt64Array(mData)[3];

// 1122334455667788
if vResult = $FFFFFFFFFFFFFFFF then
begin
result := True;
end;
end;
{$endif}

// TData verification routines

function TData.VerifyIsEmpty : boolean;
var
vIndex : integer;
begin
result := True;
for vIndex := 0 to 31 do
begin
if mData[vIndex] <> 0 then
begin
result := False;
end;
end;
end;


Click here to read the complete article
Re: Check if all 256 bits are clear or set ?

<sm095i-892.ln1@aretha.foo>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18849&group=comp.lang.c#18849

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 03 Nov 2021 04:08:18 -0500
Message-Id: <sm095i-892.ln1@aretha.foo>
From: phayw...@alphalink.com.au (Peter 'Shaggy' Haywood)
Subject: Re: Check if all 256 bits are clear or set ?
Newsgroups: comp.lang.c
Date: Wed, 03 Nov 2021 11:11:39 +1100
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de>
User-Agent: KNode/0.10.9
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
Lines: 80
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-8g1UJnJuHOeiRg5ERwi5IuKJ08DQs+zuCPQ9m98GLO4KSGaQa7aExd/Qy3zyCr3OdklolbzXlK0Vz4W!FnPrhXnPgJONVHqxKU82PUc/bYWdQx6HgUHxW3GhW8Q9tbF2LWU=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2827
 by: Peter 'Shaggy&# - Wed, 3 Nov 2021 00:11 UTC

Groovy hepcat Stefan Ram was jivin' in comp.lang.c on Fri, 29 Oct 2021
12:19 pm. It's a cool scene! Dig it.

> skybuck2000 <skybuck2000@hotmail.com> writes:
>>The code needs to check if all bits are empty or if all bits
>>are full. So basically all clear or all set.
>
> I'd set up two constant arrays, kinda like
>
> uint32_t const f = 0xffffffff;
> uint32_t const a0[ 8 ]={ 0, 0, 0, 0, 0, 0, 0, 0, };
> uint32_t const a1[ 8 ]={ f, f, f, f, f, f, f, f, };
>
> , and then use memcmp with those arrays:
>
> if( !memcmp( ( void * )&x, ( void * )&a0, sizeof( x )))
> puts( "x is all zero" );
>
> . And only do more work /if/ I then learned that
> this was too slow.

Here's a more flexible (but untested) way:

#define ALL_CLEAR 1
#define ALL_SET 2

unsigned all_bits_clear_or_set(unsigned char *data, size_t n)
{ unsigned clrset = 3;
size_t i;

for(i = 0; i < n; i++)
{
if((unsigned char)~0U == data[i])
{
clrset ^= ALL_SET;
break;
}
}

for(i = 0; i < n; i++)
{
if(0 == data[i])
{
clrset ^= ALL_CLEAR;
break;
}
}

return clrset;
}

sometype calling_function(some paramlist)
{ unsigned clrset;

/* some code */
clrset = all_bits_clear_or_set(somedata, size_of_somedata_in_bytes);
if(clrset & ALL_CLEAR)
{
/* All bits are clear. */
}
else if(clrset & ALL_SET)
{
/* All bits are set. */
}
/* some more code */
}

That's a more general solution, allowing for data of other sizes to be
tested. I like to write flexible, reuseable code. :)

--

----- Dig the NEW and IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
Ain't I'm a dawg!!

Re: Check if all 256 bits are clear or set ?

<87fssdh302.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18854&group=comp.lang.c#18854

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Wed, 03 Nov 2021 11:50:37 +0000
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <87fssdh302.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<bits-20211029021300@ram.dialup.fu-berlin.de>
<sm095i-892.ln1@aretha.foo>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8c020e7f6d7a32a25ad5aa1fd928e51b";
logging-data="11566"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kuKuOYOI7BCMtVUcNwcJFmY/HoQ1HHOo="
Cancel-Lock: sha1:W/XmPuZwOnYI/Jo9GDwoieJLxUw=
sha1:HGHBZczK3917ziBtd6rOoXOu+FM=
X-BSB-Auth: 1.d0e8a925f23f496bccca.20211103115037GMT.87fssdh302.fsf@bsb.me.uk
 by: Ben Bacarisse - Wed, 3 Nov 2021 11:50 UTC

Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> writes:

> Groovy hepcat Stefan Ram was jivin' in comp.lang.c on Fri, 29 Oct 2021
> 12:19 pm. It's a cool scene! Dig it.
>
>> skybuck2000 <skybuck2000@hotmail.com> writes:
>>>The code needs to check if all bits are empty or if all bits
>>>are full. So basically all clear or all set.
>>
>> I'd set up two constant arrays, kinda like
>>
>> uint32_t const f = 0xffffffff;
>> uint32_t const a0[ 8 ]={ 0, 0, 0, 0, 0, 0, 0, 0, };
>> uint32_t const a1[ 8 ]={ f, f, f, f, f, f, f, f, };
>>
>> , and then use memcmp with those arrays:
>>
>> if( !memcmp( ( void * )&x, ( void * )&a0, sizeof( x )))
>> puts( "x is all zero" );
>>
>> . And only do more work /if/ I then learned that
>> this was too slow.
>
> Here's a more flexible (but untested) way:
>
> #define ALL_CLEAR 1
> #define ALL_SET 2
>
> unsigned all_bits_clear_or_set(unsigned char *data, size_t n)
> {
> unsigned clrset = 3;
> size_t i;
>
> for(i = 0; i < n; i++)
> {
> if((unsigned char)~0U == data[i])
> {
> clrset ^= ALL_SET;
> break;
> }
> }

Logically, this is a while loop. I see of a lot of for loops being used
this way. Is there an active anti-while campaign I've missed?

> for(i = 0; i < n; i++)
> {
> if(0 == data[i])
> {
> clrset ^= ALL_CLEAR;
> break;
> }
> }

Ditto.

size_t i = 0;
while (i < n && data[1] != 0) i++;
clrset ^= (i < n ? ALL_CLEAR : 0);

> return clrset;
> }

But this does not solve the problem. The OP wanted a test for all bits
in all bytes, not all bits in at least one byte.

--
Ben.

Re: Check if all 256 bits are clear or set ?

<sluq0i$fm1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18866&group=comp.lang.c#18866

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Wed, 3 Nov 2021 21:04:34 +0100
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <sluq0i$fm1$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Nov 2021 20:04:34 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6870e797e3a162fded2458f84caf0ec0";
logging-data="16065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RicMFVSnnv8+Ka1vFREoi24eTBvAspxY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.1
Cancel-Lock: sha1:MiunoHPsLETmdwwjQGP96Riy90A=
In-Reply-To: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Wed, 3 Nov 2021 20:04 UTC

What about this:

template<bool Set>
bool test256( char *p )
{ uint64_t *p64 = (uint64_t *)p;
if constexpr( Set )
return p64[0] == -1 && !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
else
return p64[0] == 0 && !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
}

Re: Check if all 256 bits are clear or set ?

<sluq9s$fm1$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18867&group=comp.lang.c#18867

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Wed, 3 Nov 2021 21:09:32 +0100
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <sluq9s$fm1$2@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Nov 2021 20:09:32 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6870e797e3a162fded2458f84caf0ec0";
logging-data="16065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18j/a3NALltkztSDZKT6FPMpOyoiuJAK8o="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.1
Cancel-Lock: sha1:GTjSua7kw3iwnR0Ex51gYgpeofU=
In-Reply-To: <sluq0i$fm1$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 3 Nov 2021 20:09 UTC

Or if the testing-parameter is variable:

bool test256( char *p, bool set )
{ uint64_t *p64 = (uint64_t *)p;
return !(p64[0] + set) && !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
}

Re: Check if all 256 bits are clear or set ?

<sluqck$fm1$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18868&group=comp.lang.c#18868

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Wed, 3 Nov 2021 21:11:00 +0100
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <sluqck$fm1$3@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 3 Nov 2021 20:11:00 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6870e797e3a162fded2458f84caf0ec0";
logging-data="16065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fk6JOoaZIk3tqkbVV80V3u5qVArjqkLg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.1
Cancel-Lock: sha1:6dTfoGx/yMLL8ZTEG1iD56PtylE=
In-Reply-To: <sluq9s$fm1$2@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 3 Nov 2021 20:11 UTC

Am 03.11.2021 um 21:09 schrieb Bonita Montero:
> Or if the testing-parameter is variable:
>
> bool test256( char *p, bool set )
> {
>     uint64_t *p64 = (uint64_t *)p;
>     return !(p64[0] + set) && !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
Or more efficient:
return !(p64[0] + set) & !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
> }

Re: Check if all 256 bits are clear or set ?

<slur71$oau$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18869&group=comp.lang.c#18869

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Wed, 3 Nov 2021 21:25:05 +0100
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <slur71$oau$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Nov 2021 20:25:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6870e797e3a162fded2458f84caf0ec0";
logging-data="24926"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+NOb5snq1A+jz6GCiFBldicnC/mijRFo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:h6+afmKBqa/NUfyzC6/L9vA4B+g=
In-Reply-To: <sluqck$fm1$3@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 3 Nov 2021 20:25 UTC

In assembly:

movzx rax, dl
add rax, [rcx]
setz al
mov rdx, [rcx]
xor rdx, [rcx + 8]
xor rdx, [rcx + 16]
xor rdx, [rcx + 24]
setz dl
and al, dl
ret

rcx is the pointer to the memory block with 256 bits and dl
if either 0 or 1, depending if the bits should be all 0 or 1.

Re: Check if all 256 bits are clear or set ?

<slvt7q$r5h$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18870&group=comp.lang.c#18870

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 07:05:47 +0100
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <slvt7q$r5h$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 4 Nov 2021 06:05:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="89d8cd09ae7523d114c325c220f56114";
logging-data="27825"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fC2lqpbapVQTucD/uVp6eEoEY4OrGNmc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:iVdkExkK99IhyeibtARDFHD//wU=
In-Reply-To: <slur71$oau$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 4 Nov 2021 06:05 UTC

Am 03.11.2021 um 21:25 schrieb Bonita Montero:
> In assembly:
>
>     movzx    rax, dl
>     add      rax, [rcx]
>     setz     al
>     mov      rdx, [rcx]
>     xor      rdx, [rcx + 8]
>     xor      rdx, [rcx + 16]
>     xor      rdx, [rcx + 24]
>     setz     dl
>     and      al, dl
>     ret
>
> rcx is the pointer to the memory block with 256 bits and dl
> if either 0 or 1, depending if the bits should be all 0 or 1.

Can anyone beat this ?
I tested the above code on my Linux-machine:

#include <iostream>
#include <chrono>

using namespace std;
using namespace chrono;

extern "C"
bool test256( char const *p, bool f );

int main()
{ char mem[256 / 8] = { 0 };
auto start = high_resolution_clock::now();
for( size_t i = 100'000'000; i; --i )
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false ),
test256( mem, false );
double ns = (int64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count() / 1.0e9;
cout << ns << endl;
}

And each iteration is about 1.1ns, i.e 4.4 clock cycles.

Re: Check if all 256 bits are clear or set ?

<87zgqkdudb.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18871&group=comp.lang.c#18871

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 04 Nov 2021 11:37:36 +0000
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <87zgqkdudb.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="8061128906792ee25c1ae701cf55af45";
logging-data="23442"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VkVKlO9dO28JWCQrGzW0RMktClzkRODg="
Cancel-Lock: sha1:Lh8ADyFXvoVUwSuL51VdcDY2vdQ=
sha1:9TCiMPR+8JZjPkjYatv0BZ71bt4=
X-BSB-Auth: 1.b3ef050579003880ce70.20211104113736GMT.87zgqkdudb.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 4 Nov 2021 11:37 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:

> Am 03.11.2021 um 21:25 schrieb Bonita Montero:
>> In assembly:
>>     movzx    rax, dl
>>     add      rax, [rcx]
>>     setz     al
>>     mov      rdx, [rcx]
>>     xor      rdx, [rcx + 8]
>>     xor      rdx, [rcx + 16]
>>     xor      rdx, [rcx + 24]
>>     setz     dl
>>     and      al, dl
>>     ret
>> rcx is the pointer to the memory block with 256 bits and dl
>> if either 0 or 1, depending if the bits should be all 0 or 1.
>
> Can anyone beat this ?

But I don't think the code is correct:

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

bool test256(char *p, bool set)
{
uint64_t *p64 = (uint64_t *)p;
return !(p64[0] + set) & !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
}

int main(void)
{
char b[32] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
printf("%d\n", test256(b, true));
}

prints 1.

In addition, there is undefined behaviour if the pointer p is not
correctly aligned for uint64_t.

--
Ben.

Re: Check if all 256 bits are clear or set ?

<sm13fm$1fc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18872&group=comp.lang.c#18872

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 17:58:25 +0100
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <sm13fm$1fc$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 4 Nov 2021 16:58:30 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="89d8cd09ae7523d114c325c220f56114";
logging-data="1516"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FfOgOhQweNtdBZvmBVpeJWnSnF7iG24E="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:eFyL+QLRQOt/5DgdYMVq2ZEIxGc=
In-Reply-To: <87zgqkdudb.fsf@bsb.me.uk>
Content-Language: de-DE
 by: Bonita Montero - Thu, 4 Nov 2021 16:58 UTC

You're right, but this works:

bool test256( char *p, bool set )
{ uint64_t *p64 = (uint64_t *)p;
return !(p64[0] + set) & !(p64[1] + set) & !(p64[2] + set) & !(p64[3] +
set);
}

Re: Check if all 256 bits are clear or set ?

<874k8reted.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18873&group=comp.lang.c#18873

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 04 Nov 2021 17:13:14 +0000
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <874k8reted.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8061128906792ee25c1ae701cf55af45";
logging-data="4042"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Jd2nV2De9eFlBLtuzYD99Dp7ydxPMa8U="
Cancel-Lock: sha1:1nVg8BZBYsREj91KlmhlqQ5Ce+Y=
sha1:5/OwoC266aReHteskmCoupf0Mg0=
X-BSB-Auth: 1.302e9cd4f47ea11f4738.20211104171314GMT.874k8reted.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 4 Nov 2021 17:13 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:

> You're right, but this works:
>
> bool test256( char *p, bool set )
> {
> uint64_t *p64 = (uint64_t *)p;

This is undefined id p is not properly aligned for uint64_t.

> return !(p64[0] + set) & !(p64[1] + set) & !(p64[2] + set) & !(p64[3] + set);
> }
>

--
Ben.

Re: Check if all 256 bits are clear or set ?

<sm14cm$87v$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18874&group=comp.lang.c#18874

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 18:13:58 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <sm14cm$87v$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 4 Nov 2021 17:13:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="89d8cd09ae7523d114c325c220f56114";
logging-data="8447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4w+yRBx5Zk8a1EoBO/y5eef3n28Az1rk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:p2NRfk9HLQ8NcszrZIaKy9ap3+g=
In-Reply-To: <sm13fm$1fc$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 4 Nov 2021 17:13 UTC

Am 04.11.2021 um 17:58 schrieb Bonita Montero:
> You're right, but this works:
>
> bool test256( char *p, bool set )
> {
>     uint64_t *p64 = (uint64_t *)p;
>     return !(p64[0] + set) & !(p64[1] + set) & !(p64[2] + set) &
> !(p64[3] + set);
> }

This is the assembly-variant (MSVC):

PUBLIC ?test256@@YA_NPEBD_N@Z

_TEXT SEGMENT
?test256@@YA_NPEBD_N@Z PROC
movzx rdx, dl
mov r8, [rcx]
add r8, rdx
setz al
mov r8, [rcx + 8]
add r8, rdx
setz ah
and al, ah
mov r8, [rcx + 16]
add r8, rdx
setz ah
and al, ah
mov r8, [rcx + 24]
add r8, rdx
setz ah
and al, ah
ret
?test256@@YA_NPEBD_N@Z ENDP
_TEXT ENDS
END

rcx is p, dl is set, r8 is volatile.

Re: Check if all 256 bits are clear or set ?

<sm14f6$87v$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18875&group=comp.lang.c#18875

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 18:15:18 +0100
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <sm14f6$87v$2@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me> <874k8reted.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 4 Nov 2021 17:15:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="89d8cd09ae7523d114c325c220f56114";
logging-data="8447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19NVTLeBFinMJrQdyudTjUAFsZ6JdbdltI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:yY1aESS844xWRn4DnWnvb6GIZtk=
In-Reply-To: <874k8reted.fsf@bsb.me.uk>
Content-Language: de-DE
 by: Bonita Montero - Thu, 4 Nov 2021 17:15 UTC

Am 04.11.2021 um 18:13 schrieb Ben Bacarisse:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
>
>> You're right, but this works:
>>
>> bool test256( char *p, bool set )
>> {
>> uint64_t *p64 = (uint64_t *)p;
>
> This is undefined id p is not properly aligned for uint64_t.

Doesn't matter on x86-computers, they're even optimized in a way that
unaligned accesses that don't cross page-bounaries have no performance
-penalty.

Re: Check if all 256 bits are clear or set ?

<87pmrfde5s.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18876&group=comp.lang.c#18876

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 04 Nov 2021 17:27:43 +0000
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <87pmrfde5s.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me> <874k8reted.fsf@bsb.me.uk>
<sm14f6$87v$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8061128906792ee25c1ae701cf55af45";
logging-data="4042"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ToStXfbKR/tEhZTjvnhRIg8FT5rjAqgs="
Cancel-Lock: sha1:JbBzaCz5dmCJmOL6olwv8Prz+o0=
sha1:/xzE6KdJ3dkXH5FVTNQzOWrUMzQ=
X-BSB-Auth: 1.e9fe86da3383709d823a.20211104172743GMT.87pmrfde5s.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 4 Nov 2021 17:27 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:

> Am 04.11.2021 um 18:13 schrieb Ben Bacarisse:
>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>
>>> You're right, but this works:
>>>
>>> bool test256( char *p, bool set )
>>> {
>>> uint64_t *p64 = (uint64_t *)p;
>>
>> This is undefined [if] p is not properly aligned for uint64_t.
>
> Doesn't matter on x86-computers, they're even optimized in a way that
> unaligned accesses that don't cross page-bounaries have no performance
> -penalty.

I think it helps to point out when code is not portable.

--
Ben.

Re: Check if all 256 bits are clear or set ?

<BKUgJ.17157$QB1.4271@fx42.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18877&group=comp.lang.c#18877

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!peer02.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Check if all 256 bits are clear or set ?
Newsgroups: comp.lang.c
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me> <sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me> <slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk> <sm13fm$1fc$1@dont-email.me> <874k8reted.fsf@bsb.me.uk> <sm14f6$87v$2@dont-email.me>
Lines: 20
Message-ID: <BKUgJ.17157$QB1.4271@fx42.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 04 Nov 2021 17:28:01 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 04 Nov 2021 17:28:01 GMT
X-Received-Bytes: 1606
 by: Scott Lurndal - Thu, 4 Nov 2021 17:28 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 04.11.2021 um 18:13 schrieb Ben Bacarisse:
>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>
>>> You're right, but this works:
>>>
>>> bool test256( char *p, bool set )
>>> {
>>> uint64_t *p64 = (uint64_t *)p;
>>
>> This is undefined id p is not properly aligned for uint64_t.
>
>Doesn't matter on x86-computers, they're even optimized in a way that
>unaligned accesses that don't cross page-bounaries have no performance
>-penalty.
>

It is, of course, possible to implement the entire test256
function with three instructions on x86. Exercise left
to the student, but hint: AVX.

Re: Check if all 256 bits are clear or set ?

<sm159p$f67$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18878&group=comp.lang.c#18878

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 18:29:29 +0100
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <sm159p$f67$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me> <874k8reted.fsf@bsb.me.uk>
<sm14f6$87v$2@dont-email.me> <87pmrfde5s.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 4 Nov 2021 17:29:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="89d8cd09ae7523d114c325c220f56114";
logging-data="15559"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1oVOKHvsFocq7HbSTG7KR7QBrQBesvjQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:WBkJygDWdSlTnQC+9qNocp9z7aM=
In-Reply-To: <87pmrfde5s.fsf@bsb.me.uk>
Content-Language: de-DE
 by: Bonita Montero - Thu, 4 Nov 2021 17:29 UTC

Am 04.11.2021 um 18:27 schrieb Ben Bacarisse:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
>
>> Am 04.11.2021 um 18:13 schrieb Ben Bacarisse:
>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>
>>>> You're right, but this works:
>>>>
>>>> bool test256( char *p, bool set )
>>>> {
>>>> uint64_t *p64 = (uint64_t *)p;
>>>
>>> This is undefined [if] p is not properly aligned for uint64_t.
>>
>> Doesn't matter on x86-computers, they're even optimized in a way that
>> unaligned accesses that don't cross page-bounaries have no performance
>> -penalty.
>
> I think it helps to point out when code is not portable.

Doesn't matter.

Re: Check if all 256 bits are clear or set ?

<sm175a$tem$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18879&group=comp.lang.c#18879

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 18:01:15 +0000
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <sm175a$tem$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 4 Nov 2021 18:01:14 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="39be6205940952daf08618826eeb17a8";
logging-data="30166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7SFbRorx96bppar/qF+zac+bAQTzkScQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.1
Cancel-Lock: sha1:JaQFXuz7ABkCvA5VU++89p5wicw=
In-Reply-To: <87zgqkdudb.fsf@bsb.me.uk>
 by: Bart - Thu, 4 Nov 2021 18:01 UTC

On 04/11/2021 11:37, Ben Bacarisse wrote:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
>
>> Am 03.11.2021 um 21:25 schrieb Bonita Montero:
>>> In assembly:
>>>     movzx    rax, dl
>>>     add      rax, [rcx]
>>>     setz     al
>>>     mov      rdx, [rcx]
>>>     xor      rdx, [rcx + 8]
>>>     xor      rdx, [rcx + 16]
>>>     xor      rdx, [rcx + 24]
>>>     setz     dl
>>>     and      al, dl
>>>     ret
>>> rcx is the pointer to the memory block with 256 bits and dl
>>> if either 0 or 1, depending if the bits should be all 0 or 1.
>>
>> Can anyone beat this ?
>
> But I don't think the code is correct:
>
> #include <stdio.h>
> #include <stdbool.h>
> #include <stdint.h>
>
> bool test256(char *p, bool set)
> {
> uint64_t *p64 = (uint64_t *)p;
> return !(p64[0] + set) & !(p64[0] ^ p64[1] ^ p64[2] ^ p64[3]);
> }
>
> int main(void)
> {
> char b[32] = {
> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> };
> printf("%d\n", test256(b, true));
> }
>
> prints 1.
>
> In addition, there is undefined behaviour if the pointer p is not
> correctly aligned for uint64_t.

I had a similar solution (not posted*) based on operations between
64-bit elements.

It came with a note that it required the 32-byte array to be
64-bit-aligned. And where performance is important, as appeared to be
the case in the OP, then that should be the case; the caller should
ensure it.

(* Lost now but something like:

if ((p[0] | p[1] | p[2] | p[3]) == 0) return 1;
if ((p[0] & p[1] & p[2] & p[3]) == -1) return 1;
return 0;
)

Re: Check if all 256 bits are clear or set ?

<sm18od$1qj3$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18880&group=comp.lang.c#18880

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 19:28:28 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sm18od$1qj3$1@gioia.aioe.org>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me> <sm14cm$87v$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="60003"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Thu, 4 Nov 2021 18:28 UTC

On 11/4/2021 6:13 PM, Bonita Montero wrote:
> Am 04.11.2021 um 17:58 schrieb Bonita Montero:
>> You're right, but this works:
>>
>> bool test256( char *p, bool set )
>> {
>>      uint64_t *p64 = (uint64_t *)p;
>>      return !(p64[0] + set) & !(p64[1] + set) & !(p64[2] + set) &
>> !(p64[3] + set);
>> }
>
> This is the assembly-variant (MSVC):
>
> PUBLIC ?test256@@YA_NPEBD_N@Z
>
> _TEXT SEGMENT
> ?test256@@YA_NPEBD_N@Z PROC
>     movzx    rdx, dl
>     mov      r8, [rcx]
>     add      r8, rdx
>     setz     al
>     mov      r8, [rcx + 8]
>     add      r8, rdx
>     setz     ah
>     and      al, ah
>     mov      r8, [rcx + 16]
>     add      r8, rdx
>     setz     ah
>     and      al, ah
>     mov      r8, [rcx + 24]
>     add      r8, rdx
>     setz     ah
>     and      al, ah
>     ret
> ?test256@@YA_NPEBD_N@Z ENDP
> _TEXT ENDS
> END
>
> rcx is p, dl is set, r8 is volatile.

This may leave ah nonzero on ret

Re: Check if all 256 bits are clear or set ?

<sm1cb3$33a$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18881&group=comp.lang.c#18881

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Thu, 4 Nov 2021 20:29:38 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <sm1cb3$33a$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me> <sm14cm$87v$1@dont-email.me>
<sm18od$1qj3$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 4 Nov 2021 19:29:39 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="89d8cd09ae7523d114c325c220f56114";
logging-data="3178"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OcUm+HN72QAA+7JDYAn5Lw21lCFGJpbk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:A5G4zoOnp7h+hJzxdDltajKxehE=
In-Reply-To: <sm18od$1qj3$1@gioia.aioe.org>
Content-Language: de-DE
 by: Bonita Montero - Thu, 4 Nov 2021 19:29 UTC

Am 04.11.2021 um 19:28 schrieb Manfred:
> On 11/4/2021 6:13 PM, Bonita Montero wrote:
>> Am 04.11.2021 um 17:58 schrieb Bonita Montero:
>>> You're right, but this works:
>>>
>>> bool test256( char *p, bool set )
>>> {
>>>      uint64_t *p64 = (uint64_t *)p;
>>>      return !(p64[0] + set) & !(p64[1] + set) & !(p64[2] + set) &
>>> !(p64[3] + set);
>>> }
>>
>> This is the assembly-variant (MSVC):
>>
>> PUBLIC ?test256@@YA_NPEBD_N@Z
>>
>> _TEXT SEGMENT
>> ?test256@@YA_NPEBD_N@Z PROC
>>      movzx    rdx, dl
>>      mov      r8, [rcx]
>>      add      r8, rdx
>>      setz     al
>>      mov      r8, [rcx + 8]
>>      add      r8, rdx
>>      setz     ah
>>      and      al, ah
>>      mov      r8, [rcx + 16]
>>      add      r8, rdx
>>      setz     ah
>>      and      al, ah
>>      mov      r8, [rcx + 24]
>>      add      r8, rdx
>>      setz     ah
>>      and      al, ah
>>      ret
>> ?test256@@YA_NPEBD_N@Z ENDP
>> _TEXT ENDS
>> END
>>
>> rcx is p, dl is set, r8 is volatile.
>
> This may leave ah nonzero on ret

A bool-return is a single byte returned in al.

Re: Check if all 256 bits are clear or set ?

<sm202k$vu4$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18882&group=comp.lang.c#18882

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Fri, 5 Nov 2021 02:06:26 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sm202k$vu4$1@gioia.aioe.org>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<sluq0i$fm1$1@dont-email.me> <sluq9s$fm1$2@dont-email.me>
<sluqck$fm1$3@dont-email.me> <slur71$oau$1@dont-email.me>
<slvt7q$r5h$1@dont-email.me> <87zgqkdudb.fsf@bsb.me.uk>
<sm13fm$1fc$1@dont-email.me> <sm14cm$87v$1@dont-email.me>
<sm18od$1qj3$1@gioia.aioe.org> <sm1cb3$33a$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="32708"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Fri, 5 Nov 2021 01:06 UTC

On 11/4/2021 8:29 PM, Bonita Montero wrote:
> Am 04.11.2021 um 19:28 schrieb Manfred:
>> On 11/4/2021 6:13 PM, Bonita Montero wrote:
>>> Am 04.11.2021 um 17:58 schrieb Bonita Montero:
>>>> You're right, but this works:
>>>>
>>>> bool test256( char *p, bool set )
>>>> {
>>>>      uint64_t *p64 = (uint64_t *)p;
>>>>      return !(p64[0] + set) & !(p64[1] + set) & !(p64[2] + set) &
>>>> !(p64[3] + set);
>>>> }
>>>
>>> This is the assembly-variant (MSVC):
>>>
>>> PUBLIC ?test256@@YA_NPEBD_N@Z
>>>
>>> _TEXT SEGMENT
>>> ?test256@@YA_NPEBD_N@Z PROC
>>>      movzx    rdx, dl
>>>      mov      r8, [rcx]
>>>      add      r8, rdx
>>>      setz     al
>>>      mov      r8, [rcx + 8]
>>>      add      r8, rdx
>>>      setz     ah
>>>      and      al, ah
>>>      mov      r8, [rcx + 16]
>>>      add      r8, rdx
>>>      setz     ah
>>>      and      al, ah
>>>      mov      r8, [rcx + 24]
>>>      add      r8, rdx
>>>      setz     ah
>>>      and      al, ah
>>>      ret
>>> ?test256@@YA_NPEBD_N@Z ENDP
>>> _TEXT ENDS
>>> END
>>>
>>> rcx is p, dl is set, r8 is volatile.
>>
>> This may leave ah nonzero on ret
>
> A bool-return is a single byte returned in al.
>
>

But the assembler doesn't know that

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor