Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #145932
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: in place swap of float types |
| Date | 2019-09-03 16:13 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <qklsej$7o7$1@dont-email.me> (permalink) |
| References | <slrnqmaoja.4b2.3queens@cpe-65-25-200-198.new.res.rr.com> <slrnqmb0nn.cdo.grahn+nntp@frailea.sa.invalid> <slrn3ha8qmqd2h.t2.andrews@sdf.lonestar.org> <slrnqmqrsd.cdo.grahn+nntp@frailea.sa.invalid> <9e0efdf9-a63a-49e1-af8d-6411ba5e5b9a@googlegroups.com> |
On 03/09/2019 15:37, Malcolm McLean wrote:
> On Monday, 2 September 2019 20:37:58 UTC+1, Jorgen Grahn wrote:
>> On Mon, 2019-09-02, Andrew Smallshaw wrote:
>>> On 2019-08-27, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
>>>> On Tue, 2019-08-27, 3queens@shabbyroad.com wrote:
>>>>> Some time ago, there was a thread concerning using "a^=b; b^=a; a^=b"
>>>>> to do an in place swap of two variables of the same size. It was
>>>>> pointed out that this can only be done with integer types. I found a
>>>>> way to do it with floating point types as well.
>>>> ...
>>>>> Maybe not that useful, but if space is really tight, perhaps useful in
>>>>> some situations.
>>>>
>>>> An optimizing compiler would, I strongly suspect, do a better job with
>>>> no weird code involved. I think it's all an intellectual exercise and
>>>> nothing more (or less).
>>>
>>> I have long suspected so to, one proviso is that I'd use a dedicated
>>> variable rather than reusing a generic "temporary" variable which
>>> IMHO is bad practice anyway since there is no way to indicate to
>>> the compiler the value doesn't need to be preserved between distinct
>>> uses. To make it as clear as possible declare it with the tightest
>>> scope possible, e.g.
>>>
>>> double a, b;
>>>
>>> ...
>>>
>>> {
>>> double c = a;
>>> a = b;
>>> b = c;
>>> }
>>
>> I'd use a function: swap(&a, &b);
>>
>> I think/assume that would get inlined adequately.
>>
> It would be
>
> swap(&a, &b, sizeof(a));
>
> Then the only way of implementing it portably is via a byte copying loop.
> So you're demanding quite a lot of the compiler to see that that is
> equivalent to a store to a temporary. It would be interesting to see
> if different compilers manage to optimise it.
>
"swap" could be a _Generic macro (or, if you allow extensions, a macro
with gcc's "typeof").
I tested this briefly:
static inline void swap_a(void * pa, void * pb, size_t n) {
unsigned char t[n];
memcpy(t, pa, n);
memcpy(pa, pb, n);
memcpy(pb, t, n);
}
gcc and clang are able to handle that as efficiently as type-specific
swaps for "int" or "double" :
typedef double T;
static inline void swap_b(T * pa, T * pb) {
T t = *pa;
*pa = *pb;
*pb = t;
}
But they did not handle a manual byte copy loop as well:
static inline void swap_c(void * pa, void * pb, size_t n) {
unsigned char * p = pa;
unsigned char * q = pb;
while (n--) {
unsigned char t = *p;
*p++ = *q;
*q++ = t;
}
}
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
in place swap of float types 3queens@shabbyroad.com - 2019-08-27 17:03 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-27 19:22 +0000
Re: in place swap of float types Andrew Smallshaw <andrews@sdf.org> - 2019-09-02 15:25 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-09-02 19:37 +0000
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-03 06:37 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-03 16:13 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-03 16:54 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 11:19 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-04 02:35 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 12:31 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 12:12 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 15:53 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-04 07:17 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:12 +0200
Re: in place swap of float types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-09-05 21:11 +0100
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 16:30 +0100
Re: in place swap of float types jadill33@gmail.com - 2019-09-04 10:26 -0700
Re: in place swap of float types Ike Naar <ike@iceland.freeshell.org> - 2019-09-05 06:00 +0000
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-05 11:14 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 14:54 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-05 15:48 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:35 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-06 02:52 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 13:18 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-06 12:14 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 15:08 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-06 06:23 -0700
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-09-04 14:16 -0700
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 22:42 +0100
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-09-04 15:21 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:15 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-04 08:05 -0600
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:17 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-05 20:28 -0600
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:39 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-06 08:11 -0600
Re: in place swap of float types scott@slp53.sl.home (Scott Lurndal) - 2019-09-06 18:31 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-09-04 19:02 +0000
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-02 22:16 +0200
Re: in place swap of float types Robert Wessel <robertwessel2@yahoo.com> - 2019-08-27 17:59 -0500
Re: in place swap of float types Richard Damon <Richard@Damon-Family.org> - 2019-08-27 17:45 -0700
Re: in place swap of float types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-27 20:48 -0400
Re: in place swap of float types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-03 06:17 -0700
Re: in place swap of float types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-08-28 02:09 +0100
Re: in place swap of float types Siri Cruise <chine.bleu@yahoo.com> - 2019-08-27 21:26 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 09:22 +0200
Re: in place swap of float types Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-08-28 16:36 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 09:36 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-08-28 12:02 +0100
Re: in place swap of float types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 05:08 -0700
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-08-28 05:26 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 14:40 +0200
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-08-28 13:01 -0700
Re: in place swap of float types Philipp Klaus Krause <pkk@spth.de> - 2019-09-04 11:18 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 12:57 +0200
Re: in place swap of float types Philipp Klaus Krause <pkk@spth.de> - 2019-09-04 13:24 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 08:18 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:19 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 16:18 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-05 07:32 -0700
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 17:32 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:54 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-06 14:23 +0200
csiph-web