Skip to contentSkip to conversation

math128 - v2

PATCH In review Peter Zijlstra 2 participants 6 messages Apr 25, 2012

Subsystems (17)
Arm (32-bit) x86 PowerPC Library code +13 more subsystems
lore
Share & tools
lore
#1 lore raw

PATCH 1/2 math128: Introduce {mult,add,cmp}_u128

In reply to Peter Zijlstra

Grow rudimentary u128 support without relying on gcc/libgcc.

Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/alpha/include/asm/Kbuild      |    1 
 arch/arm/include/asm/Kbuild        |    1 
 arch/avr32/include/asm/Kbuild      |    1 
 arch/blackfin/include/asm/Kbuild   |    1 
 arch/c6x/include/asm/Kbuild        |    1 
 arch/cris/include/asm/Kbuild       |    1 
 arch/frv/include/asm/Kbuild        |    1 
 arch/h8300/include/asm/Kbuild      |    1 
 arch/hexagon/include/asm/Kbuild    |    1 
 arch/ia64/include/asm/Kbuild       |    1 
 arch/m32r/include/asm/Kbuild       |    1 
 arch/m68k/include/asm/Kbuild       |    1 
 arch/microblaze/include/asm/Kbuild |    1 
 arch/mips/include/asm/Kbuild       |    1 
 arch/mn10300/include/asm/Kbuild    |    1 
 arch/openrisc/include/asm/Kbuild   |    1 
 arch/parisc/include/asm/Kbuild     |    1 
 arch/powerpc/include/asm/Kbuild    |    1 
 arch/s390/include/asm/Kbuild       |    1 
 arch/score/include/asm/Kbuild      |    1 
 arch/sh/include/asm/Kbuild         |    1 
 arch/sparc/include/asm/Kbuild      |    1 
 arch/tile/include/asm/Kbuild       |    1 
 arch/um/include/asm/Kbuild         |    1 
 arch/unicore32/include/asm/Kbuild  |    1 
 arch/x86/include/asm/Kbuild        |    1 
 arch/xtensa/include/asm/Kbuild     |    1 
 include/asm-generic/math128.h      |    4 
 include/linux/math128.h            |  173 +++++++++++++++++++++++++++++++++++++
 lib/Makefile                       |    2 
 lib/math128.c                      |   40 ++++++++
 31 files changed, 245 insertions(+), 1 deletion(-)
arch/alpha/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ header-y += pal.h
 header-y += reg.h
 header-y += regdef.h
 header-y += sysinfo.h
+generic-y += math128.h
arch/arm/include/asm/Kbuild +1 -0
@@ -18,3 +18,4 @@ generic-y += resource.h
 generic-y += sections.h
 generic-y += siginfo.h
 generic-y += sizes.h
+generic-y += math128.h
arch/avr32/include/asm/Kbuild +1 -0
@@ -1,3 +1,4 @@
 include include/asm-generic/Kbuild.asm
 
 header-y	+= cachectl.h
+generic-y += math128.h
arch/blackfin/include/asm/Kbuild +1 -0
@@ -47,3 +47,4 @@ generic-y += xor.h
 header-y += bfin_sport.h
 header-y += cachectl.h
 header-y += fixed_code.h
+generic-y += math128.h
arch/c6x/include/asm/Kbuild +1 -0
@@ -51,3 +51,4 @@ generic-y += types.h
 generic-y += ucontext.h
 generic-y += user.h
 generic-y += vga.h
+generic-y += math128.h
arch/cris/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ header-y += etraxgpio.h
 header-y += rs485.h
 header-y += rtc.h
 header-y += sync_serial.h
+generic-y += math128.h
arch/frv/include/asm/Kbuild +1 -0
@@ -2,3 +2,4 @@ include include/asm-generic/Kbuild.asm
 
 header-y += registers.h
 header-y += termios.h
+generic-y += math128.h
arch/h8300/include/asm/Kbuild +1 -0
@@ -1 +1,2 @@
 include include/asm-generic/Kbuild.asm
+generic-y += math128.h
arch/hexagon/include/asm/Kbuild +1 -0
@@ -56,3 +56,4 @@ generic-y += types.h
 generic-y += ucontext.h
 generic-y += unaligned.h
 generic-y += xor.h
+generic-y += math128.h
arch/ia64/include/asm/Kbuild +1 -0
@@ -12,3 +12,4 @@ header-y += ptrace_offsets.h
 header-y += rse.h
 header-y += ucontext.h
 header-y += ustack.h
+generic-y += math128.h
arch/m32r/include/asm/Kbuild +1 -0
@@ -1 +1,2 @@
 include include/asm-generic/Kbuild.asm
+generic-y += math128.h
arch/m68k/include/asm/Kbuild +1 -0
@@ -1,2 +1,3 @@
 include include/asm-generic/Kbuild.asm
 header-y += cachectl.h
+generic-y += math128.h
arch/microblaze/include/asm/Kbuild +1 -0
@@ -1,3 +1,4 @@
 include include/asm-generic/Kbuild.asm
 
 header-y  += elf.h
