Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

We have phasers, I vote we blast 'em! -- Bailey, "The Corbomite Maneuver", stardate 1514.2


devel / comp.lang.c / Re: you think rust may outthrone c?

SubjectAuthor
* you think rust may outthrone c?fir
`* Re: you think rust may outthrone c?Blue-Maned_Hawk
 +- Re: you think rust may outthrone c?jak
 +* Re: you think rust may outthrone c?fir
 |`- Re: you think rust may outthrone c?fir
 +* Re: you think rust may outthrone c?rek2 hispagatos
 |`* Re: you think rust may outthrone c?Kaz Kylheku
 | `* Re: you think rust may outthrone c?Kalevi Kolttonen
 |  +* Re: you think rust may outthrone c?Scott Lurndal
 |  |`* Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | +- Re: you think rust may outthrone c?Kaz Kylheku
 |  | +* Re: you think rust may outthrone c?Bart
 |  | |`* Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | | +* Re: you think rust may outthrone c?Bart
 |  | | |+* Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | | ||`* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | || `- Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | | |`* Re: you think rust may outthrone c?David Brown
 |  | | | `* Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | | |  +* Re: you think rust may outthrone c?David Brown
 |  | | |  |`* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |  | `- Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | | |  `* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |   `* Re: you think rust may outthrone c?Kalevi Kolttonen
 |  | | |    `* Re: you think rust may outthrone c?David Brown
 |  | | |     +* Re: you think rust may outthrone c?Scott Lurndal
 |  | | |     |`- Re: you think rust may outthrone c?Keith Thompson
 |  | | |     `* Re: you think rust may outthrone c?Keith Thompson
 |  | | |      +- Re: you think rust may outthrone c?David Brown
 |  | | |      `* Re: you think rust may outthrone c?Malcolm McLean
 |  | | |       `* Re: you think rust may outthrone c?David Brown
 |  | | |        `* Re: you think rust may outthrone c?Malcolm McLean
 |  | | |         `* Re: you think rust may outthrone c?David Brown
 |  | | |          +- Re: you think rust may outthrone c?fir
 |  | | |          +* Re: you think rust may outthrone c?fir
 |  | | |          |`* Re: you think rust may outthrone c?fir
 |  | | |          | `- Re: you think rust may outthrone c?fir
 |  | | |          `* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |           `* Re: you think rust may outthrone c?Bart
 |  | | |            +* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |            |`* Re: you think rust may outthrone c?Malcolm McLean
 |  | | |            | +* Re: you think rust may outthrone c?David Brown
 |  | | |            | |+* Re: you think rust may outthrone c?Malcolm McLean
 |  | | |            | ||+* Re: you think rust may outthrone c?David Brown
 |  | | |            | |||+* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |            | ||||`* Re: you think rust may outthrone c?Tim Rentsch
 |  | | |            | |||| `- Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |            | |||`* Re: you think rust may outthrone c?Bart
 |  | | |            | ||| +- Re: you think rust may outthrone c?Malcolm McLean
 |  | | |            | ||| `- Re: you think rust may outthrone c?David Brown
 |  | | |            | ||`- Re: you think rust may outthrone c?Scott Lurndal
 |  | | |            | |`* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |            | | +- Re: you think rust may outthrone c?David Brown
 |  | | |            | | `* Re: you think rust may outthrone c?jak
 |  | | |            | |  `* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |            | |   +- Re: you think rust may outthrone c?jak
 |  | | |            | |   `- Re: you think rust may outthrone c?Tim Rentsch
 |  | | |            | `* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |            |  `* Re: you think rust may outthrone c?Malcolm McLean
 |  | | |            |   +- Re: you think rust may outthrone c?David Brown
 |  | | |            |   `- Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |            `* Re: you think rust may outthrone c?David Brown
 |  | | |             `* Re: you think rust may outthrone c?fir
 |  | | |              +* Re: you think rust may outthrone c?Chris M. Thomasson
 |  | | |              |`* Re: you think rust may outthrone c?David Brown
 |  | | |              | +* Re: you think rust may outthrone c?Chris M. Thomasson
 |  | | |              | |+* Re: you think rust may outthrone c?Chris M. Thomasson
 |  | | |              | ||+- Re: you think rust may outthrone c?Chris M. Thomasson
 |  | | |              | ||+* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |              | |||`- Re: you think rust may outthrone c?Chris M. Thomasson
 |  | | |              | ||`- Re: you think rust may outthrone c?fir
 |  | | |              | |`- Re: you think rust may outthrone c?Bart
 |  | | |              | `- Re: you think rust may outthrone c?Chris M. Thomasson
 |  | | |              `* Re: you think rust may outthrone c?fir
 |  | | |               `* Re: you think rust may outthrone c?Bart
 |  | | |                +* Re: you think rust may outthrone c?Keith Thompson
 |  | | |                |+* Re: you think rust may outthrone c?Bart
 |  | | |                ||+* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                |||`* Re: you think rust may outthrone c?Bart
 |  | | |                ||| +- Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |                ||| +* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| |+* Re: you think rust may outthrone c?David Brown
 |  | | |                ||| ||+- Re: you think rust may outthrone c?Bart
 |  | | |                ||| ||`* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| || `* Re: you think rust may outthrone c?David Brown
 |  | | |                ||| ||  `* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| ||   +* Re: you think rust may outthrone c?David Brown
 |  | | |                ||| ||   |`* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| ||   | `* Re: you think rust may outthrone c?David Brown
 |  | | |                ||| ||   |  `- Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| ||   `* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |                ||| ||    `- Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| |+* Re: you think rust may outthrone c?Bart
 |  | | |                ||| ||+* Re: you think rust may outthrone c?Ike Naar
 |  | | |                ||| |||`- Re: you think rust may outthrone c?Bart
 |  | | |                ||| ||+* Re: you think rust may outthrone c?David Brown
 |  | | |                ||| |||`* Re: you think rust may outthrone c?Bart
 |  | | |                ||| ||| `- Re: you think rust may outthrone c?David Brown
 |  | | |                ||| ||`* Re: you think rust may outthrone c?Ben Bacarisse
 |  | | |                ||| || +- Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |                ||| || +* Re: you think rust may outthrone c?Malcolm McLean
 |  | | |                ||| || `* Re: you think rust may outthrone c?Bart
 |  | | |                ||| |`* Re: you think rust may outthrone c?Scott Lurndal
 |  | | |                ||| `* Re: you think rust may outthrone c?Keith Thompson
 |  | | |                ||`- Re: you think rust may outthrone c?Keith Thompson
 |  | | |                |`* Re: you think rust may outthrone c?Kaz Kylheku
 |  | | |                `* Re: you think rust may outthrone c?fir
 |  | | +* Yeah, C is harder than many programming languages. Your point? (Was: you think Kenny McCormack
 |  | | +* Re: you think rust may outthrone c?Po Lu
 |  | | `* Re: you think rust may outthrone c?Kaz Kylheku
 |  | +* Re: you think rust may outthrone c?David Brown
 |  | `- Re: you think rust may outthrone c?Po Lu
 |  `* Re: you think rust may outthrone c?Anton Shepelev
 `* Re: you think rust may outthrone c?Bonita Montero

Pages:123456789101112131415161718192021222324252627282930313233343536373839
Re: you think rust may outthrone c?

<u8u4vf$cuu1$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kal...@kolttonen.fi (Kalevi Kolttonen)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 12:55:11 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 19
Sender: <untosten@0.0.0.0>
Message-ID: <u8u4vf$cuu1$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com> <20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me> <s3ma5vyveo9.fsf@yahoo.com> <u8rhrs$1b2m$1@dont-email.me> <20230714140259.603@kylheku.com> <u8si9j$53qt$1@dont-email.me> <875y6myurr.fsf@nosuchdomain.example.com> <u8sjqj$57ab$1@dont-email.me> <u8u45v$cscl$2@dont-email.me>
Injection-Date: Sat, 15 Jul 2023 12:55:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="df074145f7c9c9b70247792428e73095";
logging-data="424897"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9KIjg2xBDo52AZ7vIA/TJ9CTb1gNHzp8="
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.3.12-200.fc38.x86_64 (x86_64))
Cancel-Lock: sha1:3+QljGt4wZ84Dka/a+DbuqBV9TA=
 by: Kalevi Kolttonen - Sat, 15 Jul 2023 12:55 UTC

David Brown <david.brown@hesbynett.no> wrote:
> So extensions to C are undefined behaviour in respect to the C standard,
> but defined behaviour in respect to the implementation. As far as the
> standard is concerned, and therefore for the C language in general,
> signed integer overflow is undefined behaviour. However, if you are
> coding for "gcc -fwrapv", then the behaviour is fully defined by the
> implementation.

Right, and that is precisely what I did not realize before. My
simplistic "understanding" was that UB is always something that
will produce results that are, well, not guaranteed to be one
way or the other.

Relaying on Implementation Defined behaviour can of course
be perfectly acceptable as long as you do it consciously and
document it somehow.

br,
KK

Re: you think rust may outthrone c?

<uWzsM.125911$OwWc.102751@fx45.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx45.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: you think rust may outthrone c?
Newsgroups: comp.lang.c
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com> <u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me> <20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me> <u8u3ge$cscl$1@dont-email.me>
Lines: 33
Message-ID: <uWzsM.125911$OwWc.102751@fx45.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 15 Jul 2023 16:36:42 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 15 Jul 2023 16:36:42 GMT
X-Received-Bytes: 2570
 by: Scott Lurndal - Sat, 15 Jul 2023 16:36 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 14/07/2023 23:35, Kalevi Kolttonen wrote:
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> If you don't qualify "portable" somehow, like by listing the kinds
>>> of platforms supported, then it means maximally portable.
>>
>> I see. Is the meaning more formally defined somewhere in an
>> exact way? Or is it just common knowledge?
>>
>> My programming skills are so poor that I am 100% convinced I
>> could not write maximally portable C code. I have too many
>> incorrect assumptions and would be sure to rely on e.g. some
>> Implementation Defined behavior. This is no joke but that's
>> just the way it is. I do not blame C for this - it is entirely
>> my fault.
>>
>>> You open yourself to being roasted for the code assuming ASCII,
>>> 8 bit chars, pointers being all equal sized and all that.
>
>These things should not be a problem. Keep your pointers as pointers of
>compatible type - there is rarely a good reason for wanting to convert a
>pointer-to-int to a pointer-to-float, and certainly not for mixing data
>pointers and function pointers. Think about what your pointers are
>pointing at, and use that as the basis for types. And if you ever think
>you need to mess with pointer types, you are probably better using
>memcpy/memmove.
>
>Use "char" to hold characters, and never use it for anything else, then
>its size doesn't matter, nor does the character encoding.

The one gotcha in that is that getch() et al return an int, so they
can represent EOF.

Re: you think rust may outthrone c?

<u8ui58$ediv$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 18:40:07 +0200
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <u8ui58$ediv$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<s3ma5vyveo9.fsf@yahoo.com> <u8rhrs$1b2m$1@dont-email.me>
<u8rpuu$24m6$3@dont-email.me> <u8rsjl$2i8m$1@dont-email.me>
<u8rvj6$31dr$1@dont-email.me> <u8s0fc$33sa$2@dont-email.me>
<1e547403-da8a-45ce-a178-79a8f62c1569n@googlegroups.com>
<u8u3ff$cr1u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jul 2023 16:40:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e7b27cc64bddfd10e9658095efcdba7b";
logging-data="472671"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/G71okWN8TkIJhyd26gdbIbHHsl1HkI9A="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:yD83hYm5JT8HCtESJnfRUPWHMjk=
In-Reply-To: <u8u3ff$cr1u$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 15 Jul 2023 16:40 UTC

On 15/07/2023 14:29, Kalevi Kolttonen wrote:
> Paul N <gw7rib@aol.com> wrote:
>> I think you could get such a bug if you assume that
>> the letters of the alphabet all have consecutive values.
>> It's true of many computers today, but was not true of
>> the mainframe when I was at university, which used
>> EBCDIC instead of ASCII. As I understand it, this is
>> not "Implementation Defined behaviour" because there is
>> no requirement to say what values the letters have.
>
> I see, thanks. That would satisfy the requirements I gave.
>
> However, saying "just like other bugs" somehow seems to
> give the impression that there would be many bugs that
> could satisfy the requirements.
>
> Finding a single example is a very good thing, but it
> would be more convincing if there were more examples.
>

A "bug" is just code not doing what you want it to do. Some kinds of
bugs have classifications like "integer overflow" that are clearly
"undefined behaviour" in C standard terms. They may or may not be
undefined behaviour for a given C implementation, and can behave
differently between implementations. Some are coding bugs that are not
C UB - maybe you have a pointer to an int, but sometimes it points to
the wrong int. And some are specification bugs.

So if you have a specification that says "make the file path by
concatenating the directory name and file name with a backslash
character", you have a specification bug that affects *nix systems but
not Windows systems - and no C language UB in sight.

Or to give you a different example, one embedded development tool I used
(long ago) worked fine on my older computers, then crashed on newer
ones. It turned out that a timing loop overflowed (or had a divide by
zero - I forget which) on processors faster than about 200 MHz. There
was no UB in the code when running on older systems, but it failed on
newer ones.

Any time you have UB in your code - general UB, meaning the behaviour is
not defined anywhere - you don't know what will happen. The C standard
did attempt to distinguish between "bounded undefined behaviour" and
"critical undefined behaviour", but since "bounded undefined behaviour"
can have knock-on effects leading to "critical undefined behaviour", the
distinction is moot. The result could be all kinds of odd things - and
one of the possibilities is always "it works as expected".

The software world is full of examples of "it worked when /I/ tested it"
bugs. Some of these will be caused by C standard UB bugs, but others
are bugs that do not have any UB at the C level.

How you pick your examples of bugs that are not "UB" will depend on
exactly what you are meaning by "UB" at the time - IMHO, the most
general definition of "UB" is synonymous with a "bug" because the code
is doing something other than what was defined in the specification for
the code.

Re: you think rust may outthrone c?

<u8uihh$ediv$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 18:46:41 +0200
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <u8uihh$ediv$2@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<s3ma5vyveo9.fsf@yahoo.com> <u8rhrs$1b2m$1@dont-email.me>
<20230714140259.603@kylheku.com> <u8si9j$53qt$1@dont-email.me>
<875y6myurr.fsf@nosuchdomain.example.com> <u8sjqj$57ab$1@dont-email.me>
<u8u45v$cscl$2@dont-email.me> <u8u4vf$cuu1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jul 2023 16:46:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e7b27cc64bddfd10e9658095efcdba7b";
logging-data="472671"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7ZoSqfU0xicFbZgwurOH/k4bTn/iZ5EA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:0CWS57kJbeP9ybu0qstpDUSxXwc=
In-Reply-To: <u8u4vf$cuu1$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 15 Jul 2023 16:46 UTC

On 15/07/2023 14:55, Kalevi Kolttonen wrote:
> David Brown <david.brown@hesbynett.no> wrote:
>> So extensions to C are undefined behaviour in respect to the C standard,
>> but defined behaviour in respect to the implementation. As far as the
>> standard is concerned, and therefore for the C language in general,
>> signed integer overflow is undefined behaviour. However, if you are
>> coding for "gcc -fwrapv", then the behaviour is fully defined by the
>> implementation.
>
> Right, and that is precisely what I did not realize before. My
> simplistic "understanding" was that UB is always something that
> will produce results that are, well, not guaranteed to be one
> way or the other.

That definition is correct. The only question is about where the
guarantees come from. The C standard may not guarantee what happens if
you try to dereference a null pointer, but perhaps the OS you are
running on does - then it is UB as far as the C standard is concerned,
but not UB for your target platform. (A compiler for that OS may still
view it as UB for error checking and/or optimisation purposes even if
the OS defines the behaviour.)

>
> Relaying on Implementation Defined behaviour can of course
> be perfectly acceptable as long as you do it consciously and
> document it somehow.
>

Yes.

Often the requirement documentation can be implicit, if it is reasonable
enough. For example, if you want to have an 8-bit unsigned integer,
just use the type "uint8_t" - the code will fail to compile if used on
an implementation that does not have 8-bit types. But if you have some
needs that are less common, make them explicit - and, if possible, have
compile-time checks (static assertions or pre-processor checks).

Re: you think rust may outthrone c?

<u8ul00$ekod$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kal...@kolttonen.fi (Kalevi Kolttonen)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 17:28:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 37
Sender: <untosten@0.0.0.0>
Message-ID: <u8ul00$ekod$2@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com> <UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me> <s3ma5vyveo9.fsf@yahoo.com> <u8rhrs$1b2m$1@dont-email.me> <20230714140259.603@kylheku.com> <u8si9j$53qt$1@dont-email.me> <875y6myurr.fsf@nosuchdomain.example.com> <u8sjqj$57ab$1@dont-email.me> <u8u45v$cscl$2@dont-email.me> <u8u4vf$cuu1$1@dont-email.me> <u8uihh$ediv$2@dont-email.me>
Injection-Date: Sat, 15 Jul 2023 17:28:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="df074145f7c9c9b70247792428e73095";
logging-data="480013"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fyA+b4QdnUgg/Jz2k7Ulai+U2ZAY2IqA="
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.3.12-200.fc38.x86_64 (x86_64))
Cancel-Lock: sha1:ne/AGnQycAvitoct0kdESnVWKdE=
 by: Kalevi Kolttonen - Sat, 15 Jul 2023 17:28 UTC

David Brown <david.brown@hesbynett.no> wrote:
> On 15/07/2023 14:55, Kalevi Kolttonen wrote:
>> David Brown <david.brown@hesbynett.no> wrote:
>>> So extensions to C are undefined behaviour in respect to the C standard,
>>> but defined behaviour in respect to the implementation. As far as the
>>> standard is concerned, and therefore for the C language in general,
>>> signed integer overflow is undefined behaviour. However, if you are
>>> coding for "gcc -fwrapv", then the behaviour is fully defined by the
>>> implementation.
>>
>> Right, and that is precisely what I did not realize before. My
>> simplistic "understanding" was that UB is always something that
>> will produce results that are, well, not guaranteed to be one
>> way or the other.
>
> That definition is correct. The only question is about where the
> guarantees come from. The C standard may not guarantee what happens if
> you try to dereference a null pointer, but perhaps the OS you are
> running on does - then it is UB as far as the C standard is concerned,
> but not UB for your target platform. (A compiler for that OS may still
> view it as UB for error checking and/or optimisation purposes even if
> the OS defines the behaviour.)

Yes, it is correct but *only if* you limit to what the C
standard says, but like you and others have said, if the
implementation cares about the issue, and provides some
kind of defined behavior, then my previous understanding
was wrong.

In other words, I did not know that the implementations
can "override" UB (i.e. UB according to C stardard) and
turn it into Implementation Defined behavior.

That kind of ignorance is not something to be proud of.

br,
KK

Re: you think rust may outthrone c?

<u8uo0p$evvo$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 20:20:08 +0200
Organization: A noiseless patient Spider
Lines: 90
Message-ID: <u8uo0p$evvo$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<s3ma5vyveo9.fsf@yahoo.com> <u8rhrs$1b2m$1@dont-email.me>
<20230714140259.603@kylheku.com> <u8si9j$53qt$1@dont-email.me>
<875y6myurr.fsf@nosuchdomain.example.com> <u8sjqj$57ab$1@dont-email.me>
<u8u45v$cscl$2@dont-email.me> <u8u4vf$cuu1$1@dont-email.me>
<u8uihh$ediv$2@dont-email.me> <u8ul00$ekod$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jul 2023 18:20:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="05485c2580c53c8075dacd3cba82860e";
logging-data="491512"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18G2isdoIv7Ad63YEOh6RBU1zZhH0jfhvw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:khRn4gVSC81YVg+10QW3bUa8Nec=
In-Reply-To: <u8ul00$ekod$2@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 15 Jul 2023 18:20 UTC

