Skip to contentSkip to conversation

Optimized PowerPC SHA1 generation for Darwin (OS X)

RFC/PATCH Arjen Laarhoven 4 participants 7 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

The compiler toolchain supplied by Apple's Xcode environment has an old version (1.38) of the GNU assembler. It cannot assemble the optimized ppc/sha1ppc.S file. ppc/sha1ppc.S was rewritten into a Perl script which outputs the same code, but valid for the Xcode assembler.

---
 Makefile                     |   15 +++-
 ppc/darwin/darwin_ppc_gen.pl |  211 ++++++++++++++++++++++++++++++++++++++++++
 ppc/{ => linux}/sha1ppc.S    |    0 
 3 files changed, 223 insertions(+), 3 deletions(-)
 create mode 100755 ppc/darwin/darwin_ppc_gen.pl
 rename ppc/{ => linux}/sha1ppc.S (100%)
Makefile +12 -3
index b159ffd..a91fa2a 100644
@@ -587,9 +587,13 @@ ifdef OLD_ICONV
 	BASIC_CFLAGS += -DOLD_ICONV
 endif
 
-ifdef PPC_SHA1
+ifdef PPC_SHA1_LINUX
 	SHA1_HEADER = "ppc/sha1.h"
-	LIB_OBJS += ppc/sha1.o ppc/sha1ppc.o
+	LIB_OBJS += ppc/sha1.o ppc/linux/sha1ppc.o
+else
+ifdef PPC_SHA1_DARWIN
+	SHA1_HEADER = "ppc/sha1.h"
+	LIB_OBJS += ppc/sha1.o ppc/darwin/sha1ppc.o
 else
 ifdef ARM_SHA1
 	SHA1_HEADER = "arm/sha1.h"
@@ -604,6 +608,7 @@ else
 endif
 endif
 endif
+endif
 ifdef NO_PERL_MAKEMAKER
 	export NO_PERL_MAKEMAKER
 endif
@@ -620,6 +625,7 @@ endif
 ifneq ($(findstring $(MAKEFLAGS),s),s)
 ifndef V
 	QUIET_CC       = @echo '   ' CC $@;
+	QUIET_AS       = @echo '   ' AS $@; 
 	QUIET_AR       = @echo '   ' AR $@;
 	QUIET_LINK     = @echo '   ' LINK $@;
 	QUIET_BUILT_IN = @echo '   ' BUILTIN $@;
@@ -780,6 +786,9 @@ exec_cmd.o: exec_cmd.c GIT-CFLAGS
 builtin-init-db.o: builtin-init-db.c GIT-CFLAGS
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $<
 
+ppc/darwin/sha1ppc.S:
+	$(QUIET_GEN)$(PERL_PATH) ppc/darwin/darwin_ppc_gen.pl > $@
+
 http.o: http.c GIT-CFLAGS
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $<
 
@@ -962,7 +971,7 @@ dist-doc:
 ### Cleaning rules
 
 clean:
