A
Arthur J. O'Dwyer
Thanks a lot of reply messages.
You're welcome, but please don't snip attributions. Those are
the lines that say "On Foo 2004, Bar wrote:", and they tell the
reader (us) who's talking. It's politeness.
[Someone wrote:]
It is my mistake. A 'int' means a 'long'.
Of course, the same objection applies to 'long' (or any other
type you care to mention).
Hmm, if there was a memxor() function that would exclusive
or one region of memory with another, then this could be
done somewhat portably. memand() and memor() to complete
the set. Maybe it should be added to C2009.
A memxor() not implemented [in] my environment(library) yet.
But it's easy to implement for me. Thanks a valuable
information.
I'm pretty sure this corresponds to what you'll want. Untested
code, though.
#include <stdlib.h>
void mem_xor(void *p, const void *q, size_t len)
{
unsigned char *cp = p, *cend = cp+len;
const unsigned char *cq = q;
while (cp != cend)
*cp++ ^= *cq++;
}
[re: better use a temp]
Yes, I know my program is slow and tricky.
But, imagine the microchip programming like a PIC.
(which has 64/128 byte's mem only --It is off topic here?)
Technically, yes, PIC implementations that don't allow the
construction of objects at least 65535 bytes in size cannot be
conforming hosted C implementations, and thus are off-topic
in comp.lang.c. In reality, I think most questions about C
on PICs turn out to apply equally to hosted implementations.
This one seems to me to qualify.
A double tmp variable needs 8 bytes stack and microchip
has not enough memory.
You mean, your implementation doesn't even allocate 8 bytes
for a stack? That's pretty tight (and not in the good way,
either)! The obvious "fix" would be to make the temporary
variable 'static', but that would actually be worse, memory-wise,
than the 'auto' solution, assuming a naive optimizer.
Are you *sure* that you can't get a better optimizing compiler,
or activate more optimizations on the one you have? I don't
know your system, but it seems to me that if your system can
perform
x ^= y;
in registers, where x and y are memory locations, then it
certainly ought to also provide an 'XCHG' instruction to exchange
two memory locations. And if such an instruction exists, your
compiler ought to provide a way to access it, either through
its own optimizations or directly through inline assembly code
(which is *definitely* off-topic here).
Don't say 'Use Assembly Language'.
Ookaay... but it *would* be the simplest way, wouldn't it?
void dswap(double *x, double *y){
*(long long *)x ^= *(long long *)y;
*(long long *)y ^= *(long long *)x;
*(long long *)x ^= *(long long *)y;
}
In my programming environment, it worked correctly and object
code has not use the surplus mem.
But it's just as non-portable as if you'd used inline assembly
code, and much slower and larger --- so where's the gain? I
don't get it.
-Arthur