On 15/07/2023 19:28, Kalevi Kolttonen wrote:
> David Brown <david.brown@hesbynett.no> wrote:
>> On 15/07/2023 14:55, Kalevi Kolttonen wrote:
>>> David Brown <david.brown@hesbynett.no> wrote:
>>>> So extensions to C are undefined behaviour in respect to the C standard,
>>>> but defined behaviour in respect to the implementation. As far as the
>>>> standard is concerned, and therefore for the C language in general,
>>>> signed integer overflow is undefined behaviour. However, if you are
>>>> coding for "gcc -fwrapv", then the behaviour is fully defined by the
>>>> implementation.
>>>
>>> Right, and that is precisely what I did not realize before. My
>>> simplistic "understanding" was that UB is always something that
>>> will produce results that are, well, not guaranteed to be one
>>> way or the other.
>>
>> That definition is correct. The only question is about where the
>> guarantees come from. The C standard may not guarantee what happens if
>> you try to dereference a null pointer, but perhaps the OS you are
>> running on does - then it is UB as far as the C standard is concerned,
>> but not UB for your target platform. (A compiler for that OS may still
>> view it as UB for error checking and/or optimisation purposes even if
>> the OS defines the behaviour.)
>
> Yes, it is correct but *only if* you limit to what the C
> standard says,

No - you are missing the point.

If you are using "undefined behaviour" in the strict sense of the C
standards, then anything not defined by the C standards is UB, and if
you try to execute code that has UB, the standards do not give you any
guarantees about what might happen.

If you are using the term in a more general sense and considering
behaviours defined and guaranteed by the implementation, target, or
other documents, then you can safely and confidently execute code that
is defined by the implementation even if it is not defined by the C
standards. But anything whose behaviour is not defined by the
implementation and relevant documents, has no guarantee about its behaviour.

It's the same thing - it is merely a question of how specific and
detailed you want to be, and how restrictive you can be about the choice
of compiler and implementation.

> but like you and others have said, if the
> implementation cares about the issue,

(Note this is not an "issue" to care about - UB in a language is a good
thing, as long as you understand it and avoid trying to execute it.
Many languages gloss over UB and pretend they define everything, but
they don't - and often they give pointless and counter-productive
definitions to meaningless code simply so that they can claim to have no
UB. UB is really just "garbage in, garbage out".)

> and provides some
> kind of defined behavior, then my previous understanding
> was wrong.
>
> In other words, I did not know that the implementations
> can "override" UB (i.e. UB according to C stardard) and
> turn it into Implementation Defined behavior.
>

They can't turn it into "implementation-defined behaviour" - that has a
specific meaning in C terms. It means behaviour that the C standards
say can vary between implementations, but which the implementation must
document. Examples include the size of "int" and the character sets.

But anything that the C standard does not define, can be defined by an
implementation - the C standards say nothing about such things and do
not require them to be "wrong". (This is different from things like
"constraints" and syntax errors - compilers are obliged to follow these
in conforming modes. So a conforming compiler cannot have a "long long
long int" type, or implement a "%" operator on string literals. It can
do so in non-conforming modes, of course, where the compiler is free to
do anything it likes.)

> That kind of ignorance is not something to be proud of.
>

Ignorance is seldom something to be proud of - but it is not something
to be ashamed of either. You fix it by learning. And they say the
quickest way to get good answers on the internet is not to ask
questions, but to post the wrong answers - it has worked for me on many
occasions!

Re: you think rust may outthrone c?

<u8upba$f3n1$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kal...@kolttonen.fi (Kalevi Kolttonen)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 18:42:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 29
Sender: <untosten@0.0.0.0>
Message-ID: <u8upba$f3n1$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com> <u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me> <s3ma5vyveo9.fsf@yahoo.com> <u8rhrs$1b2m$1@dont-email.me> <20230714140259.603@kylheku.com> <u8si9j$53qt$1@dont-email.me> <875y6myurr.fsf@nosuchdomain.example.com> <u8sjqj$57ab$1@dont-email.me> <u8u45v$cscl$2@dont-email.me> <u8u4vf$cuu1$1@dont-email.me> <u8uihh$ediv$2@dont-email.me> <u8ul00$ekod$2@dont-email.me> <u8uo0p$evvo$1@dont-email.me>
Injection-Date: Sat, 15 Jul 2023 18:42:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="df074145f7c9c9b70247792428e73095";
logging-data="495329"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QQRGZ20V3sn+VyExzA39hyuIbwxMn5fM="
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.3.12-200.fc38.x86_64 (x86_64))
Cancel-Lock: sha1:cBfzHCIoVcB0a3lUHrEkRF264/0=
 by: Kalevi Kolttonen - Sat, 15 Jul 2023 18:42 UTC

David Brown <david.brown@hesbynett.no> wrote:
> They can't turn it into "implementation-defined behaviour" - that has a
> specific meaning in C terms. It means behaviour that the C standards
> say can vary between implementations, but which the implementation must
> document. Examples include the size of "int" and the character sets.

Thanks, yes, I used the wrong term.

> But anything that the C standard does not define, can be defined by an
> implementation -

I guess there is no existing term for that.

> Ignorance is seldom something to be proud of - but it is not something
> to be ashamed of either. You fix it by learning. And they say the
> quickest way to get good answers on the internet is not to ask
> questions, but to post the wrong answers - it has worked for me on many
> occasions!

That is true, but it is also the responsibility of all C
programmers to know the fundamental principles governing
how to use the C language properly.

I can tell you that it is not a great feeling to realize that
you have misunderstood something pretty fundamental for several
years.

br,
KK

Re: you think rust may outthrone c?

<87wmz0yemc.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 15:49:31 -0700
Organization: None to speak of
Lines: 17
Message-ID: <87wmz0yemc.fsf@nosuchdomain.example.com>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <uWzsM.125911$OwWc.102751@fx45.iad>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="5a40cfe1b2109fde655cd0f0fa9c2b71";
logging-data="550442"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FoaDLGG6yyMep4l7MOWml"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:KUKsFwsrQrRLcAnN+YsHcIkOwU4=
sha1:eM9TbXZ3VEgLx7kvnkptsbUJMG0=
 by: Keith Thompson - Sat, 15 Jul 2023 22:49 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
> David Brown <david.brown@hesbynett.no> writes:
[...]
>>Use "char" to hold characters, and never use it for anything else, then
>>its size doesn't matter, nor does the character encoding.
>
> The one gotcha in that is that getch() et al return an int, so they
> can represent EOF.

There is no getch() in standard C. There are functions of that name in
the obsolete <conio.h> and in [n]curses. You're thinking of getc(),
fgetc(), and getchar().

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: you think rust may outthrone c?

<87sf9oye05.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sat, 15 Jul 2023 16:02:50 -0700
Organization: None to speak of
Lines: 30
Message-ID: <87sf9oye05.fsf@nosuchdomain.example.com>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="5a40cfe1b2109fde655cd0f0fa9c2b71";
logging-data="554470"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uYx8Qc53RTbFYQeVTTE4N"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:Glinse0AQwIlHYn4kOgYdN72OxY=
sha1:mf3ii93bIhgOSGA57wnh2AYlnnY=
 by: Keith Thompson - Sat, 15 Jul 2023 23:02 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 14/07/2023 23:35, Kalevi Kolttonen wrote:
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
[...]
>>> You open yourself to being roasted for the code assuming ASCII,
>>> 8 bit chars, pointers being all equal sized and all that.
>
> These things should not be a problem. Keep your pointers as pointers
> of compatible type - there is rarely a good reason for wanting to
> convert a pointer-to-int to a pointer-to-float, and certainly not for
> mixing data pointers and function pointers. Think about what your
> pointers are pointing at, and use that as the basis for types. And if
> you ever think you need to mess with pointer types, you are probably
> better using memcpy/memmove.

Agreed -- but you can convert pointers without assuming that they're the
same size. For example, if void* is bigger than int*, a conversion from
int* to void* will still work correctly.

Converting between int* and float*, for example tends to be for the
purpose of aliasing int and float objects, and that's rarely useful
(particularly since int and float are not guaranteed to have the same
size).

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: you think rust may outthrone c?

<u8v9fi$gv7k$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sun, 16 Jul 2023 01:18:10 +0200
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <u8v9fi$gv7k$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jul 2023 23:18:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eefbedb6a890dddcd0a8b003618d0c96";
logging-data="556276"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cxVAqiQknIKDN+4UQYdmWhACxWn0QGug="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:VncVTD+zdRafctJBFijqbDzDSto=
Content-Language: en-GB
In-Reply-To: <87sf9oye05.fsf@nosuchdomain.example.com>
 by: David Brown - Sat, 15 Jul 2023 23:18 UTC

On 16/07/2023 01:02, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 14/07/2023 23:35, Kalevi Kolttonen wrote:
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
> [...]
>>>> You open yourself to being roasted for the code assuming ASCII,
>>>> 8 bit chars, pointers being all equal sized and all that.
>>
>> These things should not be a problem. Keep your pointers as pointers
>> of compatible type - there is rarely a good reason for wanting to
>> convert a pointer-to-int to a pointer-to-float, and certainly not for
>> mixing data pointers and function pointers. Think about what your
>> pointers are pointing at, and use that as the basis for types. And if
>> you ever think you need to mess with pointer types, you are probably
>> better using memcpy/memmove.
>
> Agreed -- but you can convert pointers without assuming that they're the
> same size. For example, if void* is bigger than int*, a conversion from
> int* to void* will still work correctly.
>

Yes, you can covert any data pointer to void*. But you have to convert
it back to pointer compatible to the original data before dereferencing
it - you can't expect "int x; float f = *(float *)(void *) &x;" to do
anything sensible. And it's also important to remember that void* is
not suitable for converting function pointers.

> Converting between int* and float*, for example tends to be for the
> purpose of aliasing int and float objects, and that's rarely useful
> (particularly since int and float are not guaranteed to have the same
> size).
>

Of course - you usually want a union if you need to alias different types.

Re: you think rust may outthrone c?