-	rm -f *.o mozilla-sha1/*.o arm/*.o ppc/*.o compat/*.o xdiff/*.o \
+	rm -f *.o mozilla-sha1/*.o arm/*.o ppc/*.o ppc/darwin/*.[os] ppc/linux/*.o compat/*.o xdiff/*.o \
 		test-chmtime$X $(LIB_FILE) $(XDIFF_LIB)
 	rm -f $(ALL_PROGRAMS) $(BUILT_INS) git$X
 	rm -f *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags
ppc/darwin/darwin_ppc_gen.pl new file +211 -0

211 lines not on this page · raw message

ppc/linux/sha1ppc.S renamed from ppc/sha1ppc.S +0 -0
similarity index 100%
rename from ppc/sha1ppc.S
rename to ppc/linux/sha1ppc.S
Signature · 1 line
1.5.1.rc3.29.gd8b6
#1 lore raw
Quoted 6 lines · [email protected] (Arjen Laarhoven) writes:

The compiler toolchain supplied by Apple's Xcode environment has an old version (1.38) of the GNU assembler. It cannot assemble the optimized ppc/sha1ppc.S file. ppc/sha1ppc.S was rewritten into a Perl script which outputs the same code, but valid for the Xcode assembler.

Gaah.

When there are improvements/fixes to the sha1ppc.S side, how are you going to keep that in sync with darwin_ppc_gen.pl? If that script *_gen.pl were a postprocessor that munges CPP output from sha1ppc.S to make it assemblable with an old assembler, it would be one thing. But this looks horrible.

#2 lore raw
In reply to Arjen Laarhoven
Quoted 5 lines · Arjen Laarhoven

The compiler toolchain supplied by Apple's Xcode environment has an old version (1.38) of the GNU assembler. It cannot assemble the optimized ppc/sha1ppc.S file. ppc/sha1ppc.S was rewritten into a Perl script which outputs the same code, but valid for the Xcode assembler.

Ugh. That's just too ugly.

The Linux version of the GNU assembler can certainly take the same limited input as the old Apple one.

So how about instea dof having two totally different versions of this file, just having *one*, and having a pre-processor that turns it into something that is acceptable to both?

And yes, it could be your perl script, except your perl script is ugly as *hell*. The old C preprocessor code is much nicer than your perl script that does "print" statements.

How about something like the following instead?

(a) make the register macros expand to something easily greppable/parseable
(b) have a *separate* preprocessor phase that actually then takes that pattern, and evaluates it to a numeric value.
(c) assemble the end result

The (a) part is trivial. Just a patch like the appended will make sure that all the registers are now written as "REG[int-expression]", and then all you need is a perl-script or something that can trigger on the regexp

	"REG\[\([^]]*\)\]"

and replace that regex with

	"%eval(\1)"

which is somethign that perl should be designed for.

That way you just have *one* source file (the "sha1ppc.S" one), which is readable, and a simple script to then evaluate the register numbers statically instead of expecting that the assembler can do it (since the Apple one apparently cannot).

So it would just require somebody who knows perl. What's a one-liner perl script to turn a line like

	add REG[((0)+0)%5+6],REG[((0)+0)%5+6],REG[(0)%16+11];

into

	add %6,%6,%11

(ie it just evaluated the expression inside the [] things, and replaced it with the "%<num>" string)?

<Taunting mode>Or maybe perl can't do that in a single line!</Taunting mode>

Linus

---

ppc/sha1ppc.S +6 -6
index f132696..cc554a4 100644
@@ -32,14 +32,14 @@
  * We use registers 6 - 10 for this.  (Registers 27 - 31 hold
  * the previous values.)
  */
-#define RA(t)	(((t)+4)%5+6)
-#define RB(t)	(((t)+3)%5+6)
-#define RC(t)	(((t)+2)%5+6)
-#define RD(t)	(((t)+1)%5+6)
-#define RE(t)	(((t)+0)%5+6)
+#define RA(t)	REG[((t)+4)%5+6]
+#define RB(t)	REG[((t)+3)%5+6]
+#define RC(t)	REG[((t)+2)%5+6]
+#define RD(t)	REG[((t)+1)%5+6]
+#define RE(t)	REG[((t)+0)%5+6]
 
 /* We use registers 11 - 26 for the W values */
-#define W(t)	((t)%16+11)
+#define W(t)	REG[(t)%16+11]
 
 /* Register 5 is used for the constant k */
 
#3 lore raw

Hi,

Quoted 10 lines · Linus Torvalds
Quoted 5 lines · Arjen Laarhoven

The compiler toolchain supplied by Apple's Xcode environment has an old version (1.38) of the GNU assembler. It cannot assemble the optimized ppc/sha1ppc.S file. ppc/sha1ppc.S was rewritten into a Perl script which outputs the same code, but valid for the Xcode assembler.

Ugh. That's just too ugly.

Yes. Very. I should've reworked it before sending it to the list. Ah well.

Quoted 6 lines

The Linux version of the GNU assembler can certainly take the same limited input as the old Apple one.

So how about instea dof having two totally different versions of this file, just having *one*, and having a pre-processor that turns it into something that is acceptable to both?

That is of course the best way to handle it. See the patch below for the reworked solution.

[snip excellent pointers]

Quoted 5 lines

So it would just require somebody who knows perl. What's a one-liner perl script to turn a line like

	add REG[((0)+0)%5+6],REG[((0)+0)%5+6],REG[(0)%16+11];
Quoted 8 lines

into

	add %6,%6,%11

(ie it just evaluated the expression inside the [] things, and replaced it with the "%<num>" string)?

<Taunting mode>Or maybe perl can't do that in a single line!</Taunting mode>

Of course it can! :-P

But there are some other issues like the underscore prefix of the symbol in the assembly and the inability of Apple's assembler to handle multiple statements per line. So for the sake of maintainability I've put it in its own file, and even turned on warnings and strict ;-)

I don't have access to a Linux/PPC machine, so it could very well need some tweaking. Someone with a Linux/PPC box want to give it a try?

---snip---
Optimized PowerPC SHA-1 calculation for Darwin

The compiler toolchain from Apple's Xcode environment uses an old version (1.38) of the GNU assembler which cannot assemble the optimized SHA-1 calculation in ppc/sha1ppc.S. The main problem is the use of calculated register numbers which gas 1.38 doesn't understand.

To create valid assembly code the registers in ppc/sha1ppc.in.S are represented by R[<register number>]. sha1ppc.in.S is postprocessed by gen_sha1ppc.pl to generate valid assembly code for gas 1.38.

---
 Makefile                        |    7 ++-
 ppc/gen_sha1ppc.pl              |   19 +++++++
 ppc/{sha1ppc.S => sha1ppc.in.S} |  110 +++++++++++++++++++-------------------
 3 files changed, 79 insertions(+), 57 deletions(-)
 create mode 100644 ppc/gen_sha1ppc.pl
 rename ppc/{sha1ppc.S => sha1ppc.in.S} (70%)
Makefile +5 -2
index ac29c62..01b69e7 100644
@@ -825,7 +825,7 @@ git$X git.spec \
 
 %.o: %.c GIT-CFLAGS
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $<
-%.o: %.S
+%.o: %.s
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $<
 
 exec_cmd.o: exec_cmd.c GIT-CFLAGS
@@ -836,6 +836,9 @@ builtin-init-db.o: builtin-init-db.c GIT-CFLAGS
 http.o: http.c GIT-CFLAGS
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $<
 
+ppc/sha1ppc.s: ppc/sha1ppc.in.S
+	$(QUIET_CC)$(CC) -c -E $< | $(PERL_PATH) ppc/gen_sha1ppc.pl > $@
+
 ifdef NO_EXPAT
 http-fetch.o: http-fetch.c http.h GIT-CFLAGS
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_EXPAT $<
@@ -1032,7 +1035,7 @@ dist-doc:
 ### Cleaning rules
 
 clean:
-	rm -f *.o mozilla-sha1/*.o arm/*.o ppc/*.o compat/*.o xdiff/*.o \
+	rm -f *.o mozilla-sha1/*.o arm/*.o ppc/*.[so] compat/*.o xdiff/*.o \
 		test-chmtime$X $(LIB_FILE) $(XDIFF_LIB)
 	rm -f $(ALL_PROGRAMS) $(BUILT_INS) git$X
 	rm -f *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags
ppc/gen_sha1ppc.pl new file +19 -0
new file mode 100644
index 0000000..79ba1a1
@@ -0,0 +1,19 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my %platform = (
+    # Special extra substitutions that have to be done on this platform
+    darwin => sub {
+        s{sha1_core}{_sha1_core};
+        s{;}{\n}g;
+    },
+);
+
+my $extra = exists $platform{$^O} ? $platform{$^O} : sub {};
+
+while (<>) {
+    $extra->();
+    s{R\[([^]]+)\]}{'r'.eval"$1"}ge;
+    print;
+}
ppc/sha1ppc.in.S renamed from ppc/sha1ppc.S +55 -55

186 lines not on this page · raw message

Signature · 1 line
1.5.1.rc3.29.gd8b6
#4 lore raw
Quoted 1 line · Arjen Laarhoven
 ppc/{sha1ppc.S => sha1ppc.in.S} |  110 +++++++++++++++++++-------------------

Wouldn't it be prettier if this filename was .S.in instead of .in.S? Additional file suffixes are usually added at the end (e.g. .tar.gz), and it makes more sense too.

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#5 lore raw

Hi,

Quoted 7 lines · Karl Hasselstr?m
Quoted 1 line · Arjen Laarhoven
 ppc/{sha1ppc.S => sha1ppc.in.S} |  110 +++++++++++++++++++-------------------

Wouldn't it be prettier if this filename was .S.in instead of .in.S? Additional file suffixes are usually added at the end (e.g. .tar.gz), and it makes more sense too.

Using the .S suffix makes gcc automatically do the right thing. .S.in requires an extra '-x assembler-with-cpp' option to gcc. Of course, it's trivial fix.

Arjen

#6 lore raw
Quoted 6 lines · Arjen Laarhoven
Quoted 4 lines · Karl Hasselström
Quoted 1 line · Arjen Laarhoven
 ppc/{sha1ppc.S => sha1ppc.in.S} |  110 +++++++++++++++++++-------------------
Quoted 7 lines
Quoted 3 lines

Wouldn't it be prettier if this filename was .S.in instead of .in.S? Additional file suffixes are usually added at the end (e.g. .tar.gz), and it makes more sense too.

Using the .S suffix makes gcc automatically do the right thing. .S.in requires an extra '-x assembler-with-cpp' option to gcc. Of course, it's trivial fix.

I just read the Makefile changes again, a bit slower this time, and noticed that you _first_ feed the .in.S file to gcc, and _then_ to the perl script, instead of the other way around like I was expecting. With that arrangement, your naming makes sense, since it reflects which file format is contained in which. Sorry for the noise.

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle