From nobody Tue Feb 10 08:27:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1522884195171306.33028601866863; Wed, 4 Apr 2018 16:23:15 -0700 (PDT) Received: from localhost ([::1]:37990 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f3rkI-0004oI-2B for importer@patchew.org; Wed, 04 Apr 2018 19:23:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54471) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f3rZZ-0004Jx-53 for qemu-devel@nongnu.org; Wed, 04 Apr 2018 19:12:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f3rZW-000443-W8 for qemu-devel@nongnu.org; Wed, 04 Apr 2018 19:12:09 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:33479) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f3rZW-00043w-Q4 for qemu-devel@nongnu.org; Wed, 04 Apr 2018 19:12:06 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 4002621B4F; Wed, 4 Apr 2018 19:11:17 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 04 Apr 2018 19:11:17 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id D1EEA10259; Wed, 4 Apr 2018 19:11:16 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=mesmtp; bh=qDVhauQvTQV+Ik Gp4X87vNV7mJwSbmBajOLIzRgofO0=; b=wdym+MkFYDQZnaUjNZTXG6R8jhWW+n hIxFGNSFdiklwdPxs4TV91TvLSaqkzi/5/P5EiPnr2DyQCoUNOcuT7jQ2THuHEXx fSM6PVcgytHgKJgwVN1fccyW2rIhj61O6i3/JmCiZWPjdPc4Ncudd5lgTwEpWDR5 HAIp/MAU29t6Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=qDVhauQvTQV+IkGp4X87vNV7mJwSbmBajOLIzRgofO0=; b=ZbslL+H/ pZW75yldC7kJnvV9syrkjY5+BRT+G83JOPbAvNxK5vBnWRxDqbWiBPk72AFi3No3 WJdQLnhXeF32q5Of2X7fd5mT2k7BhjclRH9Ro4wNO0UxKXyYZi8rY+UL4jSKszRd poMRUneXy80biQ4SKifNVtmL0QSDk4sP5F+Mq0EhWQS0aBw355lwntvaLaMlfTb3 Mzddsq3QOWy5ajjpH/Pj1Urivpm0HnoUxATxi5vVKxRmI6r5uSsyWfCa08Movnld psuDNsguxEQg8muAeuqGvp97clHVjit/kl4jmC3bFdugB9BwilqQ0fQxHXe2QPyx JWXeswCjXQphEw== X-ME-Sender: From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Wed, 4 Apr 2018 19:11:06 -0400 Message-Id: <1522883475-27858-7-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1522883475-27858-1-git-send-email-cota@braap.org> References: <1522883475-27858-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.111.4.29 Subject: [Qemu-devel] [PATCH v3 06/15] tests/fp: add fp-bench, a collection of simple floating point microbenchmarks X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Mark Cave-Ayland , Richard Henderson , Laurent Vivier , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Aurelien Jarno Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This will allow us to measure the performance impact of FP emulation optimizations. Note that we can measure both directly the impact on the softfloat functions (with "-t soft"), or the impact on an emulated workload (call with "-t host" and run under qemu user-mode). Signed-off-by: Emilio G. Cota --- tests/fp/fp-bench.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ tests/fp/.gitignore | 1 + tests/fp/Makefile | 4 +- 3 files changed, 532 insertions(+), 1 deletion(-) create mode 100644 tests/fp/fp-bench.c diff --git a/tests/fp/fp-bench.c b/tests/fp/fp-bench.c new file mode 100644 index 0000000..a012b78 --- /dev/null +++ b/tests/fp/fp-bench.c @@ -0,0 +1,528 @@ +/* + * fp-bench.c - A collection of simple floating point microbenchmarks. + * + * Copyright (C) 2018, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#ifndef HW_POISON_H +#error Must define HW_POISON_H to work around TARGET_* poisoning +#endif + +#include "qemu/osdep.h" +#include "qemu/timer.h" + +#include "fpu/softfloat.h" + +#include + +/* amortize the computation of random inputs */ +#define OPS_PER_ITER 50000 + +#define MAX_OPERANDS 3 + +#define SEED_A 0xdeadfacedeadface +#define SEED_B 0xbadc0feebadc0fee +#define SEED_C 0xbeefdeadbeefdead + +enum op { + OP_ADD, + OP_SUB, + OP_MUL, + OP_DIV, + OP_FMA, + OP_SQRT, + OP_CMP, + OP_MAX_NR, +}; + +static const char * const op_names[] =3D { + [OP_ADD] =3D "add", + [OP_SUB] =3D "sub", + [OP_MUL] =3D "mul", + [OP_DIV] =3D "div", + [OP_FMA] =3D "fma", + [OP_SQRT] =3D "sqrt", + [OP_CMP] =3D "cmp", + [OP_MAX_NR] =3D NULL, +}; + +enum precision { + PREC_SINGLE, + PREC_DOUBLE, + PREC_FLOAT32, + PREC_FLOAT64, + PREC_MAX_NR, +}; + +enum tester { + TESTER_SOFT, + TESTER_HOST, + TESTER_MAX_NR, +}; + +static const char * const tester_names[] =3D { + [TESTER_SOFT] =3D "soft", + [TESTER_HOST] =3D "host", + [TESTER_MAX_NR] =3D NULL, +}; + +union fp { + float f; + double d; + float32 f32; + float64 f64; + uint64_t u64; +}; + +struct op_state; + +typedef float (*float_func_t)(const struct op_state *s); +typedef double (*double_func_t)(const struct op_state *s); + +union fp_func { + float_func_t float_func; + double_func_t double_func; +}; + +typedef void (*bench_func_t)(void); + +struct op_desc { + const char * const name; +}; + +#define DEFAULT_DURATION_SECS 1 + +static uint64_t random_ops[MAX_OPERANDS] =3D { + SEED_A, SEED_B, SEED_C, +}; +static float_status soft_status; +static enum precision precision; +static enum op operation; +static enum tester tester; +static uint64_t n_completed_ops; +static unsigned int duration =3D DEFAULT_DURATION_SECS; +static int64_t ns_elapsed; +/* disable optimizations with volatile */ +static volatile union fp res; + +/* + * From: https://en.wikipedia.org/wiki/Xorshift + * This is faster than rand_r(), and gives us a wider range (RAND_MAX is o= nly + * guaranteed to be >=3D INT_MAX). + */ +static uint64_t xorshift64star(uint64_t x) +{ + x ^=3D x >> 12; /* a */ + x ^=3D x << 25; /* b */ + x ^=3D x >> 27; /* c */ + return x * UINT64_C(2685821657736338717); +} + +static void update_random_ops(int n_ops, enum precision prec) +{ + int i; + + for (i =3D 0; i < n_ops; i++) { + uint64_t r =3D random_ops[i]; + + if (prec =3D=3D PREC_SINGLE || PREC_FLOAT32) { + do { + r =3D xorshift64star(r); + } while (!float32_is_normal(r)); + } else if (prec =3D=3D PREC_DOUBLE || PREC_FLOAT64) { + do { + r =3D xorshift64star(r); + } while (!float64_is_normal(r)); + } else { + g_assert_not_reached(); + } + random_ops[i] =3D r; + } +} + +static void fill_random(union fp *ops, int n_ops, enum precision prec, + bool no_neg) +{ + int i; + + for (i =3D 0; i < n_ops; i++) { + switch (prec) { + case PREC_SINGLE: + case PREC_FLOAT32: + ops[i].f32 =3D make_float32(random_ops[i]); + if (no_neg && float32_is_neg(ops[i].f32)) { + ops[i].f32 =3D float32_chs(ops[i].f32); + } + /* raise the exponent to limit the frequency of denormal resul= ts */ + ops[i].f32 |=3D 0x40000000; + break; + case PREC_DOUBLE: + case PREC_FLOAT64: + ops[i].f64 =3D make_float64(random_ops[i]); + if (no_neg && float64_is_neg(ops[i].f64)) { + ops[i].f64 =3D float64_chs(ops[i].f64); + } + /* raise the exponent to limit the frequency of denormal resul= ts */ + ops[i].f64 |=3D LIT64(0x4000000000000000); + break; + default: + g_assert_not_reached(); + } + } +} + +/* + * The main benchmark function. Instead of (ab)using macros, we rely + * on the compiler to unfold this at compile-time. + */ +static void bench(enum precision prec, enum op op, int n_ops, bool no_neg) +{ + int64_t tf =3D get_clock_realtime() + duration * 1000000000LL; + + while (get_clock_realtime() < tf) { + union fp ops[MAX_OPERANDS]; + int64_t t0; + int i; + + update_random_ops(n_ops, prec); + switch (prec) { + case PREC_SINGLE: + fill_random(ops, n_ops, prec, no_neg); + t0 =3D get_clock_realtime(); + for (i =3D 0; i < OPS_PER_ITER; i++) { + float a =3D ops[0].f; + float b =3D ops[1].f; + float c =3D ops[2].f; + + switch (op) { + case OP_ADD: + res.f =3D a + b; + break; + case OP_SUB: + res.f =3D a - b; + break; + case OP_MUL: + res.f =3D a * b; + break; + case OP_DIV: + res.f =3D a / b; + break; + case OP_FMA: + res.f =3D fmaf(a, b, c); + break; + case OP_SQRT: + res.f =3D sqrtf(a); + break; + case OP_CMP: + res.u64 =3D isgreater(a, b); + break; + default: + g_assert_not_reached(); + } + } + break; + case PREC_DOUBLE: + fill_random(ops, n_ops, prec, no_neg); + t0 =3D get_clock_realtime(); + for (i =3D 0; i < OPS_PER_ITER; i++) { + double a =3D ops[0].d; + double b =3D ops[1].d; + double c =3D ops[2].d; + + switch (op) { + case OP_ADD: + res.d =3D a + b; + break; + case OP_SUB: + res.d =3D a - b; + break; + case OP_MUL: + res.d =3D a * b; + break; + case OP_DIV: + res.d =3D a / b; + break; + case OP_FMA: + res.d =3D fma(a, b, c); + break; + case OP_SQRT: + res.d =3D sqrt(a); + break; + case OP_CMP: + res.u64 =3D isgreater(a, b); + break; + default: + g_assert_not_reached(); + } + } + break; + case PREC_FLOAT32: + fill_random(ops, n_ops, prec, no_neg); + t0 =3D get_clock_realtime(); + for (i =3D 0; i < OPS_PER_ITER; i++) { + float32 a =3D ops[0].f32; + float32 b =3D ops[1].f32; + float32 c =3D ops[2].f32; + + switch (op) { + case OP_ADD: + res.f32 =3D float32_add(a, b, &soft_status); + break; + case OP_SUB: + res.f32 =3D float32_sub(a, b, &soft_status); + break; + case OP_MUL: + res.f =3D float32_mul(a, b, &soft_status); + break; + case OP_DIV: + res.f32 =3D float32_div(a, b, &soft_status); + break; + case OP_FMA: + res.f32 =3D float32_muladd(a, b, c, 0, &soft_status); + break; + case OP_SQRT: + res.f32 =3D float32_sqrt(a, &soft_status); + break; + case OP_CMP: + res.u64 =3D float32_compare_quiet(a, b, &soft_status); + break; + default: + g_assert_not_reached(); + } + } + break; + case PREC_FLOAT64: + fill_random(ops, n_ops, prec, no_neg); + t0 =3D get_clock_realtime(); + for (i =3D 0; i < OPS_PER_ITER; i++) { + float64 a =3D ops[0].f64; + float64 b =3D ops[1].f64; + float64 c =3D ops[2].f64; + + switch (op) { + case OP_ADD: + res.f64 =3D float64_add(a, b, &soft_status); + break; + case OP_SUB: + res.f64 =3D float64_sub(a, b, &soft_status); + break; + case OP_MUL: + res.f =3D float64_mul(a, b, &soft_status); + break; + case OP_DIV: + res.f64 =3D float64_div(a, b, &soft_status); + break; + case OP_FMA: + res.f64 =3D float64_muladd(a, b, c, 0, &soft_status); + break; + case OP_SQRT: + res.f64 =3D float64_sqrt(a, &soft_status); + break; + case OP_CMP: + res.u64 =3D float64_compare_quiet(a, b, &soft_status); + break; + default: + g_assert_not_reached(); + } + } + break; + default: + g_assert_not_reached(); + } + ns_elapsed +=3D get_clock_realtime() - t0; + n_completed_ops +=3D OPS_PER_ITER; + } +} + +#define GEN_BENCH(name, type, prec, op, n_ops) \ + static void __attribute__((flatten)) name(void) \ + { \ + bench(prec, op, n_ops, false); \ + } + +#define GEN_BENCH_NO_NEG(name, type, prec, op, n_ops) \ + static void __attribute__((flatten)) name(void) \ + { \ + bench(prec, op, n_ops, true); \ + } + +#define GEN_BENCH_ALL_TYPES(opname, op, n_ops) \ + GEN_BENCH(bench_ ## opname ## _float, float, PREC_SINGLE, op, n_ops) \ + GEN_BENCH(bench_ ## opname ## _double, double, PREC_DOUBLE, op, n_ops)= \ + GEN_BENCH(bench_ ## opname ## _float32, float32, PREC_FLOAT32, op, n_o= ps) \ + GEN_BENCH(bench_ ## opname ## _float64, float64, PREC_FLOAT64, op, n_o= ps) + +GEN_BENCH_ALL_TYPES(add, OP_ADD, 2) +GEN_BENCH_ALL_TYPES(sub, OP_SUB, 2) +GEN_BENCH_ALL_TYPES(mul, OP_MUL, 2) +GEN_BENCH_ALL_TYPES(div, OP_DIV, 2) +GEN_BENCH_ALL_TYPES(fma, OP_FMA, 3) +GEN_BENCH_ALL_TYPES(cmp, OP_CMP, 2) +#undef GEN_BENCH_ALL_TYPES + +#define GEN_BENCH_ALL_TYPES_NO_NEG(name, op, n) \ + GEN_BENCH_NO_NEG(bench_ ## name ## _float, float, PREC_SINGLE, op, n) \ + GEN_BENCH_NO_NEG(bench_ ## name ## _double, double, PREC_DOUBLE, op, n= ) \ + GEN_BENCH_NO_NEG(bench_ ## name ## _float32, float32, PREC_FLOAT32, op= , n) \ + GEN_BENCH_NO_NEG(bench_ ## name ## _float64, float64, PREC_FLOAT64, op= , n) + +GEN_BENCH_ALL_TYPES_NO_NEG(sqrt, OP_SQRT, 1) +#undef GEN_BENCH_ALL_TYPES_NO_NEG + +#undef GEN_BENCH_NO_NEG +#undef GEN_BENCH + +#define GEN_BENCH_FUNCS(opname, op) \ + [op] =3D { \ + [PREC_SINGLE] =3D bench_ ## opname ## _float, \ + [PREC_DOUBLE] =3D bench_ ## opname ## _double, \ + [PREC_FLOAT32] =3D bench_ ## opname ## _float32, \ + [PREC_FLOAT64] =3D bench_ ## opname ## _float64, \ + } + +static const bench_func_t bench_funcs[OP_MAX_NR][PREC_MAX_NR] =3D { + GEN_BENCH_FUNCS(add, OP_ADD), + GEN_BENCH_FUNCS(sub, OP_SUB), + GEN_BENCH_FUNCS(mul, OP_MUL), + GEN_BENCH_FUNCS(div, OP_DIV), + GEN_BENCH_FUNCS(fma, OP_FMA), + GEN_BENCH_FUNCS(sqrt, OP_SQRT), + GEN_BENCH_FUNCS(cmp, OP_CMP), +}; + +#undef GEN_BENCH_FUNCS + +static void run_bench(void) +{ + bench_func_t f; + + f =3D bench_funcs[operation][precision]; + g_assert(f); + f(); +} + +/* @arr must be NULL-terminated */ +static int find_name(const char * const *arr, const char *name) +{ + int i; + + for (i =3D 0; arr[i] !=3D NULL; i++) { + if (strcmp(name, arr[i]) =3D=3D 0) { + return i; + } + } + return -1; +} + +static void usage_complete(int argc, char *argv[]) +{ + gchar *op_list =3D g_strjoinv(", ", (gchar **)op_names); + gchar *tester_list =3D g_strjoinv(", ", (gchar **)tester_names); + + fprintf(stderr, "Usage: %s [options]\n", argv[0]); + fprintf(stderr, "options:\n"); + fprintf(stderr, " -d =3D duration, in seconds. Default: %d\n", + DEFAULT_DURATION_SECS); + fprintf(stderr, " -h =3D show this help message.\n"); + fprintf(stderr, " -o =3D floating point operation (%s). Default: %s\n= ", + op_list, op_names[0]); + fprintf(stderr, " -p =3D floating point precision (single, double). " + "Default: single\n"); + fprintf(stderr, " -t =3D tester (%s). Default: %s\n", + tester_list, tester_names[0]); + fprintf(stderr, " -z =3D flush inputs to zero (soft tester only). " + "Default: disabled\n"); + fprintf(stderr, " -Z =3D flush output to zero (soft tester only). " + "Default: disabled\n"); + + g_free(tester_list); + g_free(op_list); +} + +static void parse_args(int argc, char *argv[]) +{ + int c; + int val; + + for (;;) { + c =3D getopt(argc, argv, "d:ho:p:t:zZ"); + if (c < 0) { + break; + } + switch (c) { + case 'd': + duration =3D atoi(optarg); + break; + case 'h': + usage_complete(argc, argv); + exit(EXIT_SUCCESS); + case 'o': + val =3D find_name(op_names, optarg); + if (val < 0) { + fprintf(stderr, "Unsupported op '%s'\n", optarg); + exit(EXIT_FAILURE); + } + operation =3D val; + break; + case 'p': + if (!strcmp(optarg, "single")) { + precision =3D PREC_SINGLE; + } else if (!strcmp(optarg, "double")) { + precision =3D PREC_DOUBLE; + } else { + fprintf(stderr, "Unsupported precision '%s'\n", optarg); + exit(EXIT_FAILURE); + } + break; + case 't': + val =3D find_name(tester_names, optarg); + if (val < 0) { + fprintf(stderr, "Unsupported tester '%s'\n", optarg); + exit(EXIT_FAILURE); + } + tester =3D val; + break; + case 'z': + soft_status.flush_inputs_to_zero =3D 1; + break; + case 'Z': + soft_status.flush_to_zero =3D 1; + break; + } + } + + /* set precision based on the tester */ + switch (tester) { + case TESTER_HOST: + break; + case TESTER_SOFT: + switch (precision) { + case PREC_SINGLE: + precision =3D PREC_FLOAT32; + break; + case PREC_DOUBLE: + precision =3D PREC_FLOAT64; + break; + default: + g_assert_not_reached(); + } + break; + default: + g_assert_not_reached(); + } +} + +static void pr_stats(void) +{ + printf("%.2f MFlops\n", (double)n_completed_ops / ns_elapsed * 1e3); +} + +int main(int argc, char *argv[]) +{ + parse_args(argc, argv); + run_bench(); + pr_stats(); + return 0; +} diff --git a/tests/fp/.gitignore b/tests/fp/.gitignore index 0a9fef4..a4e59d7 100644 --- a/tests/fp/.gitignore +++ b/tests/fp/.gitignore @@ -1,3 +1,4 @@ ibm *.txt fp-test +fp-bench diff --git a/tests/fp/Makefile b/tests/fp/Makefile index a208f4c..7c88ab0 100644 --- a/tests/fp/Makefile +++ b/tests/fp/Makefile @@ -12,7 +12,7 @@ QEMU_CFLAGS +=3D -DHW_POISON_H =20 IBMFP :=3D ibm-fptests.zip =20 -OBJS :=3D fp-test$(EXESUF) +OBJS :=3D fp-test$(EXESUF) fp-bench$(EXESUF) =20 WHITELIST_FILES :=3D whitelist.txt whitelist-tininess-after.txt =20 @@ -30,5 +30,7 @@ $(WHITELIST_FILES): =20 fp-test$(EXESUF): fp-test.o softfloat.o =20 +fp-bench$(EXESUF): fp-bench.o softfloat.o + clean: rm -f *.o *.d $(OBJS) --=20 2.7.4