<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:6088:b0:403:e7aa:4bae with SMTP id hf8-20020a05622a608800b00403e7aa4baemr30279qtb.2.1689463505646;
Sat, 15 Jul 2023 16:25:05 -0700 (PDT)
X-Received: by 2002:a05:6808:192a:b0:3a4:1f43:c109 with SMTP id
bf42-20020a056808192a00b003a41f43c109mr11616823oib.8.1689463505376; Sat, 15
Jul 2023 16:25:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!3.us.feeder.erje.net!feeder.erje.net!border-1.nntp.ord.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: Sat, 15 Jul 2023 16:25:04 -0700 (PDT)
In-Reply-To: <87sf9oye05.fsf@nosuchdomain.example.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:20fc:58cb:980e:480c;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:20fc:58cb:980e:480c
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org> <u8p0kv$h9r$1@matrix.hispagatos.org>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sat, 15 Jul 2023 23:25:05 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 29
 by: Malcolm McLean - Sat, 15 Jul 2023 23:25 UTC

On Sunday, 16 July 2023 at 00:03:08 UTC+1, Keith Thompson wrote:
> David Brown <david...@hesbynett.no> writes:
> > On 14/07/2023 23:35, Kalevi Kolttonen wrote:
> >> Kaz Kylheku <864-11...@kylheku.com> wrote:
> [...]
> >>> You open yourself to being roasted for the code assuming ASCII,
> >>> 8 bit chars, pointers being all equal sized and all that.
> >
> > These things should not be a problem. Keep your pointers as pointers
> > of compatible type - there is rarely a good reason for wanting to
> > convert a pointer-to-int to a pointer-to-float, and certainly not for
> > mixing data pointers and function pointers. Think about what your
> > pointers are pointing at, and use that as the basis for types. And if
> > you ever think you need to mess with pointer types, you are probably
> > better using memcpy/memmove.
> Agreed -- but you can convert pointers without assuming that they're the
> same size. For example, if void* is bigger than int*, a conversion from
> int* to void* will still work correctly.
>
> Converting between int* and float*, for example tends to be for the
> purpose of aliasing int and float objects, and that's rarely useful
> (particularly since int and float are not guaranteed to have the same
> size).
>
It's a hacky, low level thing to do. And of course it's unlikely to be portable
to platforms on which int and float are of different sizes. And probably
not portable to platforms which don't use the particular float representation,
since the likely motive is to get access to the bits of the float directly. But
part of the power of C is that you can do these things if you wish, or if there
is some actual requirement for some reason.

Re: you think rust may outthrone c?

<u90c0s$nr2u$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sun, 16 Jul 2023 11:07:40 +0200
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <u90c0s$nr2u$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 16 Jul 2023 09:07:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eefbedb6a890dddcd0a8b003618d0c96";
logging-data="781406"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VyNL71Usu6brjCeqcQs9X4Mx2QmU7odY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:d/ySa0zPmkSF34TjLm6SUnUpeUk=
Content-Language: en-GB
In-Reply-To: <a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
 by: David Brown - Sun, 16 Jul 2023 09:07 UTC

On 16/07/2023 01:25, Malcolm McLean wrote:
> On Sunday, 16 July 2023 at 00:03:08 UTC+1, Keith Thompson wrote:
>> David Brown <david...@hesbynett.no> writes:
>>> On 14/07/2023 23:35, Kalevi Kolttonen wrote:
>>>> Kaz Kylheku <864-11...@kylheku.com> wrote:
>> [...]
>>>>> You open yourself to being roasted for the code assuming ASCII,
>>>>> 8 bit chars, pointers being all equal sized and all that.
>>>
>>> These things should not be a problem. Keep your pointers as pointers
>>> of compatible type - there is rarely a good reason for wanting to
>>> convert a pointer-to-int to a pointer-to-float, and certainly not for
>>> mixing data pointers and function pointers. Think about what your
>>> pointers are pointing at, and use that as the basis for types. And if
>>> you ever think you need to mess with pointer types, you are probably
>>> better using memcpy/memmove.
>> Agreed -- but you can convert pointers without assuming that they're the
>> same size. For example, if void* is bigger than int*, a conversion from
>> int* to void* will still work correctly.
>>
>> Converting between int* and float*, for example tends to be for the
>> purpose of aliasing int and float objects, and that's rarely useful
>> (particularly since int and float are not guaranteed to have the same
>> size).
>>
> It's a hacky, low level thing to do. And of course it's unlikely to be portable
> to platforms on which int and float are of different sizes. And probably
> not portable to platforms which don't use the particular float representation,
> since the likely motive is to get access to the bits of the float directly. But
> part of the power of C is that you can do these things if you wish, or if there
> is some actual requirement for some reason.

No, you /can't/ do that. It is a stupid thing to do, and you do people
a disservice by suggesting it is sometimes appropriate. Of course there
can be occasions where it is useful to see "inside" a floating point
value - /someone/ has to write software floating point libraries for
processors that can't handle them in hardware. But hopefully the people
that need to do that know more about C than you do, and are do it
safely, correctly, and efficiently instead of blind dangerous hacks that
are /not/ guaranteed to work in any useful fashion, even if "float" and
"int" are the same size on the target.

C lets you do all kinds of things - including lots of non-portable
things (it is intended to be usable as a systems programming language as
well as a portable applications programming language). But if you try
to place smartarse tricks, you need to know /exactly/ what you are doing.

Re: you think rust may outthrone c?

<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2a03:b0:765:9914:a338 with SMTP id o3-20020a05620a2a0300b007659914a338mr65064qkp.12.1689511351473;
Sun, 16 Jul 2023 05:42:31 -0700 (PDT)
X-Received: by 2002:a05:6808:2201:b0:3a2:214d:3da9 with SMTP id
bd1-20020a056808220100b003a2214d3da9mr13941407oib.10.1689511351245; Sun, 16
Jul 2023 05:42:31 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 16 Jul 2023 05:42:30 -0700 (PDT)
In-Reply-To: <u90c0s$nr2u$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:e8d2:5871:53f0:e6e5;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:e8d2:5871:53f0:e6e5
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org> <u8p0kv$h9r$1@matrix.hispagatos.org>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com> <u90c0s$nr2u$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sun, 16 Jul 2023 12:42:31 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4986
 by: Malcolm McLean - Sun, 16 Jul 2023 12:42 UTC

On Sunday, 16 July 2023 at 10:07:56 UTC+1, David Brown wrote:
> On 16/07/2023 01:25, Malcolm McLean wrote:
> > On Sunday, 16 July 2023 at 00:03:08 UTC+1, Keith Thompson wrote:
> >> David Brown <david...@hesbynett.no> writes:
> >>> On 14/07/2023 23:35, Kalevi Kolttonen wrote:
> >>>> Kaz Kylheku <864-11...@kylheku.com> wrote:
> >> [...]
> >>>>> You open yourself to being roasted for the code assuming ASCII,
> >>>>> 8 bit chars, pointers being all equal sized and all that.
> >>>
> >>> These things should not be a problem. Keep your pointers as pointers
> >>> of compatible type - there is rarely a good reason for wanting to
> >>> convert a pointer-to-int to a pointer-to-float, and certainly not for
> >>> mixing data pointers and function pointers. Think about what your
> >>> pointers are pointing at, and use that as the basis for types. And if
> >>> you ever think you need to mess with pointer types, you are probably
> >>> better using memcpy/memmove.
> >> Agreed -- but you can convert pointers without assuming that they're the
> >> same size. For example, if void* is bigger than int*, a conversion from
> >> int* to void* will still work correctly.
> >>
> >> Converting between int* and float*, for example tends to be for the
> >> purpose of aliasing int and float objects, and that's rarely useful
> >> (particularly since int and float are not guaranteed to have the same
> >> size).
> >>
> > It's a hacky, low level thing to do. And of course it's unlikely to be portable
> > to platforms on which int and float are of different sizes. And probably
> > not portable to platforms which don't use the particular float representation,
> > since the likely motive is to get access to the bits of the float directly. But
> > part of the power of C is that you can do these things if you wish, or if there
> > is some actual requirement for some reason.
> No, you /can't/ do that. It is a stupid thing to do, and you do people
> a disservice by suggesting it is sometimes appropriate. Of course there
> can be occasions where it is useful to see "inside" a floating point
> value - /someone/ has to write software floating point libraries for
> processors that can't handle them in hardware. But hopefully the people
> that need to do that know more about C than you do, and are do it
> safely, correctly, and efficiently instead of blind dangerous hacks that
> are /not/ guaranteed to work in any useful fashion, even if "float" and
> "int" are the same size on the target.
>
> C lets you do all kinds of things - including lots of non-portable
> things (it is intended to be usable as a systems programming language as
> well as a portable applications programming language). But if you try
> to place smartarse tricks, you need to know /exactly/ what you are doing.
>
No. C doesn't provide facilities for what you are suggesting. That doesn't mean that
you can't write bitwise operations on floats in C, without resorting to assembler.
But you can't do so "safely, correctly, efficiently" instead of "blind dangerous hacks".
And of course I do know how floating point arithmetic works and what I am doing.

Re: you think rust may outthrone c?

<u90u5r$pnad$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sun, 16 Jul 2023 16:17:30 +0200
Organization: A noiseless patient Spider
Lines: 105
Message-ID: <u90u5r$pnad$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 16 Jul 2023 14:17:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eefbedb6a890dddcd0a8b003618d0c96";
logging-data="843085"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Rd9kwhoqPfg9ht3mUU4Ni8hxztIKvHj4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:eHy/DNS0cTEzgU9iYIgRN+vtLTw=
Content-Language: en-GB
In-Reply-To: <0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
 by: David Brown - Sun, 16 Jul 2023 14:17 UTC

