Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail |
|---|---|
| NNTP-Posting-Date | Tue, 20 Sep 2011 22:10:27 -0500 |
| Message-ID | <4E7955A1.440D@mindspring.com> (permalink) |
| Date | Tue, 20 Sep 2011 23:10:25 -0400 |
| From | pete <pfiland@mindspring.com> |
| Reply-To | pfiland@mindspring.com |
| Organization | PF |
| X-Mailer | Mozilla 3.04Gold (WinNT; I) |
| MIME-Version | 1.0 |
| Newsgroups | comp.lang.c |
| Subject | Re: evaluate a generic swap function |
| References | <fae63bac-e9c4-48e2-9c4f-e0269b2b3f2c@w8g2000yqi.googlegroups.com> <f2f60fe3-fb49-4577-821b-f672d86f3bac@1g2000vbu.googlegroups.com> <640a6fdc-0001-4de0-ba3d-d7ab5abcdc69@z18g2000yqb.googlegroups.com> |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| Lines | 93 |
| X-Usenet-Provider | http://www.giganews.com |
| NNTP-Posting-Host | 4.156.228.31 |
| X-Trace | sv3-IjivL30pxgBKNfCUQ29BQXK48BU2LAAlOe4ZzE8IYRcKnfj1h1nMN/2wHrUUlfXkTRqV24hedmc8j27!gaZS0Hnf5uwzWF+MyYdGc3SNosQ/YvtB2IW4ca1Uqo6/0wAJrCAai31mqzuSfjAf99wPbYKRI9DL!6dIWeVPiSA== |
| X-Abuse-and-DMCA-Info | Please be sure to forward a copy of ALL headers |
| X-Abuse-and-DMCA-Info | Otherwise we will be unable to process your complaint properly |
| X-Postfilter | 1.3.40 |
| X-Original-Bytes | 4635 |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.c:10626 |
Show key headers only | View raw
ImpalerCore wrote:
>
> On Sep 20, 10:33 am, Malcolm McLean <malcolm.mcle...@btinternet.com>
> wrote:
> > On Sep 20, 4:51 pm, Stanley Rice <hecong...@gmail.com> wrote:> Hello all
> >
> > > I wrote a "generic" swap function, which can swap some type, such as
> > > int, double, long. But I don't know how to evaluate this function, and
> > > don't know whether it's a good idea to use such a function. I hope you
> > > can give me some suggestions.
> >
> > The function calls malloc to allocate a temporary buffer. This means
> > that it's vulnerable to failure. It's also likely to be inefficient.
> >
> > You can write swap() as a byte swapping loop, which avoids the need
> > for a malloc(). The problem is that most calls will be to arrays of
> > ints or other larger types. You can probably get a 4x speed up if you
> > know that data is a multiple of four and aligned.
> > What this means is that swap() is really a function, like memcpy, best
> > implemented by the compiler, which can apply patches to speed up
> > common cases. E.g. swapping two basic types, which is an obvious use
> > for the function, can be turned into a sequence of three
> > instructions, or even sometimes eliminated altogether.
>
> One method to consider is to write macros to declare the function
> prototype and definition of a swap specialized to the type.
>
> \code snippet
> #define C_SWAP_PROTO( NAME, TYPE ) void c_swap_##NAME( TYPE* left,
> TYPE* right )
>
> #define C_SWAP_IMPL( NAME, TYPE ) \
> void c_swap_##NAME( TYPE* left, TYPE* right ) \
> { \
> TYPE tmp; \
> \
> tmp = *left; \
> *left = *right; \
> *right = tmp; \
> }
> \endcode
>
> Then, if one wants to swap a particular type, they declare the
> prototype and definition where needed and use the generated function
> name to swap the objects. The advantage is that in a library setting,
> one can create a generic template for a swap without the need to
> maintain an arbitrary list of swap functions, like c_swap_int,
> c_swap_long, c_swap_my_struct, etc.
>
> The reason for the 'NAME' attribute is that types with spaces in them
> obviously cause problems in the function prototypes. You could use
> them like the following.
>
> \code snippet
> #include <stdio.h>
> #include <stdlib.h>
>
> C_SWAP_PROTO( uint, unsigned int );
>
> int main( void )
> {
> unsigned int u1 = 10;
> unsigned int u2 = 20;
>
> c_swap_uint( &u1, &u2 );
>
> printf( "[u1,u2] = [%u,%u]\n", u1, u2 );
>
> return EXIT_SUCCESS;
> }
>
> /* Important to *not* add the semicolon after the IMPL macro */
> C_SWAP_IMPL( uint, unsigned int )
> \endcode
>
> I fully acknowledge that some may consider this "function template" a
> coding horror. But at the same time, I have found this kind of setup
> very convenient from a maintenance perspective.
I use these macros:
#define SWAP(N, A, B, T) \
(MOV##N((T), (A)), MOV##N((A), (B)), MOV##N((B), (T)))
#define MOVi(A, B) ((void)(*(A) = *(B)))
#define MOVa(A, B) ((void)memcpy((A), (B), sizeof *(A)))
MOVa is for when *(A) is of array type.
MOVi is for when *(A) isn't of array type.
--
pete
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
evaluate a generic swap function Stanley Rice <heconghui@gmail.com> - 2011-09-20 06:51 -0700
Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-20 16:01 +0200
Re: evaluate a generic swap function jt@toerring.de (Jens Thoms Toerring) - 2011-09-20 14:29 +0000
Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-20 21:13 +0200
Re: evaluate a generic swap function jt@toerring.de (Jens Thoms Toerring) - 2011-09-20 20:30 +0000
Re: evaluate a generic swap function Keith Thompson <kst-u@mib.org> - 2011-09-20 17:05 -0700
Re: evaluate a generic swap function gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-21 15:45 +0000
Re: evaluate a generic swap function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-21 09:33 -0700
Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-21 19:17 +0200
Re: evaluate a generic swap function gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-21 17:34 +0000
Re: evaluate a generic swap function Jack Foot <foot@dev.null.invalid> - 2011-09-21 18:12 +0000
Re: evaluate a generic swap function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-20 07:33 -0700
Re: evaluate a generic swap function ImpalerCore <jadill33@gmail.com> - 2011-09-20 08:26 -0700
Re: evaluate a generic swap function pete <pfiland@mindspring.com> - 2011-09-20 23:10 -0400
Re: evaluate a generic swap function "BartC" <bc@freeuk.com> - 2011-09-20 22:20 +0100
Re: evaluate a generic swap function jacob navia <jacob@spamsink.net> - 2011-09-20 23:23 +0200
Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-20 23:07 +0100
Re: evaluate a generic swap function Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-20 21:15 -0400
Re: evaluate a generic swap function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-21 03:17 -0700
Re: evaluate a generic swap function Gene <gene.ressler@gmail.com> - 2011-09-21 02:40 -0700
Re: evaluate a generic swap function Seebs <usenet-nospam@seebs.net> - 2011-09-23 01:46 +0000
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 10:26 -0700
Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 18:36 +0100
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 10:49 -0700
Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 20:18 +0100
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 12:54 -0700
Re: evaluate a generic swap function James Kuyper <jameskuyper@verizon.net> - 2011-09-23 16:43 -0400
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 13:49 -0700
Re: evaluate a generic swap function James Kuyper <jameskuyper@verizon.net> - 2011-09-23 17:44 -0400
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 18:04 -0700
Re: evaluate a generic swap function Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 13:52 -0800
Re: evaluate a generic swap function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 21:46 +0100
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 13:59 -0700
Re: evaluate a generic swap function Seebs <usenet-nospam@seebs.net> - 2011-09-26 22:59 +0000
Re: evaluate a generic swap function pete <pfiland@mindspring.com> - 2011-09-23 18:19 -0400
Re: evaluate a generic swap function Nelu <tandauioan@gmail.com> - 2011-09-23 16:36 -0700
csiph-web