|
|
Subscribe / Log in / New account

An end to implicit fall-throughs in the kernel

An end to implicit fall-throughs in the kernel

Posted Nov 24, 2019 22:06 UTC (Sun) by ballombe (subscriber, #9523)
In reply to: An end to implicit fall-throughs in the kernel by mathstuf
Parent article: An end to implicit fall-throughs in the kernel

Handling a data structure with variable length.
switch (strlen(s)-1)
{
case 3: a[3] = cvt3(s[3]);
case 2: a[2] = cvt2(s[2]);
case 1: a[1] = cvt1(s[1]);
case 0: a[0] = cvt0(s[0]);
}
or
switch(a.dim)
{
case 4: checkt(a.t);
case 3: checkz(a.z);
case 2: checky(a.y);
case 1: checkx(a.x);
}

finishing after a loop unrolling

for(i=l;i>=3;i-=4)
{ f(i); f(i-1);f(i-2);f(i-3);}
switch(i)
{
case 3: f(3);
case 2: f(2);
case 1: f(1);
case 0: f(0);
}

conversion
switch(type(z))
{
case FOO: z = FOO_to_BAR(z);
case BAR: z = BAR_to_BAZ(z);
case BAZ: return process_BAZ(z);
}


to post comments

An end to implicit fall-throughs in the kernel

Posted Nov 25, 2019 16:35 UTC (Mon) by mathstuf (subscriber, #69389) [Link]

> Handling a data structure with variable length.

The first seems like abusing a C string for data storage. A better data structure seems relevant here. It would certainly make the code clearer.

> a.dim

I feel like this would be wrapped up better in a `checkvec` function in the first place. Though this is C; why is one abusing a vec4 to store vec3 or vec2 information (losing cache line benefits given the usual suspects for code using such structures).

> loop unrolling

Something I expect the compiler to be better at today than myself (pending benchmarks showing that the vectorization/unrolling isn't happening).

> conversion

Well, this is what you get with crappy type hierarchies :) . I think the normal way would be something like:

baz* p = NULL;
switch(type(z)) // Assuming `z` is some kind of tagged union structure.
{
case FOO: p = &z->foo.baz; break;
case BAR: p = &z->bar.baz; break;
case BAZ: p = &z->baz;
}
if (p) process_BAZ(p);

These are interesting use cases, but I still wouldn't classify them as "common" or even idiomatic.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds