Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #161815

Re: naked switches

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: naked switches
Date 2021-07-10 13:28 +0200
Organization A noiseless patient Spider
Message-ID <scc085$6h2$1@dont-email.me> (permalink)
References (5 earlier) <7e094b82-01be-4d7f-9793-4eeda725ca0en@googlegroups.com> <20210708103653.702@kylheku.com> <9a999021-6733-4d00-91fb-9797570a1328n@googlegroups.com> <sc9190$h4g$1@dont-email.me> <sc9gav$iln$1@dont-email.me>

Show all headers | View raw


On 09/07/2021 14:44, Bart wrote:
> On 09/07/2021 09:27, David Brown wrote:
>> On 09/07/2021 00:45, Robert Finch wrote:
>>> On Thursday, July 8, 2021 at 1:47:12 PM UTC-4, Kaz Kylheku wrote:
>>>> 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.
>>>
>>> One should not have to write macros for commonly used language elements.
>>
>> Your extension 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.
>>
>> Having said that, it is not at all unreasonable for an embedded compiler
>> to ship with extra headers, which would be an excellent place to put
>> macros like this for user convenience.  It would be simpler and clearer
>> to C programmers who are not used to your extensions.  This is the
>> technique used by other embedded compilers.
>>
>> I'm not saying the extension is not useful, or bad in itself - but I
>> dislike adding extensions when it is perfectly possible to get the same
>> results without changing the language.
> 
> Then there would have been no need for the [] indexing syntax either.
> You'd just write:
> 
>   GETINDEX(a, i)
>   SETINDEX(a, i, x)
> 
> instead of a[i] or a[i]=x. Except these need special headers to be
> dragged in, and everyone would be using their own incompatible macros;
> try mixing code from different sources.

In a programming language, you want convenient syntax for things you do
a lot, and you can happily have inconvenient syntax for things you only
need to do occasionally.  (This works the other way too - you pick a
language that has convenient syntax for the things you do often.  Or at
least, that's what sensible programmers do.  Some people prefer to pick
a language they don't like and complain instead.)

Accessing bit-fields like this is very rare - even in embedded
programming, it is not something you do so much that existing C syntax
is a problem.  Accessing arrays, however, is massively useful.


> 
>> You need very good reasons for
>> adding non-portable extensions.
> 
> Such a set of macros as you propose would also be non-portable unless
> you included custom headers. And then you mix code that needs
> BIT(a,10,1) with code that needs BIT(a,1,10).

Yes, but the great thing about headers with simple macros is that they
are extremely portable.

> 
> You don't however seem to mind using GNU C extensions!

No, I don't - but I generally don't need my code to be portable beyond
gcc and occasionally a few older embedded compilers.

There are two C and C++ toolchains that totally dominate - MSVC on
Windows, and gcc on everything else.  The next most popular toolchains
are clang then probably icc, both of which support a fair number of gcc
extensions.

So a gcc extension is likely to be portable to perhaps 90% of all
non-Windows C or C++ development work.  That's more than good enough for
my needs.

An extension in /your/ tool, or Robert Finch's tool, is portable to
perhaps 0.0000000001% of C or C++ development work.  That is rather
different.

Of course if you are writing code that only ever needs to run on these
home-made tools, then use your extensions - you made them, so use them.


Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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