Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #161932
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: naked switches |
| Date | 2021-07-16 03:53 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <864kcu7cnx.fsf@linuxsc.com> (permalink) |
| References | (7 earlier) <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> |
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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
naked switches Robert Finch <robfi680@gmail.com> - 2021-07-07 16:39 -0700
Re: naked switches Bart <bc@freeuk.com> - 2021-07-08 01:26 +0100
Re: naked switches Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-08 01:57 +0000
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-08 09:16 +0200
Re: naked switches Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-08 01:00 -0700
Re: naked switches Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-07-08 03:25 -0700
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-08 06:58 -0700
Re: naked switches Bart <bc@freeuk.com> - 2021-07-08 15:52 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-08 17:52 +0200
Re: naked switches Bart <bc@freeuk.com> - 2021-07-08 17:51 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 10:01 +0200
Re: naked switches Bart <bc@freeuk.com> - 2021-07-09 11:09 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 13:14 +0200
Re: naked switches Manfred <noname@add.invalid> - 2021-07-09 15:35 +0200
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 16:51 +0200
Re: naked switches Manfred <noname@add.invalid> - 2021-07-09 18:23 +0200
Re: naked switches Bart <bc@freeuk.com> - 2021-07-09 16:07 +0100
Re: naked switches Manfred <noname@add.invalid> - 2021-07-09 18:41 +0200
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-10 11:02 +0200
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 19:20 +0200
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-09 10:46 -0700
Re: naked switches Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-09 19:56 +0000
Re: naked switches Bart <bc@freeuk.com> - 2021-07-09 22:01 +0100
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-09 15:07 -0700
Re: naked switches Bart <bc@freeuk.com> - 2021-07-09 23:30 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-10 12:17 +0200
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-08 10:18 -0700
Re: naked switches Bart <bc@freeuk.com> - 2021-07-08 18:43 +0100
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-08 15:32 -0700
Re: naked switches Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-08 17:47 +0000
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-08 15:45 -0700
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 10:27 +0200
Re: naked switches Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-09 11:13 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 13:30 +0200
Re: naked switches Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-11 00:39 -0700
Re: naked switches scott@slp53.sl.home (Scott Lurndal) - 2021-07-11 14:06 +0000
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-07-11 08:01 -0700
Re: naked switches Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-16 03:53 -0700
Re: naked switches scott@slp53.sl.home (Scott Lurndal) - 2021-07-16 14:47 +0000
Re: naked switches Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-09-30 06:09 -0700
Re: naked switches Robert Finch <robfi680@gmail.com> - 2021-09-30 07:03 -0700
Re: naked switches Bart <bc@freeuk.com> - 2021-07-09 13:44 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-10 13:28 +0200
Re: naked switches Bart <bc@freeuk.com> - 2021-07-10 13:04 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-10 15:39 +0200
Re: naked switches Bart <bc@freeuk.com> - 2021-07-10 15:08 +0100
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-10 17:34 +0200
Re: naked switches Bart <bc@freeuk.com> - 2021-07-10 17:47 +0100
Re: naked switches Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-11 00:57 -0700
Re: naked switches Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-16 03:51 -0700
Re: naked switches Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-08 11:40 -0700
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 10:38 +0200
Re: naked switches Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-08 17:33 +0000
Re: naked switches scott@slp53.sl.home (Scott Lurndal) - 2021-07-08 17:47 +0000
Re: naked switches Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-08 17:58 +0000
Re: naked switches scott@slp53.sl.home (Scott Lurndal) - 2021-07-08 19:13 +0000
Re: naked switches Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-09 02:30 +0000
Re: naked switches David Brown <david.brown@hesbynett.no> - 2021-07-09 10:41 +0200
Re: naked switches Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-30 18:04 +0200
csiph-web