* Fetch-and-store for PowerPC... and more!
@ 2005-03-16 8:48 Ludovic Courtès
2005-03-16 23:26 ` Kevin Ryde
0 siblings, 1 reply; 5+ messages in thread
From: Ludovic Courtès @ 2005-03-16 8:48 UTC (permalink / raw)
Hi,
Guile 1.7.2 doesn't compile on PowerPC because the `FETCH_STORE' macro
(in `arbiters.c') that is defined is the generic one. The generic
version of `FETCH_STORE' relies on `scm_mutex_lock ()' and
`scm_mutex_unlock ()' which are not (yet) implemented.
So I implemented the PowerPC-version (32-bit) of `FETCH_STORE' (see
below, this is a well-documented example). Below is also a tiny test
program that shows that, well, it fetches and stores. ;-)
After doing it, it occurred to me that maybe I should have look at Glibc
before doing it. And it turns out that Glibc, indeed, already
implements fetch-and-store for 15 architectures (see `always_swap ()' in
`atomicity.h')... Unfortunately, this header doesn't seem to get
installed, so we'll have to rip it (G++ also comes with its own
implementation of fetch-and-store among other things anyway).
Thanks,
Ludovic.
\f
diff -ubB --show-c-function /home/ludo/tmp/guile-1.7.2/libguile/arbiters.c\~ /home/ludo/tmp/guile-1.7.2/libguile/arbiters.c
--- /home/ludo/tmp/guile-1.7.2/libguile/arbiters.c~ 2004-08-22 03:49:10.000000000 +0200
+++ /home/ludo/tmp/guile-1.7.2/libguile/arbiters.c 2005-03-16 09:35:07.000000000 +0100
@@ -36,7 +36,9 @@
ENHANCE-ME: Add more cpu-specifics. glibc atomicity.h has some of the
sort of thing required. FETCH_STORE could become some sort of
- compare-and-store if that better suited what various cpus do. */
+ compare-and-store if that better suited what various cpus do. Note: look
+ at the `always_swap ()' function is Glibc's `atomicity.h' in the `sysdeps'
+ directory. */
#if defined (__GNUC__) && defined (i386) && SIZEOF_SCM_T_BITS == 4
/* This is for i386 with the normal 32-bit scm_t_bits. The xchg instruction
@@ -59,6 +61,28 @@
} while (0)
#endif
+#if defined (__GNUC__) && defined (__powerpc__) && SIZEOF_SCM_T_BITS == 4
+
+/* On 32-bit PowerPC arches, we use the `lwarx' ("load word and reserve,
+ indexed") and `stwcx.' ("store word conditional, indexed") instructions.
+ In effect, data is loaded from MEM into FET and a reservation bit is set
+ to 1; `stwcx.' will only store STO into MEM if the reservation bit is
+ still set when it is executed, otherwise we start again the whole process.
+ This is a well-known example available in the "PowerPC Processor Reference
+ Guide" by Xilinx, for example. */
+
+#define FETCH_STORE(fet,mem,sto) \
+do { \
+ asm ("\n1:\n" \
+ "\tlwarx %0,0,%1\n" \
+ "\tstwcx. %2,0,%1\n" \
+ "\tbne- 1b\n" \
+ : "=&r" (fet) \
+ : "r" (mem), "r" (sto)); \
+} while (0)
+
+#endif
+
#ifndef FETCH_STORE
/* This is a generic version, with a mutex to ensure the operation is
atomic. Unfortunately this approach probably makes arbiters no faster
\f
#include <stdio.h>
#include <assert.h>
#define FETCH_STORE(fet,mem,sto) \
do { \
asm ("\n1:\n" \
"\tlwarx %0,0,%1\n" \
"\tstwcx. %2,0,%1\n" \
"\tbne- 1b\n" \
: "=&r" (fet) \
: "r" (mem), "r" (sto)); \
} while (0)
static int in_memory = 12;
int
main (int argc, char *argv[])
{
int old_value = 0, new_value = 7;
while (1)
{
int actual_old_value = in_memory;
FETCH_STORE (old_value, &in_memory, new_value);
printf ("old=%i, new=%i, mem=%i\n", old_value, new_value, in_memory);
assert (old_value == actual_old_value);
assert (new_value == in_memory);
new_value += old_value;
}
return 0;
}
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Fetch-and-store for PowerPC... and more!
2005-03-16 8:48 Fetch-and-store for PowerPC... and more! Ludovic Courtès
@ 2005-03-16 23:26 ` Kevin Ryde
2005-03-17 8:29 ` Ludovic Courtès
2005-03-18 9:26 ` Marius Vollmer
0 siblings, 2 replies; 5+ messages in thread
From: Kevin Ryde @ 2005-03-16 23:26 UTC (permalink / raw)
Cc: guile-user
ludovic.courtes@laas.fr (Ludovic Courtès) writes:
>
> The generic
> version of `FETCH_STORE' relies on `scm_mutex_lock ()' and
> `scm_mutex_unlock ()' which are not (yet) implemented.
Actually, they were implemented but then disappeared. Recent breakage
:-).
> + Note: look
> + at the `always_swap ()' function is Glibc's `atomicity.h' in the `sysdeps'
> + directory. */
Looks like that's changed in the latest and greatest glibc, now
bits/atomic.h and __arch_atomic_whatever().
> +#if defined (__GNUC__) && defined (__powerpc__) && SIZEOF_SCM_T_BITS == 4
> +
> +/* On 32-bit PowerPC arches, we use the `lwarx' ("load word and reserve,
> + indexed") and `stwcx.' ("store word conditional, indexed") instructions.
No need to describe instructions. (But crib notes on the reservation
mechanism for single or multi cpus is fine.)
> + asm ("\n1:\n" \
I think "1:" is a gas-ism, it might not work on aix and macos. But
dunno if guile actually runs at all on those systems at the moment.
> + : "=&r" (fet) \
> + : "r" (mem), "r" (sto)); \
I think "mem" should be in the outputs too, since it's changed.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Fetch-and-store for PowerPC... and more!
2005-03-16 23:26 ` Kevin Ryde
@ 2005-03-17 8:29 ` Ludovic Courtès
2005-03-17 20:48 ` Kevin Ryde
2005-03-18 9:26 ` Marius Vollmer
1 sibling, 1 reply; 5+ messages in thread
From: Ludovic Courtès @ 2005-03-17 8:29 UTC (permalink / raw)
Cc: guile-user
Hi,
Kevin Ryde <user42@zip.com.au> writes:
> I think "1:" is a gas-ism, it might not work on aix and macos. But
> dunno if guile actually runs at all on those systems at the moment.
Well, the GAS documentation doesn't mention any syntax incompatibilities
in the `PPC-Dependent' node, so I don't know whether this is really is a
GAS-ism.
>> + : "=&r" (fet) \
>> + : "r" (mem), "r" (sto)); \
>
> I think "mem" should be in the outputs too, since it's changed.
I don't think so since `mem', as an address, isn't changed.
However, the Glibc implementation rightfully adds the following list of
clobbered registers:
: "cr0", "memory"
(`cr0' is being modified by `stwcx.' while `memory' denotes the
reservation bit I guess).
This should be added to the patch I sent.
Thanks,
Ludovic.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Fetch-and-store for PowerPC... and more!
2005-03-17 8:29 ` Ludovic Courtès
@ 2005-03-17 20:48 ` Kevin Ryde
0 siblings, 0 replies; 5+ messages in thread
From: Kevin Ryde @ 2005-03-17 20:48 UTC (permalink / raw)
Cc: guile-user
ludovic.courtes@laas.fr (Ludovic Courtès) writes:
>
> I don't think so since `mem', as an address, isn't changed.
Yep, I meant the contents.
> `memory' denotes the reservation bit I guess).
No, memory normally means any crazy fetch or store, so gcc won't defer
storing stuff and wont hold stuff in registers across your asm.
"*mem" is the only actual change, so expressing that only it is
fetched/stored should give better code. Though the actual current
usages are simple enough that it probably doesn't make any difference.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Fetch-and-store for PowerPC... and more!
2005-03-16 23:26 ` Kevin Ryde
2005-03-17 8:29 ` Ludovic Courtès
@ 2005-03-18 9:26 ` Marius Vollmer
1 sibling, 0 replies; 5+ messages in thread
From: Marius Vollmer @ 2005-03-18 9:26 UTC (permalink / raw)
Kevin Ryde <user42@zip.com.au> writes:
> ludovic.courtes@laas.fr (Ludovic Courtès) writes:
>>
>> The generic version of `FETCH_STORE' relies on `scm_mutex_lock ()'
>> and `scm_mutex_unlock ()' which are not (yet) implemented.
>
> Actually, they were implemented but then disappeared. Recent
> breakage :-).
Yep, sorry. Thanks for fixing it!
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-03-18 9:26 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-16 8:48 Fetch-and-store for PowerPC... and more! Ludovic Courtès
2005-03-16 23:26 ` Kevin Ryde
2005-03-17 8:29 ` Ludovic Courtès
2005-03-17 20:48 ` Kevin Ryde
2005-03-18 9:26 ` Marius Vollmer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).