diff options
author | Keith Packard <keithp@keithp.com> | 2011-10-17 14:36:34 -0700 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2011-10-17 14:36:34 -0700 |
commit | 64d2d1bef14c7ec7156bb3aae64eff69f9f7be54 (patch) | |
tree | 97e08a3a7b06c8c491be1bda793386c293d21ded /dix/inpututils.c | |
parent | fb84be47db7cdaff406792c08e34670e8e0cbda9 (diff) | |
parent | 679c84bce9b03c17aa35fa69058e7e46267dbc7b (diff) |
Merge remote-tracking branch 'jeremyhu/master'
Diffstat (limited to 'dix/inpututils.c')
-rw-r--r-- | dix/inpututils.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/dix/inpututils.c b/dix/inpututils.c index eeae2a74f..5c76361b3 100644 --- a/dix/inpututils.c +++ b/dix/inpututils.c @@ -767,3 +767,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; +} |