[PATCH v2] dix: add utility functions for double to/fro FP1616/FP3232 conversion

Peter Hutterer peter.hutterer at who-t.net
Wed Oct 5 00:01:30 PDT 2011


Co-authored by Jeremy Huddleston <jeremyhu at apple.com>
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
Changes to v1:
- works for negative numbers now too. oh, the features!
- dropped the fabs(frac) < 1 part, we should be able to rely on (foo -
  (int)foo) to give us something within 0..1
- renamed tmp variables to tmp, need them to silence gcc
- added comment about encoding of negative FP16/32 numbers

 dix/inpututils.c     |   66 +++++++++++++++++++++++++
 include/inpututils.h |    6 ++
 test/input.c         |  133 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 205 insertions(+), 0 deletions(-)

diff --git a/dix/inpututils.c b/dix/inpututils.c
index 582135e..496de6c 100644
--- a/dix/inpututils.c
+++ b/dix/inpututils.c
@@ -821,3 +821,69 @@ input_option_set_value(InputOption *opt, const char *value)
     if (value)
         opt->value = strdup(value);
 }
+
+
+/* FP1616/FP3232 conversion functions.
+
+   FP types are encoded as signed integral and unsigned frac. So any
+   negative number -n.m is encoded as floor(n) + (1 - 0.m).
+ */
+double
+fp1616_to_double(FP1616 in)
+{
+    double ret;
+    double frac;
+
+    ret = (double)(in >> 16);
+    frac = ldexp((double)(in & 0xffff), -16);
+    ret += frac;
+    return ret;
+}
+
+double
+fp3232_to_double(FP3232 in)
+{
+    double ret;
+    ret = (double)in.integral;
+    ret += ldexp((double)in.frac, -32);
+    return ret;
+}
+
+
+FP1616
+double_to_fp1616(double in)
+{
+    FP1616 ret;
+    int32_t integral;
+    double tmp;
+    uint32_t frac_d;
+
+    tmp = floor(in);
+    integral = (int32_t)tmp << 16;
+
+    tmp = ldexp(in - (int)in, 16);
+    frac_d = (uint16_t)tmp;
+
+    ret = integral;
+    ret |= frac_d & 0xffff;
+    return ret;
+}
+
+FP3232
+double_to_fp3232(double in)
+{
+    FP3232 ret;
+    int32_t integral;
+    double tmp;
+    uint32_t frac_d;
+
+    tmp = floor(in);
+    integral = (int32_t)tmp;
+    tmp = ldexp(in - (int)in, 32);
+    frac_d = (uint32_t)tmp;
+
+    ret.integral = integral;
+    ret.frac = frac_d;
+    return ret;
+}
+
diff --git a/include/inpututils.h b/include/inpututils.h
index 47e242d..2832ed5 100644
--- a/include/inpututils.h
+++ b/include/inpututils.h
@@ -30,6 +30,7 @@
 #define INPUTUTILS_H
 
 #include "input.h"
+#include <X11/extensions/XI2proto.h>
 
 struct _ValuatorMask {
     int8_t      last_bit; /* highest bit set in mask */
@@ -40,4 +41,9 @@ struct _ValuatorMask {
 extern void verify_internal_event(const InternalEvent *ev);
 extern void init_device_event(DeviceEvent *event, DeviceIntPtr dev, Time ms);
 
+FP3232 double_to_fp3232(double in);
+FP1616 double_to_fp1616(double in);
+double fp1616_to_double(FP1616 in);
+double fp3232_to_double(FP3232 in);
+
 #endif
diff --git a/test/input.c b/test/input.c
index 5fb9a90..85652a3 100644
--- a/test/input.c
+++ b/test/input.c
@@ -1476,8 +1476,141 @@ static void input_option_test(void)
 }
 
 
+static void
+_test_double_fp16_values(double d)
+{
+    FP1616 fp16;
+    double re;
+    double tmp;
+    uint32_t frac;
+    int32_t integral;
+
+    if (d > 0x7FFF) {
+        printf("Test out of range\n");
+        assert(0);
+    }
+
+    tmp = ldexp(d - (int)d, 16);
+    frac = (uint16_t)tmp; /* shut up, gcc */
+    tmp = floor(d);
+    integral = (int16_t)tmp << 16;
+
+    fp16 = double_to_fp1616(d);
+    re = fp1616_to_double(fp16);
+
+    /*printf("FP16: double: %f fp16: %u int: %u frac: %u re:%f\n", d, fp16, integral, frac, re);*/
+
+    assert((fp16 & 0xFFFF0000) == integral);
+    assert((fp16 & 0x0000FFFF) == frac);
+
+    /* since we lose precision, we only do rough range testing */
+    assert(re > d - 0.1);
+    assert(re < d + 0.1);
+
+    if (d > 0)
+        _test_double_fp16_values(-d);
+}
+
+static void
+_test_double_fp32_values(double d)
+{
+    FP3232 fp32;
+    double re;
+    double tmp;
+    uint32_t frac;
+    int32_t integral;
+
+    if (d > 0x7FFFFFFF) {
+        printf("Test out of range\n");
+        assert(0);
+    }
+
+    tmp = ldexp(d - (int)d, 32);
+    frac = (uint32_t)tmp;
+    tmp = floor(d);
+    integral = (int32_t)tmp;
+
+    fp32 = double_to_fp3232(d);
+    re = fp3232_to_double(fp32);
+
+    /* printf("FP32: double: %f fp32: %u.%u int: %u frac: %u re:%f\n", d, fp32.integral, fp32.frac, integral, frac, re); */
+
+    assert(fp32.integral == integral);
+    assert(fp32.frac == frac);
+
+    /* since we lose precision, we only do rough range testing */
+    assert(re > d - 0.1);
+    assert(re < d + 0.1);
+
+    if (d > 0)
+        _test_double_fp32_values(-d);
+}
+
+static void
+dix_double_fp_conversion(void)
+{
+    long i;
+    printf("Testing double to FP1616/FP3232 conversions\n");
+
+    _test_double_fp16_values(0);
+    for (i = 1; i < 0x7FFF; i <<= 1) {
+        double val;
+
+        val = i;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        /* and some pseudo-random floating points */
+        val = i - 0.00382;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.00382;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.05234;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.12342;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.27583;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.50535;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.72342;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+
+        val = i + 0.80408;
+        _test_double_fp16_values(val);
+        _test_double_fp32_values(val);
+    }
+
+    for (i = 0x7FFFF; i < 0x7FFFFFFF; i <<= 1) {
+        _test_double_fp32_values(i);
+        /* and a few more random floating points, obtained
+         * by faceplanting into the numpad repeatedly */
+        _test_double_fp32_values(i + 0.010177);
+        _test_double_fp32_values(i + 0.213841);
+        _test_double_fp32_values(i + 0.348720);
+        _test_double_fp32_values(i + 0.472020);
+        _test_double_fp32_values(i + 0.572020);
+        _test_double_fp32_values(i + 0.892929);
+    }
+
+}
+
 int main(int argc, char** argv)
 {
+    dix_double_fp_conversion();
     dix_input_valuator_masks();
     dix_input_attributes();
     dix_init_valuators();
-- 
1.7.6.4



More information about the xorg-devel mailing list