Not hooked up to FATE due to fear of random failures due to float
instability.

float_dsp-test: use 16 * 3 * 5 as length
---
 libavutil/Makefile    |   1 +
 libavutil/float_dsp.c | 274 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 275 insertions(+)

diff --git a/libavutil/Makefile b/libavutil/Makefile
index f663f18..5869e67 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -122,6 +122,7 @@ TESTPROGS = adler32                                         
            \
             des                                                         \
             eval                                                        \
             fifo                                                        \
+            float_dsp                                                   \
             hmac                                                        \
             lfg                                                         \
             lls                                                         \
diff --git a/libavutil/float_dsp.c b/libavutil/float_dsp.c
index 3707e06..22c3c15 100644
--- a/libavutil/float_dsp.c
+++ b/libavutil/float_dsp.c
@@ -132,3 +132,277 @@ av_cold void avpriv_float_dsp_init(AVFloatDSPContext 
*fdsp, int bit_exact)
     ff_float_dsp_init_x86(fdsp);
 #endif
 }
+
+#ifdef TEST
+
+#include <float.h>
+#include <math.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "cpu.h"
+#include "lfg.h"
+#include "log.h"
+#include "mem.h"
+#include "random_seed.h"
+
+#define LEN 240
+
+static void fill_float_array(AVLFG *lfg, float *a, int len)
+{
+        int i;
+        double bmg[2], stddev = 10.0, mean = 0.0;
+
+        for (i = 0; i < len; i += 2) {
+            av_bmg_get(lfg, bmg);
+            a[i]     = bmg[0] * stddev + mean;
+            a[i + 1] = bmg[1] * stddev + mean;
+        }
+}
+static int compare_floats(const float *a, const float *b, int len,
+                          float max_diff)
+{
+    int i;
+    for (i = 0; i < len; i++) {
+        if (fabsf(a[i] - b[i]) > max_diff) {
+            av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
+                   i, a[i], b[i], a[i] - b[i]);
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static void fill_double_array(AVLFG *lfg, double *a, int len)
+{
+        int i;
+        double bmg[2], stddev = 10.0, mean = 0.0;
+
+        for (i = 0; i < len; i += 2) {
+            av_bmg_get(lfg, bmg);
+            a[i]     = bmg[0] * stddev + mean;
+            a[i + 1] = bmg[1] * stddev + mean;
+        }
+}
+
+static int compare_doubles(const double *a, const double *b, int len,
+                           double max_diff)
+{
+    int i;
+
+    for (i = 0; i < len; i++) {
+        if (fabs(a[i] - b[i]) > max_diff) {
+            av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
+                   i, a[i], b[i], a[i] - b[i]);
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
+                            const float *v1, const float *v2)
+{
+    DECLARE_ALIGNED(32, float, cdst)[LEN];
+    DECLARE_ALIGNED(32, float, odst)[LEN];
+    int ret;
+
+    cdsp->vector_fmul(cdst, v1, v2, LEN);
+    fdsp->vector_fmul(odst, v1, v2, LEN);
+
+    if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                   const float *v1, const float *src0, float 
scale)
+{
+    DECLARE_ALIGNED(32, float, cdst)[LEN];
+    DECLARE_ALIGNED(32, float, odst)[LEN];
+    int ret;
+
+    memcpy(cdst, v1, LEN * sizeof(*v1));
+    memcpy(odst, v1, LEN * sizeof(*v1));
+
+    cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
+    fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
+
+    if (ret = compare_floats(cdst, odst, LEN, 0.005))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                   const float *v1, float scale)
+{
+    DECLARE_ALIGNED(32, float, cdst)[LEN];
+    DECLARE_ALIGNED(32, float, odst)[LEN];
+    int ret;
+
+    cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
+    fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
+
+    if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                   const double *v1, double scale)
+{
+    DECLARE_ALIGNED(32, double, cdst)[LEN];
+    DECLARE_ALIGNED(32, double, odst)[LEN];
+    int ret;
+
+    cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
+    fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
+
+    if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+
+static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                   const float *v1, const float *v2, const 
float *v3)
+{
+    DECLARE_ALIGNED(32, float, cdst)[LEN];
+    DECLARE_ALIGNED(32, float, odst)[LEN];
+    int ret;
+
+    cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
+    fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
+
+    if (ret = compare_floats(cdst, odst, LEN, 0.008))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                const float *v1, const float *v2, const float 
*v3)
+{
+    DECLARE_ALIGNED(32, float, cdst)[LEN];
+    DECLARE_ALIGNED(32, float, odst)[LEN];
+    int ret;
+
+    cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
+    fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
+
+    if (ret = compare_floats(cdst, odst, LEN, 0.005))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                    const float *v1, const float *v2)
+{
+    DECLARE_ALIGNED(32, float, cdst)[LEN];
+    DECLARE_ALIGNED(32, float, odst)[LEN];
+    int ret;
+
+    cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
+    fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
+
+    if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                  const float *v1, const float *v2)
+{
+    DECLARE_ALIGNED(32, float, cv1)[LEN];
+    DECLARE_ALIGNED(32, float, cv2)[LEN];
+    DECLARE_ALIGNED(32, float, ov1)[LEN];
+    DECLARE_ALIGNED(32, float, ov2)[LEN];
+    int ret;
+
+    memcpy(cv1, v1, LEN * sizeof(*v1));
+    memcpy(cv2, v2, LEN * sizeof(*v2));
+    memcpy(ov1, v1, LEN * sizeof(*v1));
+    memcpy(ov2, v2, LEN * sizeof(*v2));
+
+    cdsp->butterflies_float(cv1, cv2, LEN);
+    fdsp->butterflies_float(ov1, ov2, LEN);
+
+    if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
+        (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext 
*cdsp,
+                                    const float *v1, const float *v2)
+{
+    float cprod, oprod;
+    int ret;
+
+    cprod = cdsp->scalarproduct_float(v1, v2, LEN);
+    oprod = fdsp->scalarproduct_float(v1, v2, LEN);
+
+    if (ret = compare_floats(&cprod, &oprod, 1, 0.2))
+        av_log(NULL, AV_LOG_ERROR, "%s failed\n", __func__);
+
+    return ret;
+}
+
+int main(void)
+{
+    int ret = 0;
+    uint32_t seed = av_get_random_seed();
+    AVFloatDSPContext fdsp, cdsp;
+    AVLFG lfg;
+
+    DECLARE_ALIGNED(32, float, src0)[LEN];
+    DECLARE_ALIGNED(32, float, src1)[LEN];
+    DECLARE_ALIGNED(32, float, src2)[LEN];
+    DECLARE_ALIGNED(32, double, dbl_src0)[LEN];
+    DECLARE_ALIGNED(32, double, dbl_src1)[LEN];
+
+    av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
+
+    av_lfg_init(&lfg, seed);
+
+    fill_float_array(&lfg, src0, LEN);
+    fill_float_array(&lfg, src1, LEN);
+    fill_float_array(&lfg, src2, LEN);
+
+    fill_double_array(&lfg, dbl_src0, LEN);
+    fill_double_array(&lfg, dbl_src1, LEN);
+
+    avpriv_float_dsp_init(&fdsp, 1);
+    av_set_cpu_flags_mask(0);
+    avpriv_float_dsp_init(&cdsp, 1);
+
+    if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
+        ret -= 1 << 0;
+    if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
+        ret -= 1 << 1;
+    if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
+        ret -= 1 << 2;
+    if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
+        ret -= 1 << 3;
+    if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
+        ret -= 1 << 4;
+    if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
+        ret -= 1 << 5;
+    if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
+        ret -= 1 << 6;
+    if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
+        ret -= 1 << 7;
+    if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))
+        ret -= 1 << 8;
+
+    return ret;
+}
+
+#endif /* TEST */
-- 
1.9.0

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to