Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

We have a equal opportunity Calculus class -- it's fully integrated.


devel / comp.lang.c / Re: naked switches

SubjectAuthor
* naked switchesRobert Finch
+- Re: naked switchesBart
+* Re: naked switchesKaz Kylheku
|`- Re: naked switchesDavid Brown
+* Re: naked switchesKeith Thompson
|`* Re: naked switchesMalcolm McLean
| `* Re: naked switchesRobert Finch
|  +- Re: naked switchesBart
|  +* Re: naked switchesDavid Brown
|  |+* Re: naked switchesBart
|  ||`* Re: naked switchesDavid Brown
|  || `* Re: naked switchesBart
|  ||  +- Re: naked switchesDavid Brown
|  ||  `* Re: naked switchesManfred
|  ||   +* Re: naked switchesDavid Brown
|  ||   |`- Re: naked switchesManfred
|  ||   `* Re: naked switchesBart
|  ||    +* Re: naked switchesManfred
|  ||    |`- Re: naked switchesDavid Brown
|  ||    `* Re: naked switchesDavid Brown
|  ||     `* Re: naked switchesRobert Finch
|  ||      +- Re: naked switchesKaz Kylheku
|  ||      +* Re: naked switchesBart
|  ||      |`* Re: naked switchesRobert Finch
|  ||      | `- Re: naked switchesBart
|  ||      `- Re: naked switchesDavid Brown
|  |`* Re: naked switchesRobert Finch
|  | +* Re: naked switchesBart
|  | |`- Re: naked switchesRobert Finch
|  | `* Re: naked switchesKaz Kylheku
|  |  +* Re: naked switchesRobert Finch
|  |  |`* Re: naked switchesDavid Brown
|  |  | +* Re: naked switchesBen Bacarisse
|  |  | |+- Re: naked switchesDavid Brown
|  |  | |`* Re: naked switchesTim Rentsch
|  |  | | `* Re: naked switchesScott Lurndal
|  |  | |  +- Re: naked switchesRobert Finch
|  |  | |  `* Re: naked switchesTim Rentsch
|  |  | |   `* Re: naked switchesScott Lurndal
|  |  | |    `* Re: naked switchesTim Rentsch
|  |  | |     `- Re: naked switchesRobert Finch
|  |  | `* Re: naked switchesBart
|  |  |  `* Re: naked switchesDavid Brown
|  |  |   `* Re: naked switchesBart
|  |  |    `* Re: naked switchesDavid Brown
|  |  |     `* Re: naked switchesBart
|  |  |      `* Re: naked switchesDavid Brown
|  |  |       `- Re: naked switchesBart
|  |  `* Re: naked switchesTim Rentsch
|  |   `- Re: naked switchesTim Rentsch
|  `* Re: naked switchesKeith Thompson
|   `- Re: naked switchesDavid Brown
+* Re: naked switchesKaz Kylheku
|`* Re: naked switchesScott Lurndal
| `* Re: naked switchesKaz Kylheku
|  +* Re: naked switchesScott Lurndal
|  |`- Re: naked switchesKaz Kylheku
|  `- Re: naked switchesDavid Brown
+- Re: naked switchesStefan Ram
`- Re: naked switchesBonita Montero

Pages:123
Re: naked switches

<865yxh8fkf.fsf@linuxsc.com>

  copy mid

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

  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: naked switches
Date: Sun, 11 Jul 2021 00:39:28 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <865yxh8fkf.fsf@linuxsc.com>
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <8735sp8cbm.fsf@nosuchdomain.example.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="52e1adecb53146628fb5ee5c083fc413";
logging-data="8554"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+Izq2OSUPwB2ZIEvoVGLpZoSE5BDpjh4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:x3NLWlX08eGMtroID6YOOyZ4wrI=
sha1:BquvgjAt9FaPTOIB/Mp6PoK/0EY=
 by: Tim Rentsch - Sun, 11 Jul 2021 07:39 UTC

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

