Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement fix point number format #3115

Open
StephanHeider opened this issue Jul 10, 2018 · 1 comment
Open

How to implement fix point number format #3115

StephanHeider opened this issue Jul 10, 2018 · 1 comment

Comments

@StephanHeider
Copy link

Hi,

we cannot use floating point operations and therefore have to rely on fix point math. Therefore I would like to bring the Q number format (see https://en.wikipedia.org/wiki/Q_(number_format)) into Halide.
The major purpose of this is to reduce the necessary code to do all this nasty bit shifting.

Basically what I want to write later on is something like:

Func input;
input(x, y) = x + y;

QNumber gain1(3, 5); // create a Q3.5 number
gain = 3.5f;

QNumber offset(7, 23); // create a Q7.23 number
offset = 1.3f; 

Func gained;
gained(x, y) = input(x, y) * gain1;

Func added;
added(x, y) = gained(x, y) + offset;

Func output;
output(x, y) = added(x, y).to_integer(8, "ceil"); // specify rounding to use

What would be the best way to do this?

I already tried using Halide::Tuple and came out with two major drawbacks:

  1. The bit width of the internal integer expression cannot be changed dynamically. This would be possible if the integer and fractional bits information would be C++ variables (constants for the JIT compiler).

  2. I am not able to determine if the Tuple is a QNumber or any user defined Tuple.
    So I would like to write something like:
    Expr bigger = select(added(x, y) < QNumber(3, 5, 12), 1, 0);
    But this is not possible because I have to explicitly cast the added(x, y) to QNumber.

So is there a better approach than using Halide::Tuple?

@StephanHeider StephanHeider changed the title How to implement FixPoint abstraction How to implement fix point number format Jul 10, 2018
@SanderVocke
Copy link
Contributor

One way could be to use unsigned integers for storing your Q numbers. Then you could write a wrapper class that offers the most common operations on Q numbers (arithmetic, conversions from/to other types, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants