vectorizor
vectorizor

Reputation: 719

initialize a variable statically (at compile time)

  1. I've got many constants in my C algo.
  2. my code works both in floating-point and fixed-point.

Right now, these constants are initialized by a function, float2fixed, whereby in floating-point it does nothing, while in fixed-point, it finds their fixed-point representation. For instance, 0.5f stays 0.5f if working in floating-point, whereas it uses the pow() routine and becomes 32768 if working in fixed-point and the fixed-point representation is Qx.16.

That's easy to maintain, but it takes a lot of time actually to compute these constants in fixed-point (pow is a floatin-point function). In C++, I'd use some meta-programming, so the compiler computes these values at compile-time, so there's no hit at run-time. But in C, thats not possible. Or is it? Anybody knows of such a trick? Is any compiler clever enough to do that?

Upvotes: 3

Views: 1991

Answers (4)

Chris Arguin
Chris Arguin

Reputation: 11998

Recent versions of GCC ( around 4.3 ) added the ability to use GMP and MPFR to do some compile-time optimisations by evaluating more complex functions that are constant. That approach leaves your code simple and portable, and trust the compiler to do the heavy lifting.

Of course, there are limits to what it can do, and it would be hard to know if it's optimizing a given instance without going and looking at the assembly. But it might be worth checking out. Here's a link to the description in the changelog

Upvotes: 0

puetzk
puetzk

Reputation: 10814

Rather than using (unsigned)(x*pow(2,16)) to do your fixed point conversion, write it as (unsigned)(0.5f * (1 << 16))

This should be an acceptable as a compile-time constant expression since it involves only builtin operators.

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

When using fixed-point, can you write a program that takes your floating point values and converts them into correct, constant initializers for the fixed point type, so you effectively add a step to the compilation that generates the fixed point values.

One advantage of this will be that you can then define and declare your constants with const so that they won't change at run-time - whereas with the initialization functions, of course, the values have to be modifiable because they are calculated once.


I mean write a simple program that can scan for formulaic lines that might read:

const double somename = 3.14159;

it would read that and generate:

const fixedpoint_t somename = { ...whatever is needed... };

You design the operation to make it easy to manage for both notations - so maybe your converter always reads the file and sometimes rewrites it.

datafile.c:   datafile.constants converter
        converter datafile.constants > datafile.c

Upvotes: 2

C Pirate
C Pirate

Reputation: 444

In plain C, there's not much you can do. You need to do the conversion at some point, and the compiler doesn't give you any access to call interesting user-provided functions at compile time. Theoretically, you could try to coax the preprocessor to do it for you, but that's the quick road to total insanity (i.e. you'd have to implement pow() in macros, which is pretty hideous).

Some options I can think of:

  1. Maintain a persistent cache on disk. At least then it'd only be slow once, though you still have to load it, make sure it's not corrupt, etc.

  2. As mentioned in another comment, use template metaprogramming anyway and compile with a C++ compiler. Most C works just fine (arguably better) with a C++ compiler.

Hmm, I guess that's about all I can think of. Good luck.

Upvotes: 0

Related Questions