On 16/07/2023 14:42, Malcolm McLean wrote:
> On Sunday, 16 July 2023 at 10:07:56 UTC+1, David Brown wrote:
>> On 16/07/2023 01:25, Malcolm McLean wrote:
>>> On Sunday, 16 July 2023 at 00:03:08 UTC+1, Keith Thompson wrote:
>>>> David Brown <david...@hesbynett.no> writes:
>>>>> On 14/07/2023 23:35, Kalevi Kolttonen wrote:
>>>>>> Kaz Kylheku <864-11...@kylheku.com> wrote:
>>>> [...]
>>>>>>> You open yourself to being roasted for the code assuming ASCII,
>>>>>>> 8 bit chars, pointers being all equal sized and all that.
>>>>>
>>>>> These things should not be a problem. Keep your pointers as pointers
>>>>> of compatible type - there is rarely a good reason for wanting to
>>>>> convert a pointer-to-int to a pointer-to-float, and certainly not for
>>>>> mixing data pointers and function pointers. Think about what your
>>>>> pointers are pointing at, and use that as the basis for types. And if
>>>>> you ever think you need to mess with pointer types, you are probably
>>>>> better using memcpy/memmove.
>>>> Agreed -- but you can convert pointers without assuming that they're the
>>>> same size. For example, if void* is bigger than int*, a conversion from
>>>> int* to void* will still work correctly.
>>>>
>>>> Converting between int* and float*, for example tends to be for the
>>>> purpose of aliasing int and float objects, and that's rarely useful
>>>> (particularly since int and float are not guaranteed to have the same
>>>> size).
>>>>
>>> It's a hacky, low level thing to do. And of course it's unlikely to be portable
>>> to platforms on which int and float are of different sizes. And probably
>>> not portable to platforms which don't use the particular float representation,
>>> since the likely motive is to get access to the bits of the float directly. But
>>> part of the power of C is that you can do these things if you wish, or if there
>>> is some actual requirement for some reason.
>> No, you /can't/ do that. It is a stupid thing to do, and you do people
>> a disservice by suggesting it is sometimes appropriate. Of course there
>> can be occasions where it is useful to see "inside" a floating point
>> value - /someone/ has to write software floating point libraries for
>> processors that can't handle them in hardware. But hopefully the people
>> that need to do that know more about C than you do, and are do it
>> safely, correctly, and efficiently instead of blind dangerous hacks that
>> are /not/ guaranteed to work in any useful fashion, even if "float" and
>> "int" are the same size on the target.
>>
>> C lets you do all kinds of things - including lots of non-portable
>> things (it is intended to be usable as a systems programming language as
>> well as a portable applications programming language). But if you try
>> to place smartarse tricks, you need to know /exactly/ what you are doing.
>>
> No. C doesn't provide facilities for what you are suggesting. That doesn't mean that
> you can't write bitwise operations on floats in C, without resorting to assembler.
> But you can't do so "safely, correctly, efficiently" instead of "blind dangerous hacks".
> And of course I do know how floating point arithmetic works and what I am doing.

Look, it is /very/ simple.

Let's assume that "float" is 32-bit. You are not going to be doing any
kind of low-level bitwise operations on float unless it is a known
format, so I think this is a fine assumption. (I've made other obvious
non-portable assumptions too.) Maybe you are wanting to look at the
highest significant bit as a way of identifying the sign of the number.

/Your/ suggestion is that you might do something like:

inline bool sign_bit(float f) {
int * p = &f;
int i = *p;
return i & 0x80000000;
}

That is just /wrong/. It is wildly unsafe, and many compilers will give
you unexpected results in practice, when the code is inlined and
optimised. The C language makes it perfectly clear that trying to use
an int* to access a float object is undefined behaviour, and compilers
will assume that you would never do such a stupid thing, or if you did,
you don't care what happens.

Getting it /right/ is very simple. Two examples are :

inline bool sign_bit(float f) {
uint32_t u;
memcpy(&u, &f, 4);
return u & 0x80000000;
}

and

inline bool sign_bit(float f) {
union {
uint32_t u;
float f;
} u;
u.f = f;
return u.u & 0x80000000;
}

Both of these are valid C (the second one is undefined behaviour in C++,
if that matters to you) and both have fully defined behaviour.

Any compiler for which it makes sense to talk about generating efficient
code, can handle such code optimally.

There really is no excuse for messing around with accessing data through
incompatible lvalues like you suggest - correct, safe and reliable
methods are simple and efficient.

Re: you think rust may outthrone c?

<525f928e-6549-4c51-bae5-4a826a2df040n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:24c8:b0:762:4e8c:edc1 with SMTP id m8-20020a05620a24c800b007624e8cedc1mr64839qkn.4.1689518647587;
Sun, 16 Jul 2023 07:44:07 -0700 (PDT)
X-Received: by 2002:a9d:6a46:0:b0:6b9:3892:efc0 with SMTP id
h6-20020a9d6a46000000b006b93892efc0mr8156346otn.7.1689518647313; Sun, 16 Jul
2023 07:44:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 16 Jul 2023 07:44:06 -0700 (PDT)
In-Reply-To: <u90u5r$pnad$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.103; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.103
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org> <u8p0kv$h9r$1@matrix.hispagatos.org>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com> <u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com> <u90u5r$pnad$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <525f928e-6549-4c51-bae5-4a826a2df040n@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 16 Jul 2023 14:44:07 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 7642
 by: fir - Sun, 16 Jul 2023 14:44 UTC

niedziela, 16 lipca 2023 o 16:17:47 UTC+2 David Brown napisał(a):
> On 16/07/2023 14:42, Malcolm McLean wrote:
> > On Sunday, 16 July 2023 at 10:07:56 UTC+1, David Brown wrote:
> >> On 16/07/2023 01:25, Malcolm McLean wrote:
> >>> On Sunday, 16 July 2023 at 00:03:08 UTC+1, Keith Thompson wrote:
> >>>> David Brown <david...@hesbynett.no> writes:
> >>>>> On 14/07/2023 23:35, Kalevi Kolttonen wrote:
> >>>>>> Kaz Kylheku <864-11...@kylheku.com> wrote:
> >>>> [...]
> >>>>>>> You open yourself to being roasted for the code assuming ASCII,
> >>>>>>> 8 bit chars, pointers being all equal sized and all that.
> >>>>>
> >>>>> These things should not be a problem. Keep your pointers as pointers
> >>>>> of compatible type - there is rarely a good reason for wanting to
> >>>>> convert a pointer-to-int to a pointer-to-float, and certainly not for
> >>>>> mixing data pointers and function pointers. Think about what your
> >>>>> pointers are pointing at, and use that as the basis for types. And if
> >>>>> you ever think you need to mess with pointer types, you are probably
> >>>>> better using memcpy/memmove.
> >>>> Agreed -- but you can convert pointers without assuming that they're the
> >>>> same size. For example, if void* is bigger than int*, a conversion from
> >>>> int* to void* will still work correctly.
> >>>>
> >>>> Converting between int* and float*, for example tends to be for the
> >>>> purpose of aliasing int and float objects, and that's rarely useful
> >>>> (particularly since int and float are not guaranteed to have the same
> >>>> size).
> >>>>
> >>> It's a hacky, low level thing to do. And of course it's unlikely to be portable
> >>> to platforms on which int and float are of different sizes. And probably
> >>> not portable to platforms which don't use the particular float representation,
> >>> since the likely motive is to get access to the bits of the float directly. But
> >>> part of the power of C is that you can do these things if you wish, or if there
> >>> is some actual requirement for some reason.
> >> No, you /can't/ do that. It is a stupid thing to do, and you do people
> >> a disservice by suggesting it is sometimes appropriate. Of course there
> >> can be occasions where it is useful to see "inside" a floating point
> >> value - /someone/ has to write software floating point libraries for
> >> processors that can't handle them in hardware. But hopefully the people
> >> that need to do that know more about C than you do, and are do it
> >> safely, correctly, and efficiently instead of blind dangerous hacks that
> >> are /not/ guaranteed to work in any useful fashion, even if "float" and
> >> "int" are the same size on the target.
> >>
> >> C lets you do all kinds of things - including lots of non-portable
> >> things (it is intended to be usable as a systems programming language as
> >> well as a portable applications programming language). But if you try
> >> to place smartarse tricks, you need to know /exactly/ what you are doing.
> >>
> > No. C doesn't provide facilities for what you are suggesting. That doesn't mean that
> > you can't write bitwise operations on floats in C, without resorting to assembler.
> > But you can't do so "safely, correctly, efficiently" instead of "blind dangerous hacks".
> > And of course I do know how floating point arithmetic works and what I am doing.
> Look, it is /very/ simple.
>
> Let's assume that "float" is 32-bit. You are not going to be doing any
> kind of low-level bitwise operations on float unless it is a known
> format, so I think this is a fine assumption. (I've made other obvious
> non-portable assumptions too.) Maybe you are wanting to look at the
> highest significant bit as a way of identifying the sign of the number.
>
> /Your/ suggestion is that you might do something like:
>
> inline bool sign_bit(float f) {
> int * p = &f;
> int i = *p;
> return i & 0x80000000;
> }
>
> That is just /wrong/. It is wildly unsafe, and many compilers will give
> you unexpected results in practice, when the code is inlined and
> optimised.

i dont know for sure but i somewhat doubt it
such things as far as i was learned what c is are legal and defined behavoiur
&f is an adress, p holds ann adres, *p gives memory piece from that adres as if some int is there..it shouldnt break

> The C language makes it perfectly clear that trying to use
> an int* to access a float object is undefined behaviour, and compilers
> will assume that you would never do such a stupid thing, or if you did,
> you don't care what happens.
>
> Getting it /right/ is very simple. Two examples are :
>
> inline bool sign_bit(float f) {
> uint32_t u;
> memcpy(&u, &f, 4);
> return u & 0x80000000;
> }
>
> and
>
> inline bool sign_bit(float f) {
> union {
> uint32_t u;
> float f;
> } u;
> u.f = f;
> return u.u & 0x80000000;
> }
>
> Both of these are valid C (the second one is undefined behaviour in C++,
> if that matters to you) and both have fully defined behaviour.
>
> Any compiler for which it makes sense to talk about generating efficient
> code, can handle such code optimally.
>
> There really is no excuse for messing around with accessing data through
> incompatible lvalues like you suggest - correct, safe and reliable
> methods are simple and efficient.

Re: you think rust may outthrone c?

<ada403ef-b9f6-4d47-aace-592194a94dcfn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:4a5b:b0:634:dbb5:a34c with SMTP id ph27-20020a0562144a5b00b00634dbb5a34cmr77329qvb.8.1689526673651;
Sun, 16 Jul 2023 09:57:53 -0700 (PDT)
X-Received: by 2002:a05:6870:9572:b0:1b0:7c0b:7db7 with SMTP id
v50-20020a056870957200b001b07c0b7db7mr8530318oal.8.1689526673399; Sun, 16 Jul
2023 09:57:53 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 16 Jul 2023 09:57:52 -0700 (PDT)
In-Reply-To: <u90u5r$pnad$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.23; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.23
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org> <u8p0kv$h9r$1@matrix.hispagatos.org>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com> <u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com> <u90u5r$pnad$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ada403ef-b9f6-4d47-aace-592194a94dcfn@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 16 Jul 2023 16:57:53 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2638
 by: fir - Sun, 16 Jul 2023 16:57 UTC

