Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

New crypt. See /usr/news/crypt.


devel / comp.lang.c / Re: 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
Re: Check if all 256 bits are clear or set ?

<sm2o4n$st5$1@dont-email.me>

  copy mid

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

  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: Fri, 5 Nov 2021 08:57:13 +0100
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <sm2o4n$st5$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> <sm1cb3$33a$1@dont-email.me>
<sm202k$vu4$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 5 Nov 2021 07:57:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ac472d7cfef525848e1b4017c4e56bbf";
logging-data="29605"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Wcg5EB7cIK3RFhSRVzdlnrftran3de0A="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:okxdZKqnhAbDAgfQs/d/wrX0lPs=
In-Reply-To: <sm202k$vu4$1@gioia.aioe.org>
Content-Language: de-DE
 by: Bonita Montero - Fri, 5 Nov 2021 07:57 UTC

Am 05.11.2021 um 02:06 schrieb Manfred:
> 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

The code returns the correct value in al where it is
expected by the caller.

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

<sm32l5$4q5$1@dont-email.me>

  copy mid

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

  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: Fri, 5 Nov 2021 11:56:37 +0100
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <sm32l5$4q5$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> <BKUgJ.17157$QB1.4271@fx42.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 5 Nov 2021 10:56:37 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ac472d7cfef525848e1b4017c4e56bbf";
logging-data="4933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WLxvwu15iK/K2vBZx4WsYvqf6GZ9Lyjo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:e9gypsELuK2wDHrTAtBv4UsnSdg=
In-Reply-To: <BKUgJ.17157$QB1.4271@fx42.iad>
Content-Language: de-DE
 by: Bonita Montero - Fri, 5 Nov 2021 10:56 UTC

Am 04.11.2021 um 18:28 schrieb Scott Lurndal:
> 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.

IF testing for ones or zeroes is variable the code is not
three instructions:

bool test256Avx( char *p, bool set )
{ __m256i xor_ = _mm256_xor_si256( *(__m256i *)p, _mm256_set1_epi8(
-(char)set ) );
return _mm256_testz_si256( xor_, xor_ );
}

.... which results in the following code ...

negl %esi
vmovd %esi, %xmm0
vpbroadcastb %xmm0, %ymm0
vpxor (%rdi), %ymm0, %ymm0
vptest %ymm0, %ymm0
sete %al
vzeroupper
ret

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

<sm3p4n$i2m$1@dont-email.me>

  copy mid

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

  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: Fri, 5 Nov 2021 18:20:25 +0100
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <sm3p4n$i2m$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> <BKUgJ.17157$QB1.4271@fx42.iad>
<sm32l5$4q5$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 5 Nov 2021 17:20:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ac472d7cfef525848e1b4017c4e56bbf";
logging-data="18518"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7KeczBJLcKV3A1ll1u85bl2Dh54T3Hhc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:bJfwVK0fSYklRWpQ0iyHpCiThJI=
In-Reply-To: <sm32l5$4q5$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Fri, 5 Nov 2021 17:20 UTC

Am 05.11.2021 um 11:56 schrieb Bonita Montero:
> Am 04.11.2021 um 18:28 schrieb Scott Lurndal:
>> 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.
>
> IF testing for ones or zeroes is variable the code is not
> three instructions:
>
> bool test256Avx( char *p, bool set )
> {
>     __m256i xor_ = _mm256_xor_si256( *(__m256i *)p, _mm256_set1_epi8(
> -(char)set ) );
>     return _mm256_testz_si256( xor_, xor_ );
> }
>
> ... which results in the following code ...
>
>     negl    %esi
>     vmovd    %esi, %xmm0
>     vpbroadcastb    %xmm0, %ymm0
>     vpxor    (%rdi), %ymm0, %ymm0
>     vptest    %ymm0, %ymm0
>     sete    %al
>     vzeroupper
>     ret

The above code results from the expansion of _mm256_set1_epi8
through gcc. MSVC is not that clever so that you would need to
encode the instructions manually through individual intrinsics.

bool test256Avx( char *p, bool set )
{ __m256i xor_ = _mm256_xor_si256( *(__m256i *)p, _mm256_broadcastb_epi8(
_mm_cvtsi32_si128( -(char)set ) ) );
return _mm256_testz_si256( xor_, xor_ );
}

On my Linux-computer (Ryzen 7 1800X) this AVX-code is
30,5% slower than the 64 bit non-AVX integer-code !

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

<sm3ps1$ms2$1@dont-email.me>

  copy mid

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

  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: Fri, 5 Nov 2021 18:32:51 +0100
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <sm3ps1$ms2$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> <BKUgJ.17157$QB1.4271@fx42.iad>
<sm32l5$4q5$1@dont-email.me> <sm3p4n$i2m$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 5 Nov 2021 17:32:49 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ac472d7cfef525848e1b4017c4e56bbf";
logging-data="23426"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6VlXtB5969jN43tBB1Xy2MtvtlphVHL4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:pmxmBDSmz3T5S1HecTKBnM4l2SE=
In-Reply-To: <sm3p4n$i2m$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Fri, 5 Nov 2021 17:32 UTC

Am 05.11.2021 um 18:20 schrieb Bonita Montero:
> Am 05.11.2021 um 11:56 schrieb Bonita Montero:
>> Am 04.11.2021 um 18:28 schrieb Scott Lurndal:
>>> 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.
>>
>> IF testing for ones or zeroes is variable the code is not
>> three instructions:
>>
>> bool test256Avx( char *p, bool set )
>> {
>>      __m256i xor_ = _mm256_xor_si256( *(__m256i *)p, _mm256_set1_epi8(
>> -(char)set ) );
>>      return _mm256_testz_si256( xor_, xor_ );
>> }
>>
>> ... which results in the following code ...
>>
>>      negl    %esi
>>      vmovd    %esi, %xmm0
>>      vpbroadcastb    %xmm0, %ymm0
>>      vpxor    (%rdi), %ymm0, %ymm0
>>      vptest    %ymm0, %ymm0
>>      sete    %al
>>      vzeroupper
>>      ret
>
> The above code results from the expansion of _mm256_set1_epi8
> through gcc. MSVC is not that clever so that you would need to
> encode the instructions manually through individual intrinsics.
>
> bool test256Avx( char *p, bool set )
> {
>     __m256i xor_ = _mm256_xor_si256( *(__m256i *)p,
> _mm256_broadcastb_epi8( _mm_cvtsi32_si128( -(char)set ) ) );
>     return _mm256_testz_si256( xor_, xor_ );
> }
>
> On my Linux-computer (Ryzen 7 1800X) this AVX-code is
> 30,5% slower than the 64 bit non-AVX integer-code !

These are the timings:

std: 1.44802ns
asm: 1.84683ns
avx: 2.66244ns

