int128: optimize and add test cases
PATCH v2 1/1 int128: optimize and add test cases
For add, the carry only requires checking one of the arguments. For sub and neg, we can similarly optimize computation of the carry.
For ge, we can just do lexicographic order.
Signed-off-by Paolo Bonzini <[email protected]>
--- include/qemu/int128.h | 25 ++++-- tests/Makefile | 6 +- tests/test-int128.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 tests/test-int128.c
include/qemu/int128.h +17 -8
@@ -1,6 +1,10 @@#ifndef INT128_H #define INT128_H +#include <assert.h> +#include <stdint.h> +#include <stdbool.h> +typedef struct Int128 Int128 ; struct Int128 {
@@ -55,21 +59,26 @@ static inline Int128 int128_rshift(Int128 a, int n)static inline Int128 int128_add (Int128 a,Int128 b) { -Int128 r = { a.lo + b.lo, a.hi + b.hi }; - r.hi += (r.lo < a.lo) || (r.lo < b.lo); -return r; +uint64_t lo = a.lo + b.lo; + +/* a.lo <= a.lo + b.lo < a.lo + k (k is the base, 2^64). Hence, +* a.lo + b.lo >= k implies 0 <= lo = a.lo + b.lo - k < a.lo. +* Similarly, a.lo + b.lo < k implies a.lo <= lo = a.lo + b.lo < k. +* +* So the carry is lo < a.lo. +*/ +return (Int128 ) { lo, (uint64_t )a.hi + b.hi + (lo < a.lo) }; }static inline Int128 int128_neg (Int128 a) { - a.lo = ~a.lo; - a.hi = ~a.hi; -return int128_add (a,int128_one ()); +uint64_t lo = -a.lo; +return (Int128 ) { lo, ~(uint64_t )a.hi + !lo }; }static inline Int128 int128_sub (Int128 a,Int128 b) { -return int128_add (a,int128_neg (b)); +return (Int128 ){ a.lo - b.lo, a.hi - b.hi - (a.lo < b.lo) }; }static inline bool int128_nonneg (Int128 a)
@@ -89,7 +98,7 @@ static inline bool int128_ne(Int128 a, Int128 b)static inline bool int128_ge (Int128 a,Int128 b) { -return int128_nonneg (int128_sub (a, b)); +return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo); }static inline bool int128_lt (Int128 a,Int128 b)
tests/Makefile +5 -1
@@ -44,6 +44,9 @@ check-unit-y += tests/test-cutils$(EXESUF) gcov-files-test-cutils-y += util/cutils.c check-unit-y += tests/test-mul64$(EXESUF) gcov-files-test-mul64-y = util/host-utils.c +check-unit-y += tests/test-int128$(EXESUF) +# all code tested by test-int128 is inside int128.h +gcov-files-test-int128-y = check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
@@ -75,7 +78,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \ tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \ tests/test-qmp-commands.o tests/test-visitor-serialization.o \ - tests/test-x86-cpuid.o tests/test-mul64.o + tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o
@@ -98,6 +101,7 @@ tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o libqemuutil.a libqemustub.a tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o xbzrle.o page_cache.o libqemuutil.a tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o +tests/test-int128$(EXESUF): tests/test-int128.o tests/test-qapi-types.c tests/test-qapi-types.h :\ $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-int128.c new file +212 -0
212 lines not on this page · raw message
Signature · 1 line
1.8.1.4
tests/test-int128.c line 2
Quoted 5 lines · Paolo Bonzini
tests/test-int128.c new file +2
-0@@ -0,0 +1,212 @@ +/* +* Test 64x64 -> 128 multiply subroutines
Cutnpaste test description. Otherwise,
Reviewed-by Richard Henderson <[email protected]>
r~
Il 21/06/2013 17:10, Richard Henderson ha scritto:
Quoted 10 lines · Paolo Bonzini
tests/test-int128.c line 2
Quoted 5 lines
tests/test-int128.c new file +2
-0@@ -0,0 +1,212 @@ +/* +* Test 64x64 -> 128 multiply subroutines Cutnpaste test description. Otherwise,
Reviewed-by Richard Henderson <[email protected]>
Ok, I'll fix it and add it to my queue.
Paolo