summaryrefslogtreecommitdiff
path: root/dix
diff options
context:
space:
mode:
authorJeremy Huddleston <jeremyhu@apple.com>2011-10-05 15:02:52 -0700
committerJeremy Huddleston <jeremyhu@apple.com>2011-10-15 21:15:47 -0700
commitf9c6903d4a90b59c328f4fa05d2be9e0ce1c5189 (patch)
tree88c0cbb0337a8eec61e6c2e8836b302922c9ae4d /dix
parent09dbfcb0ad7b6c8bac94502f2801e82f2a2ef435 (diff)
dix: add utility functions for double to/fro FP1616/FP3232 conversion
Co-authored-by: Jeremy Huddleston <jeremyhu@apple.com> Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Jeremy Huddleston <jeremyhu@apple.com> Reviewed-by: Mark Kettenis <kettenis@openbsd.org>
Diffstat (limited to 'dix')
-rw-r--r--dix/inpututils.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/dix/inpututils.c b/dix/inpututils.c
index 0a3d3d8b4..cd4577347 100644
--- a/dix/inpututils.c
+++ b/dix/inpututils.c
@@ -759,3 +759,66 @@ input_option_set_value(InputOption *opt, const char *value)
if (value)
opt->value = strdup(value);
}
+
+
+/* FP1616/FP3232 conversion functions.
+ * Fixed point 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;
+
+ ret = (double)(in >> 16);
+ ret += (double)(in & 0xffff) * (1.0 / (1UL << 16)); /* Optimized: ldexp((double)(in & 0xffff), -16); */
+ return ret;
+}
+
+double
+fp3232_to_double(FP3232 in)
+{
+ double ret;
+ ret = (double)in.integral;
+ ret += (double)in.frac * (1.0 / (1ULL << 32)); /* Optimized: 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;
+
+ tmp = (in - integral) * (1UL << 16); /* Optimized: ldexp(in - integral, 16) */
+ frac_d = (uint16_t)tmp;
+
+ ret = integral << 16;
+ 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 = (in - integral) * (1ULL << 32); /* Optimized: ldexp(in - integral, 32) */
+ frac_d = (uint32_t)tmp;
+
+ ret.integral = integral;
+ ret.frac = frac_d;
+ return ret;
+}