std is my C++-function &-ing four bools which are p[n] + set.
asm is the straightforward asm-variant of that.
avx is the above avx-code.
So my straight idea written in C++ is the fastest.
I used __attribute((noinline) / __declspec(noinline) and disabled
whole program optimization to prevent the compiler from optimizing
away my code.

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

<37fh5i-t92.ln1@aretha.foo>

  copy mid

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

  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: Sat, 06 Nov 2021 04:08:07 -0500
Message-Id: <37fh5i-t92.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: Sat, 06 Nov 2021 16:08:19 +1100
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk>
User-Agent: KNode/0.10.9
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
Lines: 99
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-yeXmeyC2oKtisihSLkMpqFv1RPD15emlmOFReZIiS+6gP1ttt2sXNL0xoKvkPznVhq1IMck1Gbw48IS!g7Bn/CUB4xDKLHC0bspG9yeo4/pYWvYsuYM+O4O+HbvBrq2UEYQ=
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: 4409
 by: Peter 'Shaggy&# - Sat, 6 Nov 2021 05:08 UTC

Groovy hepcat Ben Bacarisse was jivin' in comp.lang.c on Wed, 3 Nov 2021
10:50 pm. It's a cool scene! Dig it.

> 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?

I don't know why you say that. It contains initialisation, condition
and update clauses - a for loop I'd say.
But if you want to use a while instead, and put in separate init and
update statements, you're perfectly free to do so.

>> 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.

Perhaps you missed the intended logic (which, I admit, I
could've/should've commented better... or at all) that the returned
variable, clrset, is initialised to 3 (ALL_CLEAR | ALL_SET, which I now
wish I had used in the first place, for clarity) to assume that all
bits are set and all bits are clear. (Yeah, I know they can't be both;
but it's just a starting point.) The algorithm then checks the array
twice, once looking for bytes that are not all bits clear (and stopping
at the first instance, since that's all that's required to determine
that the array is not all clear), and once looking for bytes that are
not all bits set (again stopping at the first instance), and updating
clrset accordingly. By the end, clrset will contain either the value 0
indicating that the array is neither all bits clear nor all bits set, 1
(ALL_CLEAR) indicating that the array has all bits clear, or 2
(ALL_SET) indicating that the array has all bits set.
But, of course, as I said, it's untested, so I might have muffed it.

--

----- 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 ?

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

  copy mid

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

  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: Sat, 06 Nov 2021 11:02:44 +0000
Organization: A noiseless patient Spider
Lines: 127
Message-ID: <87ilx5a6nf.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<bits-20211029021300@ram.dialup.fu-berlin.de>
<sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk>
<37fh5i-t92.ln1@aretha.foo>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="05b9fa80338c97d653a7fb8fdb205590";
logging-data="3606"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6QFB3d5Z602IBJjEWfuCHWqwfs8nkt4s="
Cancel-Lock: sha1:PxVMH5V4LMpP3FmETcpCIfuDvpw=
sha1:5p3G+Z1HsVVG0GtdTH3dCK+70b0=
X-BSB-Auth: 1.47aeafa5946b097235c9.20211106110244GMT.87ilx5a6nf.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 6 Nov 2021 11:02 UTC

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

> Groovy hepcat Ben Bacarisse was jivin' in comp.lang.c on Wed, 3 Nov 2021
> 10:50 pm. It's a cool scene! Dig it.
>
>> 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?
>
> I don't know why you say that. It contains initialisation, condition
> and update clauses - a for loop I'd say.
> But if you want to use a while instead, and put in separate init and
> update statements, you're perfectly free to do so.

It's the break that makes me think that. I think it's more natural to
have the condition up front. I was taught to think about pre- and
post-conditions.

>> 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.
>
> Perhaps you missed the intended logic (which, I admit, I
> could've/should've commented better... or at all) that the returned
> variable, clrset, is initialised to 3 (ALL_CLEAR | ALL_SET, which I now
> wish I had used in the first place, for clarity) to assume that all
> bits are set and all bits are clear. (Yeah, I know they can't be both;
> but it's just a starting point.) The algorithm then checks the array
> twice, once looking for bytes that are not all bits clear (and stopping
> at the first instance, since that's all that's required to determine
> that the array is not all clear), and once looking for bytes that are
> not all bits set (again stopping at the first instance),

The code does not do that. It scans for bytes that are all set and
stops at the first, flipping (so un-setting) the ALL_SET bit. I think
you have the two conditions flipped.

As it happens, I find the code re-written using two while loops much
easier to verify in my head, but that's because I've got into the habit
of inverting the test of a loop and checking that is makes sense just
after the loop.

Here is what you wrote, written as while loops, with the conditions that
must apply after the loop and in the following test as comments:

i = 0;
while (i < n && (unsigned char)~0U != data[i]) i++;
// i >= n or data[i] == -1
if (i < n)
// data[i] == -1
clrset ^= ALL_SET;

i = 0;
while (i < n && 0 != data[i]) i++;
// i >= n or data[i] == 0
if (i < n)
// data[i] == 0
clrset ^= ALL_CLEAR;

I don't think you would have got the test the wrong way round if you'd
written it this way. Here it is with the tests the right way round:

i = 0;
while (i < n && (unsigned char)~0U == data[i]) i++;
// i >= n or data[i] != -1
if (i < n)
// data[i] != -1
clrset ^= ALL_SET;

i = 0;
while (i < n && 0 == data[i]) i++;
// i >= n or data[i] != 0
if (i < n)
// data[i] != 0
clrset ^= ALL_CLEAR;

I think this is as obviously right and the previous code is obviously
wrong.

I do this automatically in my head (if the conditions are not too
complex) with every loop, so I notice, and worry, when I see a for loop
with a break in it because I know I've going to have to work harder to
check the code.

--
Ben.

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

<sm61sh$bdc$1@dont-email.me>

  copy mid

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

  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: Sat, 6 Nov 2021 15:01:55 +0100
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <sm61sh$bdc$1@dont-email.me>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo>
<87fssdh302.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Nov 2021 14:01:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="892a5df4e1e292968bec3b2f26e74d28";
logging-data="11692"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iW6ldeTf1h0m85YbaRwZbn51Nu6wMn/I="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:ZZFLO7i+UrVW7dp4Cz5Jd41pqxI=
In-Reply-To: <87fssdh302.fsf@bsb.me.uk>
Content-Language: de-DE
 by: Bonita Montero - Sat, 6 Nov 2021 14:01 UTC

Am 03.11.2021 um 12:50 schrieb Ben Bacarisse:
> 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);
>

N000b !

>> 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.
>

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

<86v915aqlg.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Sat, 06 Nov 2021 15:04:11 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <86v915aqlg.fsf@linuxsc.com>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk> <37fh5i-t92.ln1@aretha.foo> <87ilx5a6nf.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="352b810152e65a952f2faa649d7ab5f1";
logging-data="26974"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4vtsN6amuimdabmuGpSDAj5Eh1fcHiDk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:wIZ6ivZmTjZRQdI6zRbymjol12M=
sha1:fw92jQO9k3dbO7DZbMp9dBVPebE=
 by: Tim Rentsch - Sat, 6 Nov 2021 22:04 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

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

>> Perhaps you missed the intended logic (which, I admit, I
>> could've/should've commented better... or at all) that the returned
>> variable, clrset, is initialised to 3 (ALL_CLEAR | ALL_SET, which I
>> now wish I had used in the first place, for clarity) to assume that
>> all bits are set and all bits are clear. (Yeah, I know they can't
>> be both; but it's just a starting point.) The algorithm then
>> checks the array twice, once looking for bytes that are not all
>> bits clear (and stopping at the first instance, since that's all
>> that's required to determine that the array is not all clear), and
>> once looking for bytes that are not all bits set (again stopping at
>> the first instance),
>
> The code does not do that. It scans for bytes that are all set
> and stops at the first, flipping (so un-setting) the ALL_SET bit.
> I think you have the two conditions flipped.

I believe the original code is right except there are two ==
operators that should be != operators.

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

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

  copy mid

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

  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: Sat, 06 Nov 2021 22:06:35 +0000
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <87o86x547o.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<bits-20211029021300@ram.dialup.fu-berlin.de>
<sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk>
<37fh5i-t92.ln1@aretha.foo> <87ilx5a6nf.fsf@bsb.me.uk>
<86v915aqlg.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="05b9fa80338c97d653a7fb8fdb205590";
logging-data="3247"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+g35DBBsqB2f3zksZthzDAcX5GeTvPZJI="
Cancel-Lock: sha1:CNTTALXiFYazJ+Aa0cqaBIDktFY=
sha1:iPcj4E9lBT0PoV25WUcIeL4IRWo=
X-BSB-Auth: 1.fad24e8ca4a7c617c9e2.20211106220635GMT.87o86x547o.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 6 Nov 2021 22:06 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> writes:
>
>>> Perhaps you missed the intended logic (which, I admit, I
>>> could've/should've commented better... or at all) that the returned
>>> variable, clrset, is initialised to 3 (ALL_CLEAR | ALL_SET, which I
>>> now wish I had used in the first place, for clarity) to assume that
>>> all bits are set and all bits are clear. (Yeah, I know they can't
>>> be both; but it's just a starting point.) The algorithm then
>>> checks the array twice, once looking for bytes that are not all
>>> bits clear (and stopping at the first instance, since that's all
>>> that's required to determine that the array is not all clear), and
>>> once looking for bytes that are not all bits set (again stopping at
>>> the first instance),
>>
>> The code does not do that. It scans for bytes that are all set
>> and stops at the first, flipping (so un-setting) the ALL_SET bit.
>> I think you have the two conditions flipped.
>
> I believe the original code is right except there are two ==
> operators that should be != operators.

Yes (see my own follow up). In fact I took the operators to be correct
but the initialisation of clrset to be wrong, so I thought the OP was
aiming for a different function.

--
Ben.

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

<86r1bsc4b7.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Sat, 06 Nov 2021 15:22:36 -0700
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <86r1bsc4b7.fsf@linuxsc.com>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk> <37fh5i-t92.ln1@aretha.foo> <87ilx5a6nf.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="352b810152e65a952f2faa649d7ab5f1";
logging-data="26974"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vYsj7dG5KXihI2kkOTZmO/aJM48V8Sfs="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:ktlkbcqi+9eoG728wKm777i5kaM=
sha1:Sa2gunZ6blsJmrN0cs7A7Kewl0E=
 by: Tim Rentsch - Sat, 6 Nov 2021 22:22 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> i = 0;
> while (i < n && (unsigned char)~0U == data[i]) i++;
> // i >= n or data[i] != -1
> if (i < n)
> // data[i] != -1
> clrset ^= ALL_SET;
>
> i = 0;
> while (i < n && 0 == data[i]) i++;
> // i >= n or data[i] != 0
> if (i < n)
> // data[i] != 0
> clrset ^= ALL_CLEAR;

For fun:

size_t i = -1, clear = 1, set = 1;
unsigned char ones = -1;
while( clear | set && ++i < n ){
clear &= data[i] == 0, set &= data[i] == ones;
}
return set<<1 | clear;

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

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

  copy mid

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

  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: Sun, 07 Nov 2021 00:08:11 +0000
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <87fss86d5g.fsf@bsb.me.uk>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com>
<bits-20211029021300@ram.dialup.fu-berlin.de>
<sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk>
<37fh5i-t92.ln1@aretha.foo> <87ilx5a6nf.fsf@bsb.me.uk>
<86r1bsc4b7.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="14cbb517b2490b19a82ecd6051397a44";
logging-data="18475"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/b3gh1FAVjlJrIqpaJf2PAjeCRNNYHGvo="
Cancel-Lock: sha1:6q0nKXLWFy6j6FmKyD3Pbwp50UE=
sha1:PBBFJkz6gpe4VdUtTED1b+u97vo=
X-BSB-Auth: 1.2f00d154eedffae4a522.20211107000811GMT.87fss86d5g.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 7 Nov 2021 00:08 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> i = 0;
>> while (i < n && (unsigned char)~0U == data[i]) i++;
>> // i >= n or data[i] != -1
>> if (i < n)
>> // data[i] != -1
>> clrset ^= ALL_SET;
>>
>> i = 0;
>> while (i < n && 0 == data[i]) i++;
>> // i >= n or data[i] != 0
>> if (i < n)
>> // data[i] != 0
>> clrset ^= ALL_CLEAR;

For the record, not what I would write! I was explaining a point about
loops.

> For fun:
>
> size_t i = -1, clear = 1, set = 1;
> unsigned char ones = -1;
> while( clear | set && ++i < n ){
> clear &= data[i] == 0, set &= data[i] == ones;
> }
> return set<<1 | clear;
>

--
Ben.

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

<86ee7saqsf.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Check if all 256 bits are clear or set ?
Date: Sun, 07 Nov 2021 08:12:16 -0800
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <86ee7saqsf.fsf@linuxsc.com>
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk> <37fh5i-t92.ln1@aretha.foo> <87ilx5a6nf.fsf@bsb.me.uk> <86r1bsc4b7.fsf@linuxsc.com> <87fss86d5g.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="2dc722b95f3891b2f37c442b9fb0adbf";
logging-data="7671"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9oiWOB9r4urYLCzH6DANWH9Tdp7qrbMI="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:21dmjJaK79Pl0XYvDBTBhBec8is=
sha1:8vxeTAdH62jO5yydhApcm2X4A8M=
 by: Tim Rentsch - Sun, 7 Nov 2021 16:12 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> i = 0;
>>> while (i < n && (unsigned char)~0U == data[i]) i++;
>>> // i >= n or data[i] != -1
>>> if (i < n)
>>> // data[i] != -1
>>> clrset ^= ALL_SET;
>>>
>>> i = 0;
>>> while (i < n && 0 == data[i]) i++;
>>> // i >= n or data[i] != 0
>>> if (i < n)
>>> // data[i] != 0
>>> clrset ^= ALL_CLEAR;
>
> For the record, not what I would write! [...]

I never assumed otherwise. I was just having fun
with the code.

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

<cue36i-uq1.ln1@aretha.foo>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 12 Nov 2021 19:08:07 -0600
Message-Id: <cue36i-uq1.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: Sat, 13 Nov 2021 11:54:04 +1100
References: <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo> <87fssdh302.fsf@bsb.me.uk> <37fh5i-t92.ln1@aretha.foo> <87ilx5a6nf.fsf@bsb.me.uk> <86v915aqlg.fsf@linuxsc.com>
User-Agent: KNode/0.10.9
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
Lines: 36
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-xv1/mqgzxX7UgafC0GWh0xEkDvwFldrVvxplTHWVXh5WVc3ZF+AjldUp9331eJOuD5XoR/Tes3ojfVT!5UdXc/yb26FxrB7Q+9heblA3/lsWo8m3eTp0+HvpQ1aJe3IM
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: 2662
 by: Peter 'Shaggy&# - Sat, 13 Nov 2021 00:54 UTC

Groovy hepcat Tim Rentsch was jivin' in comp.lang.c on Sun, 7 Nov 2021
09:04 am. It's a cool scene! Dig it.

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> writes:
>
>>> Perhaps you missed the intended logic (which, I admit, I
>>> could've/should've commented better... or at all) that the returned
>>> variable, clrset, is initialised to 3 (ALL_CLEAR | ALL_SET, which I
>>> now wish I had used in the first place, for clarity) to assume that
>>> all bits are set and all bits are clear. (Yeah, I know they can't
>>> be both; but it's just a starting point.) The algorithm then
>>> checks the array twice, once looking for bytes that are not all
>>> bits clear (and stopping at the first instance, since that's all
>>> that's required to determine that the array is not all clear), and
>>> once looking for bytes that are not all bits set (again stopping at
>>> the first instance),
>>
>> The code does not do that. It scans for bytes that are all set
>> and stops at the first, flipping (so un-setting) the ALL_SET bit.
>> I think you have the two conditions flipped.
>
> I believe the original code is right except there are two ==
> operators that should be != operators.

Ah! (Slaps forehead.) Yes. Well spotted! :)

--

----- 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 ?

<e8053c75-b35e-4720-b141-6ac647ca06f8n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:4155:: with SMTP id e21mr7607471qtm.312.1637684597536;
Tue, 23 Nov 2021 08:23:17 -0800 (PST)
X-Received: by 2002:ac8:4159:: with SMTP id e25mr7707309qtm.69.1637684597364;
Tue, 23 Nov 2021 08:23:17 -0800 (PST)
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: Tue, 23 Nov 2021 08:23:17 -0800 (PST)
In-Reply-To: <37fh5i-t92.ln1@aretha.foo>
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>
<bits-20211029021300@ram.dialup.fu-berlin.de> <sm095i-892.ln1@aretha.foo>
<87fssdh302.fsf@bsb.me.uk> <37fh5i-t92.ln1@aretha.foo>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e8053c75-b35e-4720-b141-6ac647ca06f8n@googlegroups.com>
Subject: Re: Check if all 256 bits are clear or set ?
From: skybuck2...@hotmail.com (skybuck2000)
Injection-Date: Tue, 23 Nov 2021 16:23:17 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 131
 by: skybuck2000 - Tue, 23 Nov 2021 16:23 UTC

Conceptually this is very smart to invert the logic !

Your idea (Peter 'Shaggy' Haywood) has given me a new idea for the code:

Instead of wasting cpu cycles/times checking everything instead only check what is needed to come to a certain "inverted conclusions".

It is indeed not necessary to check all bits to see if they are all clear, if one of them is not clear then obviously the data is not empty, and the function can exit earlier.

It is indeed not necessary to check all bits to see if they are all set, if one of them is not set then obviously the data is not full, and the function can exit earlier.

HAHA ! Pretty smart and brilliant ! ;) =D

Or is it ? hmmmm

Maybe not.... it depends...

Let's analyze code below:

{$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}

Theoretically it needs to pull in 8x 32 bit integers, this could strain the memory bus somewhat, slow down the cpu, while it's waiting for data.... and it may pollute the l1 data cache unnecessarily.

On the other hand it does only one branch comparison.

Now the alternative would be to do 8 branch comparisons. Perhaps there is a golden ground in the middle, maybe only do 2 or 4 comparisons that is a possibility as well.

So different combinations could be tried to see which one performs best.

Here is where a little bit of artificial intelligence could be handy.

The application could measure all versions during active/real world usage with statisticall data tracking... for the first few minutes or so...

Then once another variations and statistics collected it can switch to non-statistical methods and pick the one which it believes is the fastest.

Anyway let's create a worst case counter example and analyze that, untested code:

To cool thing is this also gets rid of the extra variable.

function TData.IsEmpty : boolean;
type
TUInt32Array = packed array[0..7] of longword;
begin
result := False;

// if not empty then exit.
if TUInt32Array(mData)[0]) <> 0 then exit;
if TUInt32Array(mData)[1]) <> 0 then exit;
if TUInt32Array(mData)[2]) <> 0 then exit;
if TUInt32Array(mData)[3]) <> 0 then exit;
if TUInt32Array(mData)[4]) <> 0 then exit;
if TUInt32Array(mData)[5]) <> 0 then exit;
if TUInt32Array(mData)[6]) <> 0 then exit;
if TUInt32Array(mData)[7]) <> 0 then exit;

result := True; // if execution reaches here then return true.
end;
{$endif}

Pretty cool,

These are still 8 data accesses, no or instructions, 8 comparisons, possibly 8 jumps

On average this will probably run at 4,4,4

It will take up some more branch predicators, less data cache

Hard to say which one will run faster ;) for now my bet would be on the first one, because or instructions are 1 cycle and produces can be 15 or something.

However data access can be in the range of 400 nanoseconds, maybe 400 cycles, that could severely impact performance.

And this this newer version might run faster, depends on how could the L1 data cache lining is, if it's good maybe 10 cycles per data access... for a average total of 40.

Hmmm... I may have to benchmark this sometime ! ;)

And the isFull version:

// untested code

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

// if not full exit
if TUInt32Array(mData)[0] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[1] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[2] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[3] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[4] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[5] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[6] <> $FFFFFFFF then exit;
if TUInt32Array(mData)[7] <> $FFFFFFFF then exit;

result := True;
end;
{$endif}

Ok interesting,

Now we truely have something to benchmark ! ;) =D

I am kinda busy so this will have to wait time a further time, but other people are welcome to benchmark this themselfes ! ;) :)

Bye for now,
And thanks for the feedback and ideas !
Skybuck Flying ! =D

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor