Topic: Exchange Operator


Author: "Jonathan Ross" <Ross@CharterTN.net>
Date: Tue, 26 Feb 2002 17:58:17 GMT
Raw View
   While looking at several ways to optimize string functions I realized one
way would involve the use of exchange between two bytes (words for Unicode)
in different locations of the string.  While I realize I can use the XOR
trick to accomplish this without need of external storage such as another
variable or a register, I also realized that the x86 architecture supports
the xchg instruction.  This would be very portable and would also serve to
give the compiler more of an idea of what you're trying to do to allow
better optimization if an xchg function doesn't exist on the target
platform.

   And while yes, a function may suffice, however there would be instances
where an exchange would come in handy with objects as an overloaded
function, especially pointers to objects so you could change all aliases and
not just raw values such as integers.

   var1 <=> var2;              /* Something like that although I'm sure
someone would have a better suggestion for the actual operator */
   var1 += var2 <=> var3 /* The "new" left value would be returned for use
in multi-operation statements. */

--Jonathan Ross, ross@chartertnNO123SPAM.net (please remove all capital
letters and numbers if you wish to e-mail me)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Craig Tiller" <ctiller@bigpond.net.au>
Date: Wed, 27 Feb 2002 07:29:34 CST
Raw View
This might make people more eager to implement swap as well...

"Jonathan Ross" <Ross@CharterTN.net> wrote in message
news:008801c1be1c$7ee71640$56669e18@vross...
>    While looking at several ways to optimize string functions I realized
one
> way would involve the use of exchange between two bytes (words for
Unicode)
> in different locations of the string.  While I realize I can use the XOR
> trick to accomplish this without need of external storage such as another
> variable or a register, I also realized that the x86 architecture supports
> the xchg instruction.  This would be very portable and would also serve to
> give the compiler more of an idea of what you're trying to do to allow
> better optimization if an xchg function doesn't exist on the target
> platform.
>
>    And while yes, a function may suffice, however there would be instances
> where an exchange would come in handy with objects as an overloaded
> function, especially pointers to objects so you could change all aliases
and
> not just raw values such as integers.
>
>    var1 <=> var2;              /* Something like that although I'm sure
> someone would have a better suggestion for the actual operator */
>    var1 += var2 <=> var3 /* The "new" left value would be returned for use
> in multi-operation statements. */
>
> --Jonathan Ross, ross@chartertnNO123SPAM.net (please remove all capital
> letters and numbers if you wish to e-mail me)
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Michiel.Salters@cmg.nl (Michiel Salters)
Date: Wed, 27 Feb 2002 17:14:40 GMT
Raw View
"Jonathan Ross" <Ross@CharterTN.net> wrote in message news:<008801c1be1c$7ee71640$56669e18@vross>...
> While looking at several ways to optimize string functions I realized one
> way would involve the use of exchange between two bytes (words for Unicode)
> in different locations of the string.  While I realize I can use the XOR
> trick to accomplish this without need of external storage such as another
> variable or a register, I also realized that the x86 architecture supports
> the xchg instruction.  This would be very portable and would also serve to
> give the compiler more of an idea of what you're trying to do to allow
> better optimization if an xchg function doesn't exist on the target
> platform.

>    var1 <=> var2;              /* Something like that although I'm sure
> someone would have a better suggestion for the actual operator */

Actually, I do have a better idea how to name this. How about
std::swap()? std::swap<int> probably can be implemented using XCHG.

Regards,

--
Michiel Salters

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]