> David Brown <david.brown@hesbynett.no> writes:
>
>> Your extension [for extracting contiguous bits from a word] is not
>> commonly used, because it only exists in your tool. People who need
>> to do a lot of bit slicing and don't like masking and shifting (or
>> struct bit-fields) should not have trouble defining:
>>
>> #define BIT(x, top, bottom) \
>> (((x) >> (bottom)) & ((1llu << ((top) - (bottom) + 1)) - 1))
>>
>> Yes, the parenthesis you need for macros are ugly, but you only need to
>> get it right once, and now you can read your bit-fields.
>
> That's undefined when all the bits are wanted. (The left shift of 1llu
> could be equal to the width.) Maybe you wrote it for a compiler that
> documents an extension.
>
> I usually right shift -1llu by the width minus the number of bits
> wanted, but you can also just do two left shifts.

Another way, assuming the mask width desired is greater
than zero, is this

((1LLU << width-1) -1) *2 +1

which works great if 'width' is a compile-time constant.

Re: naked switches

<861r858eqz.fsf@linuxsc.com>

  copy mid

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

  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: naked switches
Date: Sun, 11 Jul 2021 00:57:08 -0700
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <861r858eqz.fsf@linuxsc.com>
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <8735sp8cbm.fsf@nosuchdomain.example.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="52e1adecb53146628fb5ee5c083fc413";
logging-data="8554"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/boOOC7cCeodrC2rpE/rvVxiLa3c6AGN4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:+lCvtHN4CXnbyMYIrK0FBl5sgiU=
sha1:xyePTU6SmsfDOAlk4AbJ6726k4k=
 by: Tim Rentsch - Sun, 11 Jul 2021 07:57 UTC

Kaz Kylheku <563-365-8930@kylheku.com> writes:

> On 2021-07-08, Robert Finch <robfi680@gmail.com> wrote:
>
>>> You wrote that you "have been working on a C/C++ like compiler" - do you
>>> mean you have been /using/ such a compiler, or you have been /writing/
>>> such a compiler?
>>
>> I have been using it for simple demos, actually running the compiled
>> code sometime. It has been revamped for several different custom
>> processors, but is still a work in progress.
>>
>>> As I mentioned earlier, I think a "naked switch" like this is a terrible
>>> idea. It is not something I have seen on other compilers, and I've used
>>> quite a large number over the years for far smaller and slower devices
>>> than you are describing here.
>>
>> It is a bad idea in terms of allowing program crashes. But then
>> there are naked functions.
>>
>> CC64 supports a number of ?features? not found in standard C. But I
>> call it a ?C? like compiler because it can compile most C code
>> without changes. I used it to compile the standard C library.
>> Stills lots of bugs in the compiler though.
>>
>> One feature I like is the ability to manipulate bit slices.
>>
>> int a, b;
>>
>> a = b[10:1];
>>
>> Sets a equal to bits 1 to 10 of b. A bit slice can be distinguished
>> from an array index by the colon. Compiles painlessly directly to
>> field manipulation instructions and gets rid of code like: a = (b >>
>> 1) & 0x3ff;
>
> A macro can do this:
>
> a = BIT(b,10,1);
>
> and works everywhere. What you need is a typeof extension to make it
> work with different integer types while retaining efficiency, otherwise
> you're looking at making it assume 64 bit, or saddling it with a type
> name argument. [... and later _Generic is mentioned.]

No typeof is needed, nor _Generic, nor type name argument, nor
forcing a width of 64 bits, to define a macro that produces an
"and" of the appropriate size with a compile-time constant (as
evidenced by both gcc and clang with -O0). Just straight C90.

(I'll try to post a followup in a day or two, if necessary, to
show how. But I expect someone here will get it before then.)

Re: naked switches

<3WCGI.8419$rr3.5144@fx34.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx34.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: naked switches
Newsgroups: comp.lang.c
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <8735sp8cbm.fsf@nosuchdomain.example.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk> <865yxh8fkf.fsf@linuxsc.com>
Lines: 78
Message-ID: <3WCGI.8419$rr3.5144@fx34.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sun, 11 Jul 2021 14:06:55 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sun, 11 Jul 2021 14:06:55 GMT
X-Received-Bytes: 3684
 by: Scott Lurndal - Sun, 11 Jul 2021 14:06 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> Your extension [for extracting contiguous bits from a word] is not
>>> commonly used, because it only exists in your tool. People who need
>>> to do a lot of bit slicing and don't like masking and shifting (or
>>> struct bit-fields) should not have trouble defining:
>>>
>>> #define BIT(x, top, bottom) \
>>> (((x) >> (bottom)) & ((1llu << ((top) - (bottom) + 1)) - 1))
>>>
>>> Yes, the parenthesis you need for macros are ugly, but you only need to
>>> get it right once, and now you can read your bit-fields.
>>
>> That's undefined when all the bits are wanted. (The left shift of 1llu
>> could be equal to the width.) Maybe you wrote it for a compiler that
>> documents an extension.
>>
>> I usually right shift -1llu by the width minus the number of bits
>> wanted, but you can also just do two left shifts.
>
>Another way, assuming the mask width desired is greater
>than zero, is this
>
> ((1LLU << width-1) -1) *2 +1
>
>which works great if 'width' is a compile-time constant.

In the C++ world, we use this:

namespace bit
{ template <class T> static inline T maskT(size_t bits)
{
if (bits >= sizeof(T)*8)
return -1;
else
return ~(static_cast<T>(-1)<<bits);
}

template<class T> static inline T extract(T input, size_t stop_bit, size_t start_bit)
{
input >>= start_bit;
input &= maskT<T>(stop_bit - start_bit + 1);
return input;
}

static inline int64_t extracts(uint64_t input, size_t stop_bit, size_t start_bit)
{
int64_t imm = extract(input, stop_bit, start_bit);
int shift = 63-stop_bit+start_bit;
imm <<= shift;
imm >>= shift;
return imm;
}

template<class T> static inline T insert(T original, uint64_t insert, size_t start_bit, size_t width_bits)
{
T mask = bit::maskT<T>(width_bits);
T newdata = mask & insert;
mask <<= start_bit;
newdata <<= start_bit;
original &= ~mask;
original |= newdata;
return original;
}
};

uint64_t registervalue, field;
int64_t field1;

field = bit::extract(registervalue, 15, 0); /* Extract <15:0> and zero-extend */
field1 = bit::extracts((int64_t)registervalue, 20, 16); /* Extract and sign-extend */

registervalue = bit::insert(registervalue, 0xff, 16, 4); /* Set bits<19:16> == 0xff */

Re: naked switches

<93b4fe04-1925-4089-8eb3-ecba3486c939n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:1192:: with SMTP id b18mr31712208qkk.100.1626015666768; Sun, 11 Jul 2021 08:01:06 -0700 (PDT)
X-Received: by 2002:ad4:5e8b:: with SMTP id jl11mr6154670qvb.36.1626015666584; Sun, 11 Jul 2021 08:01:06 -0700 (PDT)
Path: i2pn2.org!rocksolid2!news.neodome.net!news.uzoreto.com!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.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: Sun, 11 Jul 2021 08:01:06 -0700 (PDT)
In-Reply-To: <3WCGI.8419$rr3.5144@fx34.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:fea8:1de1:d400:d06b:84a1:4eb:edff; posting-account=QId4bgoAAABV4s50talpu-qMcPp519Eb
NNTP-Posting-Host: 2607:fea8:1de1:d400:d06b:84a1:4eb:edff
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <8735sp8cbm.fsf@nosuchdomain.example.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk> <865yxh8fkf.fsf@linuxsc.com> <3WCGI.8419$rr3.5144@fx34.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <93b4fe04-1925-4089-8eb3-ecba3486c939n@googlegroups.com>
Subject: Re: naked switches
From: robfi...@gmail.com (Robert Finch)
Injection-Date: Sun, 11 Jul 2021 15:01:06 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 104
 by: Robert Finch - Sun, 11 Jul 2021 15:01 UTC

On Sunday, July 11, 2021 at 10:07:09 AM UTC-4, Scott Lurndal wrote:
> Tim Rentsch <tr.1...@z991.linuxsc.com> writes:
> >Ben Bacarisse <ben.u...@bsb.me.uk> writes:
> >
> >> David Brown <david...@hesbynett.no> writes:
> >>
> >>> Your extension [for extracting contiguous bits from a word] is not
> >>> commonly used, because it only exists in your tool. People who need
> >>> to do a lot of bit slicing and don't like masking and shifting (or
> >>> struct bit-fields) should not have trouble defining:
> >>>
> >>> #define BIT(x, top, bottom) \
> >>> (((x) >> (bottom)) & ((1llu << ((top) - (bottom) + 1)) - 1))
> >>>
> >>> Yes, the parenthesis you need for macros are ugly, but you only need to
> >>> get it right once, and now you can read your bit-fields.
> >>
> >> That's undefined when all the bits are wanted. (The left shift of 1llu
> >> could be equal to the width.) Maybe you wrote it for a compiler that
> >> documents an extension.
> >>
> >> I usually right shift -1llu by the width minus the number of bits
> >> wanted, but you can also just do two left shifts.
> >
> >Another way, assuming the mask width desired is greater
> >than zero, is this
> >
> > ((1LLU << width-1) -1) *2 +1
> >
> >which works great if 'width' is a compile-time constant.
> In the C++ world, we use this:
>
> namespace bit
> {
> template <class T> static inline T maskT(size_t bits)
> {
> if (bits >= sizeof(T)*8)
> return -1;
> else
> return ~(static_cast<T>(-1)<<bits);
> }
>
> template<class T> static inline T extract(T input, size_t stop_bit, size_t start_bit)
> {
> input >>= start_bit;
> input &= maskT<T>(stop_bit - start_bit + 1);
> return input;
> }
>
> static inline int64_t extracts(uint64_t input, size_t stop_bit, size_t start_bit)
> {
> int64_t imm = extract(input, stop_bit, start_bit);
> int shift = 63-stop_bit+start_bit;
> imm <<= shift;
> imm >>= shift;
> return imm;
> }
>
> template<class T> static inline T insert(T original, uint64_t insert, size_t start_bit, size_t width_bits)
> {
> T mask = bit::maskT<T>(width_bits);
> T newdata = mask & insert;
> mask <<= start_bit;
> newdata <<= start_bit;
> original &= ~mask;
> original |= newdata;
> return original;
> }
> };
>
> uint64_t registervalue, field;
> int64_t field1;
>
> field = bit::extract(registervalue, 15, 0); /* Extract <15:0> and zero-extend */
> field1 = bit::extracts((int64_t)registervalue, 20, 16); /* Extract and sign-extend */
>
> registervalue = bit::insert(registervalue, 0xff, 16, 4); /* Set bits<19:16> == 0xff */

I use the bit-slicing notation in part because the compiler I am using is not very sophisticated.
It cannot determine from a group of shifting and masking operations what bitfield instruction
to use. It does not have very sophisticated pattern matching for instruction sequences. Hence
to get efficient code from the compiler the bit-slicing syntax was added. It is as much to
support the compiler as it is for ease of use by the programmer.
x = a[15:10]
Compiles directly to an extract instruction something like: ext $t0,$a0,#10,#5 – a single
instruction. If shifting and masking macros were used, the compiler would likely generate all
the operations, turning what should be a single cycle op into a multi-cycle multi-instruction op.

Re: naked switches

<868s267cqn.fsf@linuxsc.com>

  copy mid

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

  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: naked switches
Date: Fri, 16 Jul 2021 03:51:44 -0700
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <868s267cqn.fsf@linuxsc.com>
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <8735sp8cbm.fsf@nosuchdomain.example.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <861r858eqz.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="09634a13793bf13421bcf3f217467df0";
logging-data="9110"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bF/ypoOZ4FOBSjC1jHhVnzvRaJ6kxPF4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:XfACfF7tRgjz1J7VYtLlDnCEvys=
sha1:2Xh8mdThYIg7mjMySXdPPfj05kI=
 by: Tim Rentsch - Fri, 16 Jul 2021 10:51 UTC

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

> Kaz Kylheku <563-365-8930@kylheku.com> writes:
>
>> On 2021-07-08, Robert Finch <robfi680@gmail.com> wrote:
>>
>>>> You wrote that you "have been working on a C/C++ like compiler" - do you
>>>> mean you have been /using/ such a compiler, or you have been /writing/
>>>> such a compiler?
>>>
>>> I have been using it for simple demos, actually running the compiled
>>> code sometime. It has been revamped for several different custom
>>> processors, but is still a work in progress.
>>>
>>>> As I mentioned earlier, I think a "naked switch" like this is a terrible
>>>> idea. It is not something I have seen on other compilers, and I've used
>>>> quite a large number over the years for far smaller and slower devices
>>>> than you are describing here.
>>>
>>> It is a bad idea in terms of allowing program crashes. But then
>>> there are naked functions.
>>>
>>> CC64 supports a number of ?features? not found in standard C. But I
>>> call it a ?C? like compiler because it can compile most C code
>>> without changes. I used it to compile the standard C library.
>>> Stills lots of bugs in the compiler though.
>>>
>>> One feature I like is the ability to manipulate bit slices.
>>>
>>> int a, b;
>>>
>>> a = b[10:1];
>>>
>>> Sets a equal to bits 1 to 10 of b. A bit slice can be distinguished
>>> from an array index by the colon. Compiles painlessly directly to
>>> field manipulation instructions and gets rid of code like: a = (b >>
>>> 1) & 0x3ff;
>>
>> A macro can do this:
>>
>> a = BIT(b,10,1);
>>
>> and works everywhere. What you need is a typeof extension to make it
>> work with different integer types while retaining efficiency, otherwise
>> you're looking at making it assume 64 bit, or saddling it with a type
>> name argument. [... and later _Generic is mentioned.]
>
> No typeof is needed, nor _Generic, nor type name argument, nor
> forcing a width of 64 bits, to define a macro that produces an
> "and" of the appropriate size with a compile-time constant (as
> evidenced by both gcc and clang with -O0). Just straight C90.
>
> (I'll try to post a followup in a day or two, if necessary, to
> show how. But I expect someone here will get it before then.)

To extract a "field" from expression 'e' of width 'w' at position 'p'

#define FIELD(e,w,p) ( (e)>>(p) & ( ( (0?(e):1) << (w)-1 ) -1) *2 +1 )

The idiom (0?(e):1) produces the value 1 in a type wide enough to mask
a field in the expression of 'e' (assuming the field does not include
the sign bit of a signed integer type).

Re: naked switches

<864kcu7cnx.fsf@linuxsc.com>

  copy mid

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

  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: naked switches
Date: Fri, 16 Jul 2021 03:53:22 -0700
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <864kcu7cnx.fsf@linuxsc.com>
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <8735sp8cbm.fsf@nosuchdomain.example.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk> <865yxh8fkf.fsf@linuxsc.com> <3WCGI.8419$rr3.5144@fx34.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="09634a13793bf13421bcf3f217467df0";
logging-data="9110"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194yMprumshQvmF7MJQR9CWRsYQAmC52lQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:9cIBT2xwtKtYxx7KSU8z/qry7t0=
sha1:n2jHhBhk4PlcN3KFO76w5sLPfwY=
 by: Tim Rentsch - Fri, 16 Jul 2021 10:53 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> Your extension [for extracting contiguous bits from a word] is not
>>>> commonly used, because it only exists in your tool. People who need
>>>> to do a lot of bit slicing and don't like masking and shifting (or
>>>> struct bit-fields) should not have trouble defining:
>>>>
>>>> #define BIT(x, top, bottom) \
>>>> (((x) >> (bottom)) & ((1llu << ((top) - (bottom) + 1)) - 1))
>>>>
>>>> Yes, the parenthesis you need for macros are ugly, but you only need to
>>>> get it right once, and now you can read your bit-fields.
>>>
>>> That's undefined when all the bits are wanted. (The left shift of 1llu
>>> could be equal to the width.) Maybe you wrote it for a compiler that
>>> documents an extension.
>>>
>>> I usually right shift -1llu by the width minus the number of bits
>>> wanted, but you can also just do two left shifts.
>>
>> Another way, assuming the mask width desired is greater
>> than zero, is this
>>
>> ((1LLU << width-1) -1) *2 +1
>>
>> which works great if 'width' is a compile-time constant.
>
> In the C++ world, we use this:
>
> namespace bit
> {
> template <class T> static inline T maskT(size_t bits)
> {
> if (bits >= sizeof(T)*8)
> return -1;
> else
> return ~(static_cast<T>(-1)<<bits);
> }
>
> template<class T> static inline T extract(T input, size_t stop_bit, size_t start_bit)
> {
> input >>= start_bit;
> input &= maskT<T>(stop_bit - start_bit + 1);
> return input;
> }
>
> static inline int64_t extracts(uint64_t input, size_t stop_bit, size_t start_bit)
> {
> int64_t imm = extract(input, stop_bit, start_bit);
> int shift = 63-stop_bit+start_bit;
> imm <<= shift;
> imm >>= shift;
> return imm;
> }
>
> template<class T> static inline T insert(T original, uint64_t insert, size_t start_bit, size_t width_bits)
> {
> T mask = bit::maskT<T>(width_bits);
> T newdata = mask & insert;
> mask <<= start_bit;
> newdata <<= start_bit;
> original &= ~mask;
> original |= newdata;
> return original;
> }
> };
>
> uint64_t registervalue, field;
> int64_t field1;
>
> field = bit::extract(registervalue, 15, 0); /* Extract <15:0> and zero-extend */
> field1 = bit::extracts((int64_t)registervalue, 20, 16); /* Extract and sign-extend */
>
> registervalue = bit::insert(registervalue, 0xff, 16, 4); /* Set bits<19:16> == 0xff */

Ahh, rather like using an elephant gun to shoot a field mouse.

Re: naked switches

<p_gII.3896$5Y6.3223@fx10.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx10.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: naked switches
Newsgroups: comp.lang.c
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk> <865yxh8fkf.fsf@linuxsc.com> <3WCGI.8419$rr3.5144@fx34.iad> <864kcu7cnx.fsf@linuxsc.com>
Lines: 22
Message-ID: <p_gII.3896$5Y6.3223@fx10.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 16 Jul 2021 14:47:49 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 16 Jul 2021 14:47:49 GMT
X-Received-Bytes: 1881
 by: Scott Lurndal - Fri, 16 Jul 2021 14:47 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>

>>
>> uint64_t registervalue, field;
>> int64_t field1;
>>
>> field = bit::extract(registervalue, 15, 0); /* Extract <15:0> and zero-extend */
>> field1 = bit::extracts((int64_t)registervalue, 20, 16); /* Extract and sign-extend */
>>
>> registervalue = bit::insert(registervalue, 0xff, 16, 4); /* Set bits<19:16> == 0xff */
>
>Ahh, rather like using an elephant gun to shoot a field mouse.

The compiler generates optimal code[*]. The source is descriptive
and far more readable (and less subject to error) than manual bit
shifting/masking.

I don't see a problem.

[*] As inline functions, they're translated with the rest of the function.

Re: naked switches

<86sfxmtdke.fsf@linuxsc.com>

  copy mid

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

  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: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: naked switches
Date: Thu, 30 Sep 2021 06:09:37 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <86sfxmtdke.fsf@linuxsc.com>
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com> <255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com> <sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk> <865yxh8fkf.fsf@linuxsc.com> <3WCGI.8419$rr3.5144@fx34.iad> <864kcu7cnx.fsf@linuxsc.com> <p_gII.3896$5Y6.3223@fx10.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="d4788905ada0446dfc3ae6b02ecc1355";
logging-data="28032"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18787lbj56CzNLdTSwnAxF6ZQ+bteIYfx0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:UkBzJFr+9Sq/z8bYXRrxMZfWi2I=
sha1:e365Fa0WwCoegyDqwwL497y0PBQ=
 by: Tim Rentsch - Thu, 30 Sep 2021 13:09 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> scott@slp53.sl.home (Scott Lurndal) writes:
>>
>
>>>
>>> uint64_t registervalue, field;
>>> int64_t field1;
>>>
>>> field = bit::extract(registervalue, 15, 0); /* Extract <15:0> and zero-extend */
>>> field1 = bit::extracts((int64_t)registervalue, 20, 16); /* Extract and sign-extend */
>>>
>>> registervalue = bit::insert(registervalue, 0xff, 16, 4); /* Set bits<19:16> == 0xff */
>>
>> Ahh, rather like using an elephant gun to shoot a field mouse.
>
> The compiler generates optimal code[*].

Perhaps some compilers do, but it's more likely that fast code
will result from a purely functional expression that computes
the desired result directly. Not that code quality had anything to
do with my earlier comments.

> The source is descriptive and far more readable (and less subject
> to error) than manual bit shifting/masking.

That's funny. Reminds me of things I used to hear 50 years ago when
people would say assembly language is easier to understand than the
high-level languages of the day.

> I don't see a problem.

To me it looks like you're confusing length and verbosity with
comprehensibility.

Re: naked switches

<afbef2eb-dde1-4bfd-bb88-4419eb1c0733n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:b087:: with SMTP id z129mr4773099qke.357.1633010608610;
Thu, 30 Sep 2021 07:03:28 -0700 (PDT)
X-Received: by 2002:ac8:4e30:: with SMTP id d16mr6663200qtw.309.1633010608464;
Thu, 30 Sep 2021 07:03:28 -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, 30 Sep 2021 07:03:28 -0700 (PDT)
In-Reply-To: <86sfxmtdke.fsf@linuxsc.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:fea8:1de5:5800:c81e:65b7:90e0:d32e;
posting-account=QId4bgoAAABV4s50talpu-qMcPp519Eb
NNTP-Posting-Host: 2607:fea8:1de5:5800:c81e:65b7:90e0:d32e
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com>
<255c09e7-2365-4983-ad4d-3bfc83cf04e7n@googlegroups.com> <31342574-cb69-4ad6-8576-387dcf9caf70n@googlegroups.com>
<sc76v8$8ah$1@dont-email.me> <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com>
<20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com>
<sc9190$h4g$1@dont-email.me> <87czrr3ics.fsf@bsb.me.uk> <865yxh8fkf.fsf@linuxsc.com>
<3WCGI.8419$rr3.5144@fx34.iad> <864kcu7cnx.fsf@linuxsc.com>
<p_gII.3896$5Y6.3223@fx10.iad> <86sfxmtdke.fsf@linuxsc.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <afbef2eb-dde1-4bfd-bb88-4419eb1c0733n@googlegroups.com>
Subject: Re: naked switches
From: robfi...@gmail.com (Robert Finch)
Injection-Date: Thu, 30 Sep 2021 14:03:28 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 12
 by: Robert Finch - Thu, 30 Sep 2021 14:03 UTC

I have modified my compiler to recognize when switches are one-hot powers of two and it uses the BBS instruction if available. No special syntax required for switches.
I added syntax to enums however to allow using an enum to define one-hot values.

enum (2*) { a, b, c, d, e};

Assigns the values 0,1,2,4,8 to a,b,c,d,e respectively.
The (2*) is the exponential increment spec. (3*, 4*, etc is also possible).

enums in my compiler also allow the increment to vary

enum(3) { a, b, c, d, e}; for instance increments the enum value by 3 for each symbol.

Re: naked switches

<sj4n6b$f7i$1@dont-email.me>

  copy mid

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

  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: naked switches
Date: Thu, 30 Sep 2021 18:04:26 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <sj4n6b$f7i$1@dont-email.me>
References: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 30 Sep 2021 16:04:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="891781f2b9e20bda4d5686aa40e9a95c";
logging-data="15602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fY9eHlvJszJ8jD/nyjL+ipZpltaRYPJU="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:c3dXbb5xMK9RDHhzvewVwDmpg7E=
In-Reply-To: <06a0049a-2d7d-40f0-899a-fb35c9a15d5fn@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 30 Sep 2021 16:04 UTC

Am 08.07.2021 um 01:39 schrieb Robert Finch:
> I have been working on a C/C++ like compiler. One feature supported in the compiler is naked switches. A naked switch omits the range checking code that is normally associated with the switch statement. Omitting this code can improve performance at the risk of a crash if invalid cases are processed. I am wondering if there is a similar option in other C compilers? Or would this just be an automatic optimization at high levels?
>
> Normal Switch:
>
> ; switch(x) {
> ldo $t0,64[$fp]
> sge $t1,$t0,#1 ; x varies between 1 and 12
> sle $t2,$t0,#12
> and $t1,$t1,$t2
> beq $t1,TestSwitch_89
> sub $t0,$t0,#1
> sll $t0,$t0,#4
> ldo $t0,TestSwitch_116[$t0]
> jmp $t0
>
> Naked Switch
> ; switch(x; naked) {
> ldo $t0,64[$fp]
> sub $t0,$t0,#1
> sll $t0,$t0,#4
> ldo $t0,TestSwitch_144[$t0]
> jmp $t0
>

Don't make such strange C-extensions.
Help the optimizer with sth. like __assume in MSVC++.

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor