]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Move summation to a separate routine
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 5 Mar 2022 20:36:40 +0000 (20:36 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 5 Mar 2022 20:36:40 +0000 (20:36 +0000)
src/libutil/util.c
src/libutil/util.h

index e7348982bcb8016337adf3184afeada796a41d49..44f40280b5af9fdfe8c0f3fc9f9fecf2c6438f11 100644 (file)
@@ -2456,3 +2456,26 @@ rspamd_ptr_array_shuffle (GPtrArray *ar)
                g_ptr_array_index (ar, i) = t;
        }
 }
+
+float
+rspamd_sum_floats (float *ar, gsize *nelts)
+{
+       float sum = 0.0f;
+       volatile float c = 0.0f; /* We don't want any optimisations around c */
+       gsize cnt = 0;
+
+       for (gsize i = 0; i < *nelts; i ++) {
+               float elt = ar[i];
+
+               if (!isnan(elt)) {
+                       cnt ++;
+                       float y = elt - c;
+                       float t = sum + y;
+                       c = (t - sum) - y;
+                       sum = t;
+               }
+       }
+
+       *nelts = cnt;
+       return sum;
+}
index 0eff1c990cc52d6d9fee1304e11c9c7971d3f54b..1d53807d4294627edc1d345aa0bbdd6ea06d7b77 100644 (file)
@@ -518,6 +518,14 @@ struct rspamd_controller_pbkdf {
 
 extern const struct rspamd_controller_pbkdf pbkdf_list[];
 
+/**
+ * Sum array of floats using Kahan sum algorithm
+ * @param ar
+ * @param nelts
+ * @return
+ */
+float rspamd_sum_floats (float *ar, gsize *nelts);
+
 #ifdef  __cplusplus
 }
 #endif