2 * Copyright IBM Corp. 2008
4 * Author(s): Martin Peschke <mp3@de.ibm.com>
5 * Stefan Raspl <stefan.raspl@de.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/types.h>
36 static inline void minmax_init(struct minmax *mm)
45 static inline void minmax_account(struct minmax *mm, __u64 value)
48 mm->sos += value * value;
56 static inline void minmax_merge(struct minmax *dst, struct minmax *src)
60 if (src->min < dst->min)
62 if (src->max > dst->max)
67 static inline void minmax_to_be(struct minmax *mm)
69 mm->sum = cpu_to_be64(mm->sum);
70 mm->sos = cpu_to_be64(mm->sos);
71 mm->min = cpu_to_be64(mm->min);
72 mm->max = cpu_to_be64(mm->max);
73 mm->num = cpu_to_be64(mm->num);
76 static inline double minmax_avg(struct minmax *mm)
81 return (mm->sum / (double)mm->num);
84 static inline double minmax_var(struct minmax *mm)
86 double num = (double)mm->num;
91 return ((mm->sos - ((mm->sum * mm->sum) / num)) / num);
94 static inline int minmax_print(FILE *fp, const char *s, struct minmax *mm)
96 return fprintf(fp, "%s: num %Ld, min %Ld, max %Ld, sum %Ld, squ %Ld, "
97 "avg %.1f, var %.1f\n", s, (unsigned long long)mm->num,
98 (unsigned long long)mm->min, (unsigned long long)mm->max,
99 (unsigned long long)mm->sum, (unsigned long long)mm->sos,
100 minmax_avg(mm), minmax_var(mm));
109 static inline __u64 histlog2_upper_limit(int index, struct histlog2 *h)
111 return h->first + (index ? h->delta << (index - 1) : 0);
114 static inline int histlog2_index(__u64 val, struct histlog2 *h)
118 for (i = 0; i < (h->num - 1) && val > histlog2_upper_limit(i, h); i++);
122 static inline void histlog2_account(__u32 *bucket, __u32 val,
125 int index = histlog2_index(val, h);
129 static inline void histlog2_merge(struct histlog2 *h, __u32 *dst, __u32 *src)
133 for (i = 0; i < h->num; i++)
137 static inline void histlog2_to_be(__u32 a[], struct histlog2 *h)
141 for (i = 0; i < h->num; i++)
142 a[i] = cpu_to_be32(a[i]);
145 static inline void histlog2_print(FILE *fp, const char *s, __u32 a[],
150 fprintf(fp, "%s:\n", s);
151 for (i = 0; i < h->num - 1; i++) {
152 fprintf(fp, " %10ld:%6d",
153 (unsigned long)(histlog2_upper_limit(i, h)), a[i]);
157 fprintf(fp, " >%8ld:%6d\n",
158 (unsigned long)(histlog2_upper_limit(i - 1, h)), a[i]);