+generic-y += math128.h
arch/mips/include/asm/Kbuild +1 -0
@@ -3,3 +3,4 @@ include include/asm-generic/Kbuild.asm
 header-y += cachectl.h
 header-y += sgidefs.h
 header-y += sysmips.h
+generic-y += math128.h
arch/mn10300/include/asm/Kbuild +1 -0
@@ -1 +1,2 @@
 include include/asm-generic/Kbuild.asm
+generic-y += math128.h
arch/openrisc/include/asm/Kbuild +1 -0
@@ -65,3 +65,4 @@ generic-y += topology.h
 generic-y += types.h
 generic-y += ucontext.h
 generic-y += user.h
+generic-y += math128.h
arch/parisc/include/asm/Kbuild +1 -0
@@ -1,3 +1,4 @@
 include include/asm-generic/Kbuild.asm
 
 header-y += pdc.h
+generic-y += math128.h
arch/powerpc/include/asm/Kbuild +1 -0
@@ -36,3 +36,4 @@ header-y += ucontext.h
 header-y += unistd.h
 
 generic-y += rwsem.h
+generic-y += math128.h
arch/s390/include/asm/Kbuild +1 -0
@@ -13,3 +13,4 @@ header-y += tape390.h
 header-y += ucontext.h
 header-y += vtoc.h
 header-y += zcrypt.h
+generic-y += math128.h
arch/score/include/asm/Kbuild +1 -0
@@ -1,3 +1,4 @@
 include include/asm-generic/Kbuild.asm
 
 header-y +=
+generic-y += math128.h
arch/sh/include/asm/Kbuild +1 -0
@@ -9,3 +9,4 @@ header-y += ptrace_32.h
 header-y += ptrace_64.h
 header-y += unistd_32.h
 header-y += unistd_64.h
+generic-y += math128.h
arch/sparc/include/asm/Kbuild +1 -0
@@ -21,3 +21,4 @@ generic-y += div64.h
 generic-y += local64.h
 generic-y += irq_regs.h
 generic-y += local.h
+generic-y += math128.h
arch/tile/include/asm/Kbuild +1 -0
@@ -42,3 +42,4 @@ generic-y += termios.h
 generic-y += types.h
 generic-y += ucontext.h
 generic-y += xor.h
+generic-y += math128.h
arch/um/include/asm/Kbuild +1 -0
@@ -2,3 +2,4 @@ generic-y += bug.h cputime.h device.h em
 generic-y += hw_irq.h irq_regs.h kdebug.h percpu.h sections.h topology.h xor.h
 generic-y += ftrace.h pci.h io.h param.h delay.h mutex.h current.h exec.h
 generic-y += switch_to.h
+generic-y += math128.h
arch/unicore32/include/asm/Kbuild +1 -0
@@ -58,3 +58,4 @@ generic-y += unaligned.h
 generic-y += user.h
 generic-y += vga.h
 generic-y += xor.h
+generic-y += math128.h
arch/x86/include/asm/Kbuild +1 -0
@@ -26,3 +26,4 @@ header-y += vsyscall.h
 genhdr-y += unistd_32.h
 genhdr-y += unistd_64.h
 genhdr-y += unistd_x32.h
+generic-y += math128.h
arch/xtensa/include/asm/Kbuild +1 -0
@@ -1 +1,2 @@
 include include/asm-generic/Kbuild.asm
+generic-y += math128.h
include/asm-generic/math128.h new file +4 -0
@@ -0,0 +1,4 @@
+#ifndef _ASM_GENERIC_MATH128_H
+#define _ASM_GENERIC_MATH128_H
+
+#endif /*_ASM_GENERIC_MATH128_H */
include/linux/math128.h new file +173 -0

173 lines not on this page · raw message

lib/Makefile +1 -1
@@ -12,7 +12,7 @@ lib-y := ctype.o string.o vsprintf.o cmd
 	 idr.o int_sqrt.o extable.o prio_tree.o \
 	 sha1.o md5.o irq_regs.o reciprocal_div.o argv_split.o \
 	 proportions.o prio_heap.o ratelimit.o show_mem.o \
-	 is_single_threaded.o plist.o decompress.o
+	 is_single_threaded.o plist.o decompress.o math128.o
 
 lib-$(CONFIG_MMU) += ioremap.o
 lib-$(CONFIG_SMP) += cpumask.o
lib/math128.c new file +40 -0
@@ -0,0 +1,40 @@
+#include <linux/math128.h>
+
+#ifndef mult_u64_u64
+/*
+ * a * b = (ah * 2^32 + al) * (bh * 2^32 + bl) =
+ *   ah*bh * 2^64 + (ah*bl + bh*al) * 2^32 + al*bl
+ */
+u128 mult_u64_u64(u64 a, u64 b)
+{
+	u128 t1, t2, t3, t4;
+	u32 ah, al;
+	u32 bh, bl;
+
+	ah = a >> 32;
+	al = a;
+
+	bh = b >> 32;
+	bl = b;
+
+	t1.lo = 0;
+	t1.hi = (u64)ah * bh;
+
+	t2.lo = (u64)ah * bl;
+	t2.hi = t2.lo >> 32;
+	t2.lo <<= 32;
+
+	t3.lo = (u64)al * bh;
+	t3.hi = t3.lo >> 32;
+	t3.lo <<= 32;
+
+	t4.lo = (u64)al * bl;
+	t4.hi = 0;
+
+	t1 = add_u128(t1, t2);
+	t1 = add_u128(t1, t3);
+	t1 = add_u128(t1, t4);
+
+	return t1;
+}
+#endif /* mult_u128 */
#3 lore raw

PATCH 2/2 math128, x86_64: Implement {mult,add}_u128 in 64bit asm

In reply to Peter Zijlstra
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/include/asm/Kbuild    |    1 -
 arch/x86/include/asm/math128.h |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
arch/x86/include/asm/Kbuild +0 -1
@@ -26,4 +26,3 @@ header-y += vsyscall.h
 genhdr-y += unistd_32.h
 genhdr-y += unistd_64.h
 genhdr-y += unistd_x32.h
-generic-y += math128.h
arch/x86/include/asm/math128.h new file +39 -0
@@ -0,0 +1,39 @@
+#ifndef _ASM_MATH128_H
+#define _ASM_MATH128_H
+
+#ifdef CONFIG_X86_64
+
+#ifdef __SIZEOF_INT128__
+#define ARCH_HAS_INT128
+#endif
+
+#ifndef ARCH_HAS_INT128
+
+static inline mul_u64_u64(u64 a, u64 b)
+{
+       u128 res;
+
+       asm("mulq %2"
+               : "=a" (res.lo), "=d" (res.hi)
+               :  "rm" (b), "0" (a));
+
+       return res;
+}
+#define mult_u64_u64 mult_u64_u64
+
+static inline add_u128(u128 a, u128 b)
+{
+       u128 res;
+
+       asm("addq %2,%0;\n"
+           "adcq %3,%1;\n"
+               : "=rm" (res.lo), "=rm" (res.hi)
+               : "r" (b.lo), "r" (b.hi), "0" (a.lo), "1" (a.hi));
+
+       return res;
+}
+#define add_u128 add_u128
+
+#endif /* ARCH_HAS_INT128 */
+#endif /* CONFIG_X86_64 */
+#endif /* _ASM_MATH128_H */
#0 lore raw

cover math128 - v2

Take two of the math128 bits..

Changes since last time:
- Added gcc __int128 versions for where it works
- Added Andy's mul_u64_u32_shr() due to popular demand :-)
- Added {shl,shr}_u128 as requested by hpa (although I suspect he'll use Andy's fancy function).
- Added a U128_INIT() constant initializer.
- Renamed mult_u128 to mul_u64_u64, hpa complaned the name was misleading
- Fixed some of the code generation issues with add_u128 and mul_u64_64.

I've also assumed we're all actually ok with adding this functionality, so unless someone hollers I'll push this for inclusion.

#2 lore raw

Re: [PATCH 1/2] math128: Introduce {mult,add,cmp}_u128

In reply to Peter Zijlstra

include/linux/math128.h line 39

Quoted 8 lines · Peter Zijlstra
include/linux/math128.h new file +8 -0
+static inline u128 add_u128(u128 a, u128 b)
+{
+       a.lo += b.lo;
+       if (a.lo < b.lo)
+               a.hi++;
+
+       return a;
+} 

D'0h I lost a .hi add there..

#4 lore raw

Re: [PATCH 2/2] math128, x86_64: Implement {mult,add}_u128 in 64bit asm

In reply to Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>

---
 arch/x86/include/asm/Kbuild    |    1 - arch/x86/include/asm/math128.h |   39 +++++++++++++++++++++++++++++++++++++++

 2 files changed, 39 insertions(+), 1 deletion(-)
arch/x86/include/asm/math128.h new file +2 -0
@@ -0,0 +1,39 @@
+#ifndef _ASM_MATH128_H
+#define _ASM_MATH128_H

arch/x86/include/asm/math128.h line 12

Quoted 10 lines
arch/x86/include/asm/math128.h new file +10 -0
+
+#ifdef CONFIG_X86_64
+
+#ifdef __SIZEOF_INT128__
+#define ARCH_HAS_INT128
+#endif
+
+#ifndef ARCH_HAS_INT128
+
+static inline mul_u64_u64(u64 a, u64 b)

I think you're missing a return type and a 't'.

arch/x86/include/asm/math128.h line 24

Quoted 12 lines
arch/x86/include/asm/math128.h new file +12 -0
+{
+       u128 res;
+
+       asm("mulq %2"
+               : "=a" (res.lo), "=d" (res.hi)
+               :  "rm" (b), "0" (a));
+
+       return res;
+}
+#define mult_u64_u64 mult_u64_u64
+
+static inline add_u128(u128 a, u128 b)

Return type here, too.

--Andy

#5 lore raw

Re: [PATCH 2/2] math128, x86_64: Implement {mult,add}_u128 in 64bit asm

Quoted 4 lines · Andy Lutomirski
Quoted 2 lines

+static inline mul_u64_u64(u64 a, u64 b)

I think you're missing a return type and a 't'.

argh, that's what you get for not actually compiling with an old enough gcc :/

Fixed, thanks!