From patchwork Mon Jul 6 12:35:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lespiau, Damien" X-Patchwork-Id: 6723261 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 037679F3A0 for ; Mon, 6 Jul 2015 12:35:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2A03220601 for ; Mon, 6 Jul 2015 12:35:51 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id CE881202F0 for ; Mon, 6 Jul 2015 12:35:49 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 540B86E86A; Mon, 6 Jul 2015 05:35:49 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTP id 18B676E86A for ; Mon, 6 Jul 2015 05:35:48 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP; 06 Jul 2015 05:35:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,414,1432623600"; d="scan'208";a="741420980" Received: from magarwal-mobl.amr.corp.intel.com (HELO strange.amr.corp.intel.com) ([10.254.77.111]) by fmsmga001.fm.intel.com with ESMTP; 06 Jul 2015 05:35:47 -0700 From: Damien Lespiau To: intel-gfx@lists.freedesktop.org Date: Mon, 6 Jul 2015 13:35:29 +0100 Message-Id: <1436186144-19665-2-git-send-email-damien.lespiau@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1436186144-19665-1-git-send-email-damien.lespiau@intel.com> References: <1436186144-19665-1-git-send-email-damien.lespiau@intel.com> Subject: [Intel-gfx] [PATCH i-g-t 01/16] stats: Add a way to generate values following a normal distribution X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Damien Lespiau --- lib/igt_stats.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/igt_stats.h | 3 +++ 2 files changed, 71 insertions(+) diff --git a/lib/igt_stats.c b/lib/igt_stats.c index 70650ec..37fcc23 100644 --- a/lib/igt_stats.c +++ b/lib/igt_stats.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "igt_core.h" #include "igt_stats.h" @@ -132,6 +133,73 @@ void igt_stats_init_with_size(igt_stats_t *stats, unsigned int capacity) stats->max = 0; } +/* https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform */ +static double rand_normal(double mean, double stddev) +{ + static double n2 = 0.0; + static bool n2_cached = 0; + double d, n1; + + if (!n2_cached) { + double x, y, r; + + do { + x = 2.0 * rand() / RAND_MAX - 1; + y = 2.0 * rand() / RAND_MAX - 1; + + r = x * x + y * y; + } while (r == 0.0 || r > 1.0); + + d = sqrt(-2.0 * log(r) / r); + n1 = x * d; + n2 = y * d; + + n2_cached = 1; + return n1 * stddev + mean; + } else { + n2_cached = 0; + return n2 * stddev + mean; + } +} + +static double * +igt_vector_normal(double mean, double sigma, unsigned int n_values) +{ + double *v; + unsigned int i; + + v = malloc(n_values * sizeof(double)); + srand(time(NULL)); + for (i = 0; i < n_values; i++) + v[i] = rand_normal(mean, sigma); + + return v; +} + +/** + * igt_stats_init_normal: + * @stats: An #igt_stats_t instance + * @mean: Mean of the distribution + * @std_deviation: Standard deviation of the distribution + * @n_values: Number of values to generate + * + * Generates @n_values numbers following a normal distribution defined by + * (@mean, @std_deviation). + */ +void igt_stats_init_normal(igt_stats_t *stats, + double mean, double std_deviation, + unsigned int n_values) +{ + double *v; + unsigned int i; + + v = igt_vector_normal(mean, std_deviation, n_values); + igt_stats_init_with_size(stats, n_values); + for (i = 0; i < n_values; i++) + igt_stats_push(stats, v[i]); + free(v); +} + /** * igt_stats_fini: * @stats: An #igt_stats_t instance diff --git a/lib/igt_stats.h b/lib/igt_stats.h index 554ab15..e1757b7 100644 --- a/lib/igt_stats.h +++ b/lib/igt_stats.h @@ -49,6 +49,9 @@ typedef struct { void igt_stats_init(igt_stats_t *stats); void igt_stats_init_with_size(igt_stats_t *stats, unsigned int capacity); +void igt_stats_init_normal(igt_stats_t *stats, + double mean, double std_deviation, + unsigned int n_values); void igt_stats_fini(igt_stats_t *stats); bool igt_stats_is_population(igt_stats_t *stats); void igt_stats_set_population(igt_stats_t *stats, bool full_population);