niedziela, 16 lipca 2023 o 16:17:47 UTC+2 David Brown napisał(a):
> /Your/ suggestion is that you might do something like:
>
> inline bool sign_bit(float f) {
> int * p = &f;
> int i = *p;
> return i & 0x80000000;
> }
>
this one above is ok (imo)

>
> inline bool sign_bit(float f) {
> uint32_t u;
> memcpy(&u, &f, 4);
> return u & 0x80000000;
> }
>
>
this one above is stupid

> inline bool sign_bit(float f) {
> union {
> uint32_t u;
> float f;
> } u;
> u.f = f;
> return u.u & 0x80000000;
> }
>
this is the best imo (unions are very good invention )

overally i think you dont say the truth when you say compiler can optimize things to the extent it will damege the first piece

Re: you think rust may outthrone c?

<67219de1-555d-4f35-a55d-f2d11477fbc8n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5902:0:b0:403:ac57:a01b with SMTP id 2-20020ac85902000000b00403ac57a01bmr71465qty.5.1689528865101;
Sun, 16 Jul 2023 10:34:25 -0700 (PDT)
X-Received: by 2002:a05:6871:1c9:b0:1b0:4e46:7f13 with SMTP id
q9-20020a05687101c900b001b04e467f13mr10576054oad.2.1689528864915; Sun, 16 Jul
2023 10:34:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 16 Jul 2023 10:34:24 -0700 (PDT)
In-Reply-To: <ada403ef-b9f6-4d47-aace-592194a94dcfn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.60; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.60
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org> <u8p0kv$h9r$1@matrix.hispagatos.org>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com> <u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com> <u90u5r$pnad$1@dont-email.me>
<ada403ef-b9f6-4d47-aace-592194a94dcfn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <67219de1-555d-4f35-a55d-f2d11477fbc8n@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 16 Jul 2023 17:34:25 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3533
 by: fir - Sun, 16 Jul 2023 17:34 UTC

niedziela, 16 lipca 2023 o 18:58:04 UTC+2 fir napisał(a):
> niedziela, 16 lipca 2023 o 16:17:47 UTC+2 David Brown napisał(a):
> > /Your/ suggestion is that you might do something like:
> >
> > inline bool sign_bit(float f) {
> > int * p = &f;
> > int i = *p;
> > return i & 0x80000000;
> > }
> >
> this one above is ok (imo)
> >
> > inline bool sign_bit(float f) {
> > uint32_t u;
> > memcpy(&u, &f, 4);
> > return u & 0x80000000;
> > }
> >
> >
> this one above is stupid
> > inline bool sign_bit(float f) {
> > union {
> > uint32_t u;
> > float f;
> > } u;
> > u.f = f;
> > return u.u & 0x80000000;
> > }
> >
> this is the best imo (unions are very good invention )
>
> overally i think you dont say the truth when you say compiler can optimize things to the extent it will damege the first piece

overally its weird (or worse their weird) to write on architecture some dont know, or on two importanly different architectures at once... imu such writing could be rather understood as ugly trick/hack
so ask yourself if knowing your types and asuming them is more trick than writing on two or more at once in semi generalized way (imo the other is more trick genarally..though situations are different and sometimes its ok to use soem tricks probably)

besides compatibility should work on binary level often - my programs for windows works for example on unix even though i write totali specificaly for windows and core winapi only - and thats fine level of compatibility

Re: you think rust may outthrone c?

<ebcd03cd-6a3b-4b20-b95e-b6d36a01e3een@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:460d:b0:767:d37c:cc30 with SMTP id br13-20020a05620a460d00b00767d37ccc30mr68077qkb.13.1689529275979;
Sun, 16 Jul 2023 10:41:15 -0700 (PDT)
X-Received: by 2002:a05:6808:13c2:b0:3a1:efa5:e4a3 with SMTP id
d2-20020a05680813c200b003a1efa5e4a3mr13989378oiw.4.1689529275698; Sun, 16 Jul
2023 10:41:15 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 16 Jul 2023 10:41:15 -0700 (PDT)
In-Reply-To: <67219de1-555d-4f35-a55d-f2d11477fbc8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.60; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.60
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org> <u8p0kv$h9r$1@matrix.hispagatos.org>
<20230713104558.553@kylheku.com> <u8phd0$3niul$1@dont-email.me>
<UpYrM.257503$W7d4.2562@fx18.iad> <u8pmt9$3o80g$1@dont-email.me>
<u8q0q4$3p89v$1@dont-email.me> <u8qqpv$3v3pg$1@dont-email.me>
<u8r94r$dn9$1@dont-email.me> <u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com> <u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com> <u90u5r$pnad$1@dont-email.me>
<ada403ef-b9f6-4d47-aace-592194a94dcfn@googlegroups.com> <67219de1-555d-4f35-a55d-f2d11477fbc8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ebcd03cd-6a3b-4b20-b95e-b6d36a01e3een@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 16 Jul 2023 17:41:15 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2609
 by: fir - Sun, 16 Jul 2023 17:41 UTC

niedziela, 16 lipca 2023 o 19:34:35 UTC+2 fir napisał(a):
>
> besides compatibility should work on binary level often - my programs for windows works for example on unix even though i write totali specificaly for windows and core winapi only - and thats fine level of compatibility

i mean linux..but im fan of binary compatibility - i think even apis for some task should be standarized, not liek today you got variety of libraries with variety of interfaces and thats a mess - that creates chaos in a societu of programmers and that chaos has imo mostly downsides..its liek this so caled babel tower

Re: you think rust may outthrone c?

<87sf9niqb6.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Sun, 16 Jul 2023 20:55:57 +0100
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <87sf9niqb6.fsf@bsb.me.uk>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me>
<87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
<u90u5r$pnad$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="a75d2986c179d02b37cafed0aba66e6c";
logging-data="907276"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KHDIHI+WzLOzuRDy6qNRK/UQzBTewQGo="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:TMdMoY7A2eyIK1/AScw54H2ATd4=
sha1:FH4LgPBMGEEtvtttaV/4HFCFVGE=
X-BSB-Auth: 1.1a8ab586417b99d4bf2d.20230716205557BST.87sf9niqb6.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 16 Jul 2023 19:55 UTC

David Brown <david.brown@hesbynett.no> writes:

> Let's assume that "float" is 32-bit. You are not going to be doing any
> kind of low-level bitwise operations on float unless it is a known format,
> so I think this is a fine assumption. (I've made other obvious
> non-portable assumptions too.) Maybe you are wanting to look at the
> highest significant bit as a way of identifying the sign of the number.
>
> /Your/ suggestion is that you might do something like:
>
> inline bool sign_bit(float f) {
> int * p = &f;
> int i = *p;
> return i & 0x80000000;
> }
>
> That is just /wrong/. It is wildly unsafe, and many compilers will give
> you unexpected results in practice, when the code is inlined and optimised.
> The C language makes it perfectly clear that trying to use an int* to
> access a float object is undefined behaviour, and compilers will assume
> that you would never do such a stupid thing, or if you did, you don't care
> what happens.
>
> Getting it /right/ is very simple. Two examples are :
>
> inline bool sign_bit(float f) {
> uint32_t u;
> memcpy(&u, &f, 4);
> return u & 0x80000000;
> }
>
> and
>
> inline bool sign_bit(float f) {
> union {
> uint32_t u;
> float f;
> } u;
> u.f = f;
> return u.u & 0x80000000;
> }

Very a tangent, but I like the fact that compound literals and
designated initialisers let one write

return (union { uint32_t u; float f; }){ .f = f }.u & 0x80000000;

--
Ben.

Re: you think rust may outthrone c?

<u923gr$tf08$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Mon, 17 Jul 2023 01:54:51 +0100
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <u923gr$tf08$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
<u90u5r$pnad$1@dont-email.me> <87sf9niqb6.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 17 Jul 2023 00:54:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="badbdf683e03aa7c0751d87855b94887";
logging-data="965640"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180KG5/Xow5kwpiNy7nKTCXQ9eZSW8EHno="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:3opbDhlxPD2S0CAjo4ye6Ye8VSQ=
In-Reply-To: <87sf9niqb6.fsf@bsb.me.uk>
 by: Bart - Mon, 17 Jul 2023 00:54 UTC

On 16/07/2023 20:55, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:

>> Getting it /right/ is very simple. Two examples are :
>>
>> inline bool sign_bit(float f) {
>> uint32_t u;
>> memcpy(&u, &f, 4);
>> return u & 0x80000000;
>> }
>>
>> and
>>
>> inline bool sign_bit(float f) {
>> union {
>> uint32_t u;
>> float f;
>> } u;
>> u.f = f;
>> return u.u & 0x80000000;
>> }
>
> Very a tangent, but I like the fact that compound literals and
> designated initialisers let one write
>
> return (union { uint32_t u; float f; }){ .f = f }.u & 0x80000000;

I'm struggling to see the appeal of being able to do this!

Also, consider that the function result is a bool, which means probably
having to turn 0 or 0x80000000 into 0 or 1.

A lot of things going on just to access bit 31 of the parameter. Why the
need to go around the houses so much?

My language makes it easier, but it requires a type-punning cast to get
to an int value, so I spent a few minutes to make that automatic. Now it
is just:

f.[31] # when f has f32 type
f.[63] # f has f64 type (also f.msbit)
f.[52..62] # exponent field of f64 type

f can be any rvalue expression. (Clearly my idea of a systems language
has diverged considerably from C's.)

Sticking to C, however, I would just do:

(*(u32*)&f) & 0x80000000 # get 0x00000000 or 0x80000000

With >> or !! or (bool) used to obtain 0 or 1. Here `f` is a simple
variable and `u32` is a typedef.

If f is not a suitable l-value, it will need copying into a temp, or
wrapping the above into a function.

It just needs stipulating that it only works on the dozen or three
target platforms of interest.

Re: you think rust may outthrone c?

<87mszvwbwl.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Mon, 17 Jul 2023 02:43:22 +0100
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <87mszvwbwl.fsf@bsb.me.uk>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me>
<87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
<u90u5r$pnad$1@dont-email.me> <87sf9niqb6.fsf@bsb.me.uk>
<u923gr$tf08$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="f8d883382072c36cb7be2847616c9d1f";
logging-data="1093867"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DmHGn24fmD0N1fTlqpEDy1vYbXKVv+0M="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:f4KHOs23hDpkDQ1U0EKEpW23ugw=
sha1:m5Gr4g4+rvPXCrvP+8U0yddtfMU=
X-BSB-Auth: 1.47e1169f9c1b3abe9843.20230717024322BST.87mszvwbwl.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 17 Jul 2023 01:43 UTC

Bart <bc@freeuk.com> writes:

> On 16/07/2023 20:55, Ben Bacarisse wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>
>>> Getting it /right/ is very simple. Two examples are :
>>>
>>> inline bool sign_bit(float f) {
>>> uint32_t u;
>>> memcpy(&u, &f, 4);
>>> return u & 0x80000000;
>>> }
>>>
>>> and
>>>
>>> inline bool sign_bit(float f) {
>>> union {
>>> uint32_t u;
>>> float f;
>>> } u;
>>> u.f = f;
>>> return u.u & 0x80000000;
>>> }
>>
>> Very a tangent, but I like the fact that compound literals and
>> designated initialisers let one write
>>
>> return (union { uint32_t u; float f; }){ .f = f }.u & 0x80000000;
>
> I'm struggling to see the appeal of being able to do this!

It's not clear to what your "this" refers: the contraction into one
expression, the use of a union or something else?

> Also, consider that the function result is a bool, which means probably
> having to turn 0 or 0x80000000 into 0 or 1.

You can use

return (union { uint32_t u; float f; }){ .f = f }.u >> 31;

if you prefer. I kept the & 0x80000000 to minimise the changes. By
putting the float first, I could also have eliminated the designated
initialiser:

return (union { float _; uint32_t u; }){ f }.u >> 31;

> A lot of things going on just to access bit 31 of the parameter. Why the
> need to go around the houses so much?

Because C has not way to get at bit 31 of a float. Anyway, I don't
think there are really many houses being gone round here.

> Sticking to C, however, I would just do:
>
> (*(u32*)&f) & 0x80000000 # get 0x00000000 or 0x80000000

Why? It seems odd to prefer an expression with no defined meaning.

> It just needs stipulating that it only works on the dozen or three target
> platforms of interest.

But you could use an expression that has no such restrictions. (It
still has restrictions, of course, just not those specific stipulated
ones.)

If you are writing code for yourself, it's you that takes any hit, but I
started my programming life at a time when there were lots of different
kinds of hardware and no standard for C. I spent a lot of time
wrestling with code that was "just fine" on the few machines it had been
tried on. I saw the 1989 C standard as a huge step forward. It did not
stop people writing code the "just worked" on the three targets they
cared about, but it did let some people chose to write more portable
code if they wanted to.

--
Ben.

Re: you think rust may outthrone c?

<u92mms$12tmp$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Mon, 17 Jul 2023 08:22:20 +0200
Organization: A noiseless patient Spider
Lines: 117
Message-ID: <u92mms$12tmp$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
<u90u5r$pnad$1@dont-email.me> <87sf9niqb6.fsf@bsb.me.uk>
<u923gr$tf08$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 17 Jul 2023 06:22:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="25f00b93e551b12c719cd8f8756ce410";
logging-data="1144537"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18OpfJp02QWVJk3prnRgEFJiEc0uVL318o="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:aZYSP0zRLxQ/JV30ULUvig7n96M=
In-Reply-To: <u923gr$tf08$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Mon, 17 Jul 2023 06:22 UTC

On 17/07/2023 02:54, Bart wrote:
>
> On 16/07/2023 20:55, Ben Bacarisse wrote:
> > David Brown <david.brown@hesbynett.no> writes:
>
> >> Getting it /right/ is very simple.  Two examples are :
> >>
> >>     inline bool sign_bit(float f) {
> >>         uint32_t u;
> >>         memcpy(&u, &f, 4);
> >>         return u & 0x80000000;
> >>     }
> >>
> >> and
> >>
> >>     inline bool sign_bit(float f) {
> >>         union {
> >>             uint32_t u;
> >>             float f;
> >>         } u;
> >>         u.f = f;
> >>         return u.u & 0x80000000;
> >>     }
> >
> > Very a tangent, but I like the fact that compound literals and
> > designated initialisers let one write
> >
> >    return (union { uint32_t u; float f; }){ .f = f }.u & 0x80000000;
>

Yes, compound literals and designated initialisers can let you write
things a little neater, but in examples like these I prefer to be more
step-by-step to show what is going on. And not everyone is familiar
with the syntax here - whether for good reasons or bad reasons, neither
compound literals nor designated initialisers are very common in practice.

> I'm struggling to see the appeal of being able to do this!
>
> Also, consider that the function result is a bool, which means probably
> having to turn 0 or 0x80000000 into 0 or 1.
>

Yes.

Of course, I would expect a function like this to be optimised inline by
the compiler (the "inline" function qualifier is a hint to this). The
whole thing would cook down to a single bit test in the float, and the
compiler would use it for a conditional, or do a 31-bit shift-right, or
whatever is most efficient at the time.

Good programming is about writing clear and correct source code, and
letting the compiler do the job of the efficiency details. If
converting back or forth between a bool is an efficiency problem, you
should get a better compiler.

> A lot of things going on just to access bit 31 of the parameter. Why the
> need to go around the houses so much?
>

The code is quite simple. But it is not negligible code, because C is a
high level language. It is not assembly. A "float" is a floating point
object, not a collection of bits - you need source code to tell the
compiler that you want to view it as bits. The compiler will not
generate any unnecessary code.

>
> Sticking to C, however, I would just do:
>
>     (*(u32*)&f) & 0x80000000         # get 0x00000000 or 0x80000000
>

Yes, some people would do that - including Malcolm. That was why I made
the post, because such code is /wrong/ in C.

/Your/ own personal C compiler might have semantics to say that this is
valid and defined behaviour - that's fine. "gcc -fno-strict-aliasing"
also does. I don't know off-hand of any other compiler that supports
it, by which I mean /documents/ it as an extension to the C language.
(MSVC mentions something about it in a blog somewhere, but I don't see
that as proper documentation. However, I haven't read all of MSVC's
official documentation.)

The C standards specifically say that accessing an object through an
incompatible lvalue is undefined behaviour. The code might work by
chance, for a while, but it's a trap waiting to catch the unwary. So
why would anyone write such risky code?

We are talking about an unusual situation - it is very rare that you
ever need bit-level access to objects. (And I work at the lowest level
in coding.) It's okay that something rare takes a few lines of source
code. It's not difficult code, and it does not have run-time overhead.
So there is no excuse for not writing it /correctly/ and /safely/.

> With >> or !! or (bool) used to obtain 0 or 1. Here `f` is a simple
> variable and `u32` is a typedef.
>
> If f is not a suitable l-value, it will need copying into a temp, or
> wrapping the above into a function.

"f" is not a suitable lvalue for access by a "uint32 *" pointer.

>
> It just needs stipulating that it only works on the dozen or three
> target platforms of interest.
>

It doesn't work on gcc, clang, icc, or many other compilers. It might
work by luck on some others, and possibly it will work by design with
your own C compiler (only you can answer that).

By "work", I mean /always/ work - guaranteed by design - rather than
just look okay on a brief test but possibly fail in real use in the future.

Re: you think rust may outthrone c?

<u92mub$12sng$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Mon, 17 Jul 2023 02:26:19 -0400
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <u92mub$12sng$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8o2jf$3isv1$1@bluemanedhawk.eternal-september.org>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <s3ma5vyveo9.fsf@yahoo.com>
<u8rhrs$1b2m$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 17 Jul 2023 06:26:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="99a4a65592d9084640c0c6bb3cca7e8b";
logging-data="1143536"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18cx7OMxWazs8ZvbHHmLZHoAUf7kGv6TC4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:kUmS8RuGUDGAVCkD+xDvxHno9M4=
Content-Language: en-US
In-Reply-To: <u8rhrs$1b2m$1@dont-email.me>
 by: James Kuyper - Mon, 17 Jul 2023 06:26 UTC

On 7/14/23 09:16, Kalevi Kolttonen wrote:
.....
> You could well be right. But invoking UB is something so evil that no
> C or C++ program must ever do it, meaning that the
> programmers must know enough to avoid it.

I've unfortunately had no time to post a response since shortly before
you sent that message. In the time since then, you've been fully
informed about what's wrong with that statement, and acknowledged it.
Despite that fact, I'd still like to post my own response.

The standard defines the term "undefined behavior" as "behavior, upon
use of a nonportable or erroneous program construct or of erroneous
data, for which this document imposes no requirements". That definition
contains two important phrases that are often misunderstood. The most
important is "no requirements" - many people seem to think that UB means
that the program must fail immediately and catastrophically. In reality,
one of the most dangerous ways UB manifests is as your program doing
precisely what you incorrectly assumed it was required to do. What makes
this possibility dangerous is that you don't find out, not even by
careful testing, that your code is incorrect. You won't find out until
you port your code to a different system where the actual behavior is
different.

Another very common form of UB is to do something subtly different from
what you intended it to do, so subtly that you won't notice that it is
different unless you do very careful testing. You won't find out until
you need to make a major presentation to a client.

You don't seem to be suffering from that misconception, you seem
properly scared of UB.

The other tricky phrase is "this document". That's referring to the C
standard. Other documents might impose requirements, but as far as the C
standard is concerned, the code has undefined behavior. That was the
thing you apparently didn't realize, but I gather you're fully aware of
that now.

Something that I don't think has been properly discussed is the
connection between UB and conforming extensions to C. A fully conforming
implementation of C can provide extensions, either conforming or
non-conforming, that generally add features to the C language. A
conforming implementation can add features that radically change the
behavior of your code, so long as those features necessarily involve
code that renders the behavior undefined. The fact that the behavior is
undefined allows the implementation to define the behavior to be
whatever it wants, in order to implement the extension. The most common
way to do this is to make the feature depend upon the use of a keyword
whose name makes it a reserved identifier. For instance, any identifier
that starts with "_" followed by a capital letter is reserved, so for
example, a _OnesComplement keyword could be used to define an integer
type that uses one's complement representation. Until quite recently, it
was allowed for an implementation to specify that a particular type used
that representation, but standard C has never provided a way of
specifying that this be the case.

Re: you think rust may outthrone c?

<ea263e0e-b936-43d2-a4b2-9fe62ccccce9n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:190e:b0:762:1b2f:ec53 with SMTP id bj14-20020a05620a190e00b007621b2fec53mr86095qkb.7.1689589010765;
Mon, 17 Jul 2023 03:16:50 -0700 (PDT)
X-Received: by 2002:a05:6808:1514:b0:3a4:14c1:20f5 with SMTP id
u20-20020a056808151400b003a414c120f5mr16610526oiw.6.1689589010474; Mon, 17
Jul 2023 03:16:50 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 17 Jul 2023 03:16:50 -0700 (PDT)
In-Reply-To: <87mszvwbwl.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:10b:bea8:c8b0:7796;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:10b:bea8:c8b0:7796
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me> <20230714140153.112@kylheku.com>
<u8sf33$4nc3$1@dont-email.me> <u8u3ge$cscl$1@dont-email.me>
<87sf9oye05.fsf@nosuchdomain.example.com> <a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me> <0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
<u90u5r$pnad$1@dont-email.me> <87sf9niqb6.fsf@bsb.me.uk> <u923gr$tf08$1@dont-email.me>
<87mszvwbwl.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ea263e0e-b936-43d2-a4b2-9fe62ccccce9n@googlegroups.com>
Subject: Re: you think rust may outthrone c?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Mon, 17 Jul 2023 10:16:50 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4453
 by: Malcolm McLean - Mon, 17 Jul 2023 10:16 UTC

On Monday, 17 July 2023 at 02:43:50 UTC+1, Ben Bacarisse wrote:
> Bart <b...@freeuk.com> writes:
>
> > On 16/07/2023 20:55, Ben Bacarisse wrote:
> >> David Brown <david...@hesbynett.no> writes:
> >
> >>> Getting it /right/ is very simple. Two examples are :
> >>>
> >>> inline bool sign_bit(float f) {
> >>> uint32_t u;
> >>> memcpy(&u, &f, 4);
> >>> return u & 0x80000000;
> >>> }
> >>>
> >>> and
> >>>
> >>> inline bool sign_bit(float f) {
> >>> union {
> >>> uint32_t u;
> >>> float f;
> >>> } u;
> >>> u.f = f;
> >>> return u.u & 0x80000000;
> >>> }
> >>
> >> Very a tangent, but I like the fact that compound literals and
> >> designated initialisers let one write
> >>
> >> return (union { uint32_t u; float f; }){ .f = f }.u & 0x80000000;
> >
> > I'm struggling to see the appeal of being able to do this!
> It's not clear to what your "this" refers: the contraction into one
> expression, the use of a union or something else?
> > Also, consider that the function result is a bool, which means probably
> > having to turn 0 or 0x80000000 into 0 or 1.
> You can use
>
> return (union { uint32_t u; float f; }){ .f = f }.u >> 31;
>
> if you prefer. I kept the & 0x80000000 to minimise the changes. By
> putting the float first, I could also have eliminated the designated
> initialiser:
>
> return (union { float _; uint32_t u; }){ f }.u >> 31;
> > A lot of things going on just to access bit 31 of the parameter. Why the
> > need to go around the houses so much?
> Because C has not way to get at bit 31 of a float. Anyway, I don't
> think there are really many houses being gone round here.
> > Sticking to C, however, I would just do:
> >
> > (*(u32*)&f) & 0x80000000 # get 0x00000000 or 0x80000000
>
> Why? It seems odd to prefer an expression with no defined meaning.
>
It's got two main advantages. You don't need to create a union, which is a bit
messy (if you've several similar functions, do you create the same union everywhere
in local scope, as you did, or do you create one in global scope - neither answer
is ideal). And the C is likely a very close match for the assembler. If you've got
to use bitwise operations instead of comparing to zero with a relational operator
to get the sign, then it's quite likely you'll end up going to assembly at some
point.

This doesn't mean incidentally that casting a pointer is the way Malcolm
would do it, as some people seem to think. Just that there might be reasons
for choosing that option.

Re: you think rust may outthrone c?

<u93dlc$158qe$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: you think rust may outthrone c?
Date: Mon, 17 Jul 2023 14:54:04 +0200
Organization: A noiseless patient Spider
Lines: 102
Message-ID: <u93dlc$158qe$1@dont-email.me>
References: <ebb9e01a-6369-4629-8b38-11d28df63693n@googlegroups.com>
<u8p0kv$h9r$1@matrix.hispagatos.org> <20230713104558.553@kylheku.com>
<u8phd0$3niul$1@dont-email.me> <UpYrM.257503$W7d4.2562@fx18.iad>
<u8pmt9$3o80g$1@dont-email.me> <u8q0q4$3p89v$1@dont-email.me>
<u8qqpv$3v3pg$1@dont-email.me> <u8r94r$dn9$1@dont-email.me>
<u8rcu9$qd2$1@dont-email.me> <u8rdrf$uca$1@dont-email.me>
<20230714140153.112@kylheku.com> <u8sf33$4nc3$1@dont-email.me>
<u8u3ge$cscl$1@dont-email.me> <87sf9oye05.fsf@nosuchdomain.example.com>
<a189a8ff-5e91-43b7-9601-1f29b6a667bfn@googlegroups.com>
<u90c0s$nr2u$1@dont-email.me>
<0ff5dfe1-de32-4dae-872e-5401a3c04c97n@googlegroups.com>
<u90u5r$pnad$1@dont-email.me> <87sf9niqb6.fsf@bsb.me.uk>
<u923gr$tf08$1@dont-email.me> <87mszvwbwl.fsf@bsb.me.uk>
<ea263e0e-b936-43d2-a4b2-9fe62ccccce9n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 17 Jul 2023 12:54:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="973090694752348adfc8d9d8783b8e9d";
logging-data="1221454"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+W40HVv1u9OCXZOTPlkY4foJ3J3JmJojc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:bXj/+jnMl7+FEGTpwU5LcxEvFBk=
In-Reply-To: <ea263e0e-b936-43d2-a4b2-9fe62ccccce9n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 17 Jul 2023 12:54 UTC

On 17/07/2023 12:16, Malcolm McLean wrote:
> On Monday, 17 July 2023 at 02:43:50 UTC+1, Ben Bacarisse wrote:
>> Bart <b...@freeuk.com> writes:
>>
>>> On 16/07/2023 20:55, Ben Bacarisse wrote:
>>>> David Brown <david...@hesbynett.no> writes:
>>>
>>>>> Getting it /right/ is very simple. Two examples are :
>>>>>
>>>>> inline bool sign_bit(float f) {
>>>>> uint32_t u;
>>>>> memcpy(&u, &f, 4);
>>>>> return u & 0x80000000;
>>>>> }
>>>>>
>>>>> and
>>>>>
>>>>> inline bool sign_bit(float f) {
>>>>> union {
>>>>> uint32_t u;
>>>>> float f;
>>>>> } u;
>>>>> u.f = f;
>>>>> return u.u & 0x80000000;
>>>>> }
>>>>
>>>> Very a tangent, but I like the fact that compound literals and
>>>> designated initialisers let one write
>>>>
>>>> return (union { uint32_t u; float f; }){ .f = f }.u & 0x80000000;
>>>
>>> I'm struggling to see the appeal of being able to do this!
>> It's not clear to what your "this" refers: the contraction into one
>> expression, the use of a union or something else?
>>> Also, consider that the function result is a bool, which means probably
>>> having to turn 0 or 0x80000000 into 0 or 1.
>> You can use
>>
>> return (union { uint32_t u; float f; }){ .f = f }.u >> 31;
>>
>> if you prefer. I kept the & 0x80000000 to minimise the changes. By
>> putting the float first, I could also have eliminated the designated
>> initialiser:
>>
>> return (union { float _; uint32_t u; }){ f }.u >> 31;
>>> A lot of things going on just to access bit 31 of the parameter. Why the
>>> need to go around the houses so much?
>> Because C has not way to get at bit 31 of a float. Anyway, I don't
>> think there are really many houses being gone round here.
>>> Sticking to C, however, I would just do:
>>>
>>> (*(u32*)&f) & 0x80000000 # get 0x00000000 or 0x80000000
>>
>> Why? It seems odd to prefer an expression with no defined meaning.
>>
> It's got two main advantages.

It doesn't matter if it has a hundred advantages - your code has no
defined meaning, so it is wrong. That overrides all your (imagined)
advantages.

> You don't need to create a union, which is a bit
> messy (if you've several similar functions, do you create the same union everywhere
> in local scope, as you did, or do you create one in global scope - neither answer
> is ideal).

Your code is messy too. It's okay for something as rarely needed as
this to be a bit verbose.

And what do you even mean by "create a union" ? Do you mean declaring a
union type? That has no runtime cost, and microscopic compile time
cost, so it is irrelevant. Do you mean creating an object of union type
at runtime? You should be able to figure out how often that is done by
Ben's code - it's basic C. I find it hard to see your complaint as
having any meaning, never mind any merit.

> And the C is likely a very close match for the assembler.

That myth was dispelled fifty years ago. C is not an assembler.

> If you've got
> to use bitwise operations instead of comparing to zero with a relational operator
> to get the sign, then it's quite likely you'll end up going to assembly at some
> point.

Complete and utter bollocks.

And even if it were true, why would "you might use assembly at some
point" have any bearing on the fact that it is a bad idea to write
incorrect code in C?

>
> This doesn't mean incidentally that casting a pointer is the way Malcolm
> would do it, as some people seem to think. Just that there might be reasons
> for choosing that option.
>

The main reason people continue to write code like this is because
people like you continue to tell them it's a good idea. Please stop
doing so.


devel / comp.lang.c / Re: you think rust may outthrone c?

Pages:123456789101112131415161718192021222324252627282930313233343536373839
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor