Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163858
| From | Bart <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Book or tutorial on standard C threads |
| Date | 2021-12-16 14:16 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <spfhn3$370$1@dont-email.me> (permalink) |
| References | (5 earlier) <dd0c4355-bae4-42d1-a1cf-5898cb7c7b50n@googlegroups.com> <spd6hp$t5b$1@dont-email.me> <spd8gl$dq4$1@dont-email.me> <spdfc4$1l6$1@dont-email.me> <spenqd$t0c$1@dont-email.me> |
On 16/12/2021 06:54, Bonita Montero wrote:
> Here's another good example of using long lambdas:
>
> auto gatherTLBs = [&]( bool update ) -> size_t
> {
> size_t nTLBs = 0;
> auto push = [&]( bool l2, bool code, bool _4k, bool _2M4M,
> bool _1G, unsigned n, unsigned ways, bool update )
> {
> if( !n || !ways )
> return;
> ++nTLBs;
> if( update )
> tlbs.emplace_back( (uint8_t)l2 + 1, !code ?
> tlb_descr::type_t::DATA : tlb_descr::type_t::CODE, _4k, _2M4M, _2M4M,
> _1G, n, ways );
> };
> static auto longWays = []( uint8_t ways ) -> unsigned {
> return ways != 0xFF ? ways : -1; };
> static unsigned const shortWays[0x10] = { 0, 1, 2, 0, 4, 0,
> 8, 0, 16, 0, 32, 48, 64, 96, 128, (unsigned)-1 };
> cpuid( regs, 0x80000005u, 0 );
> push( false, false, true, false, false, regs[1] >> 16 &
> 0x0FF, longWays( regs[1] >> 24 ), update );
> push( false, false, false, true, false, regs[0] >> 16 &
> 0x0FF, longWays( regs[0] >> 24 ), update );
> push( false, true, true, false, false, regs[1] & 0xFF,
> longWays( regs[1] >> 8 & 0xFF ), update );
> push( false, true, false, true, false, regs[0] & 0xFF,
> longWays( regs[0] >> 8 & 0xFF ), update );
> cpuid( regs, 0x80000006u, 0 );
> push( true, false, true, false, false, regs[1] >> 16 &
> 0x0FFF, shortWays[regs[1] >> 28], update );
> push( true, false, false, true, false, regs[0] >> 16 &
> 0x0FFF, shortWays[regs[0] >> 28], update );
> push( true, true, true, false, false, regs[1] & 0xFFF,
> shortWays[regs[1] >> 12 & 0xF], update );
> push( true, true, false, true, false, regs[0] & 0xFFF,
> shortWays[regs[0] >> 12 & 0xF], update );
> if( has1GPages() && maxExtCpuid >= 0x80000019u )
> cpuid( regs, 0x80000019u, 0 ),
> push( false, false, false, false, true, regs[0] >> 16 &
> 0xFFF, shortWays[regs[0] >> 28], update ),
> push( false, true, false, false, true, regs[0] & 0xFFF,
> shortWays[regs[1] >> 12 & 0xF], update ),
> push( true, false, false, false, true, regs[1] >> 16 &
> 0xFFF, shortWays[regs[0] >> 28], update ),
> push( true, true, false, false, true, regs[1] & 0xFFF,
> shortWays[regs[1] >> 12 & 0xF], update );
> return nTLBs;
> };
> tlbs.reserve( gatherTLBs( false ) );
> gatherTLBs( true );
>
> This function collects the TLB-sizes for AMD-CPUs. In one mode it just
> counts the number of entries (update = false), in th other it fills the
> vector tlbs. That prevents any reallocation of tlbs.
> And the push lambda prevents a huge amount of redundant code.
Which bit is the lambda? If I create a more compact version so that I
can see the whole thing more easily:
auto gatherTLBs = [&](bool update ) -> size_t
{
size_t nTLBs = 0;
auto push = [&](bool l2, code, _4k, _2M4M, _1G, unsigned n, ways,
bool update )
{
if( !n || !ways )
return;
++nTLBs;
if( update )
tlbs.emplace_back( (uint8_t)l2 + 1, !code ? DATA : CODE,
_4k, _2M4M, _2M4M, _1G, n, ways );
};
static auto longWays = []( uint8_t ways ) -> unsigned { return ways
!= 0xFF ? ways : -1; };
static unsigned shortWays[0x10] = { 0, 1, 2, 0, 4, 0, 8, 0, 16,
0, 32, 48, 64, 96, 128, (unsigned)-1 };
cpuid( regs, 0x80000005u, 0 );
push( F, F, T, F, F, regs[1] >> 16 & 0x0FF, longWays( regs[1] >>
24 ), update );
push( F, F, F, T, F, regs[0] >> 16 & 0x0FF, longWays( regs[0] >>
24 ), update );
push( F, T, T, F, F, regs[1] & 0xFF, longWays( regs[1] >> 8 &
0xFF ), update );
push( F, T, F, T, F, regs[0] & 0xFF, longWays( regs[0] >> 8 &
0xFF ), update );
cpuid( regs, 0x80000006u, 0 );
push( T, F, T, F, F, regs[1] >> 16 & 0x0FFF, shortWays[regs[1] >>
28], update );
push( T, F, F, T, F, regs[0] >> 16 & 0x0FFF, shortWays[regs[0] >>
28], update );
push( T, T, T, F, F, regs[1] & 0xFFF, shortWays[regs[1] >> 12 &
0xF], update );
push( T, T, F, T, F, regs[0] & 0xFFF, shortWays[regs[0] >> 12 &
0xF], update );
if( has1GPages() && maxExtCpuid >= 0x80000019u )
cpuid( regs, 0x80000019u, 0 ),
push( F, F, F, F, T, regs[0] >> 16 & 0xFFF, shortWays[regs[0]
>> 28], update ),
push( F, T, F, F, T, regs[0] & 0xFFF, shortWays[regs[1] >> 12
& 0xF], update ),
push( T, F, F, F, T, regs[1] >> 16 & 0xFFF, shortWays[regs[0]
>> 28], update ),
push( T, T, F, F, T, regs[1] & 0xFFF, shortWays[regs[1] >> 12
& 0xF], update );
return nTLBs;
};
tlbs.reserve( gatherTLBs( F ) );
gatherTLBs( T );
then push() just looks like an ordinary local function.
What could be significantly improved are all those bitfield ops. Ideally
you would use named bitfields within what is presumably a 32-bit
unsigned value, instead of all those mysterious shifts and masks.
Those 0xFFF masks mixed with 16-bit shifts look suspicious until you
realise these are 12+4-bit fields in each half of a 32-bit value.
Here even a simple GETBITS macro would improve both readability and
confidence that the code is correct.
I don't do named bitfields outside of a struct [in my languages], but at
least I can manage this:
macro F1 = 0..11
macro F2 = 12..15
macro F3 = 16..27
macro F4 = 28..31
push(F,F,F,F,T, regs[0].[F3], shortways[regs[1].F4)
Better if I knew what those fields were for then I could give proper names.
So, it looks like your emphasis is different aspects: those elusive
lambdas (which doesn't help readability at all, unless it would be even
worse without them), rather than doing something about basic readability.
I'd also have aliases for regs[0] and regs[1], such as R0 and R1 (after
all you write assembly using R0 not REGS[0]).
Those first 5 Bool parameters for push also look like they are ripe for
conversion to a single flag parameter containing 5 single-bit fields
(Just Or-ing named bit-masks would be better.)
Remember your original looked like this:
push( false, false, true, false, false, ...
What do each of these signify? At least, if using multiple parameters,
use keyword parameters combined with a default value, such as false,
then this example can become:
push(..., FlagA:true)
(Keyword parameters go after positional ones.) Again I don't know what
these mean so can't give a more useful name.
(Does C++ have keyword parameters? It looks like it doesn't.)
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Book or tutorial on standard C threads Mehdi Amini <atorrses@gmail.com> - 2021-12-11 10:06 +0330
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-11 08:29 +0100
Re: Book or tutorial on standard C threads gazelle@shell.xmission.com (Kenny McCormack) - 2021-12-13 08:12 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-14 17:41 +0100
Re: Book or tutorial on standard C threads Thiago Adams <thiago.adams@gmail.com> - 2021-12-13 09:16 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-14 17:47 +0100
Re: Book or tutorial on standard C threads scott@slp53.sl.home (Scott Lurndal) - 2021-12-14 17:13 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-14 18:43 +0100
Re: Book or tutorial on standard C threads Guillaume <message@bottle.org> - 2021-12-14 19:23 +0100
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-14 20:16 +0100
Re: Book or tutorial on standard C threads Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-15 04:02 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-15 17:53 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-15 17:26 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-15 18:30 +0100
Re: Book or tutorial on standard C threads scott@slp53.sl.home (Scott Lurndal) - 2021-12-15 17:49 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-15 19:19 +0100
Re: Book or tutorial on standard C threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-15 17:44 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-15 20:23 +0100
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-16 07:54 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-16 14:16 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-16 15:49 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-16 15:57 +0000
Re: Book or tutorial on standard C threads Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 17:03 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-16 20:08 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-16 20:48 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-17 07:19 +0100
Re: Book or tutorial on standard C threads David Brown <david.brown@hesbynett.no> - 2021-12-16 17:24 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-16 18:00 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-16 20:06 +0100
Re: Book or tutorial on standard C threads David Brown <david.brown@hesbynett.no> - 2021-12-16 20:14 +0100
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-17 09:30 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-17 19:06 +0100
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-18 03:07 -0800
Re: Book or tutorial on standard C threads David Brown <david.brown@hesbynett.no> - 2021-12-18 18:33 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-17 18:19 +0000
Re: Book or tutorial on standard C threads David Brown <david.brown@hesbynett.no> - 2021-12-18 18:45 +0100
Re: Book or tutorial on standard C threads Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:45 +0000
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-16 23:02 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-18 13:45 +0100
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-18 06:02 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-18 15:31 +0100
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-18 07:12 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-18 16:45 +0100
Re: Book or tutorial on standard C threads Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-18 23:38 +0000
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-19 10:40 -0800
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-19 19:07 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-19 20:17 +0100
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-19 12:41 -0800
Re: Book or tutorial on standard C threads Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-20 03:21 -0800
Re: Book or tutorial on standard C threads Öö Tiib <ootiib@hot.ee> - 2021-12-20 09:39 -0800
Re: Book or tutorial on standard C threads scott@slp53.sl.home (Scott Lurndal) - 2021-12-16 18:32 +0000
Re: Book or tutorial on standard C threads David Brown <david.brown@hesbynett.no> - 2021-12-16 20:27 +0100
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-16 20:33 +0100
Re: Book or tutorial on standard C threads Bart <bc@freeuk.com> - 2021-12-17 10:59 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-17 13:08 +0100
Re: Book or tutorial on standard C threads scott@slp53.sl.home (Scott Lurndal) - 2021-12-17 16:25 +0000
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-17 17:38 +0100
Re: Book or tutorial on standard C threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-16 16:03 -0800
Re: Book or tutorial on standard C threads Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-17 00:12 +0000
Re: Book or tutorial on standard C threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-16 17:05 -0800
Re: Book or tutorial on standard C threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-16 17:07 -0800
Re: Book or tutorial on standard C threads "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-19 17:23 -0800
Re: Book or tutorial on standard C threads Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-20 07:35 +0100
csiph-web