Fixed Point API

Fixed Point API — Fixed Point API

Functions

Types and Values

Description

COGL has a fixed point API targeted at platforms without a floating point unit, such as embedded devices. On such platforms this API should be preferred to the floating point one as it does not trigger the slow path of software emulation, relying on integer math for fixed-to-floating and floating-to-fixed notations conversion.

It is not recommened for use on platforms with a floating point unit (e.g. desktop systems), nor for use in language bindings.

Basic rules of Fixed Point arithmethic:

  • Two fixed point numbers can be directly added, subtracted and have their modulus taken.

  • To add other numerical type to a fixed point number it has to be first converted to fixed point.

  • A fixed point number can be directly multiplied or divided by an integer.

  • Two fixed point numbers can only be multiplied and divided by the provided COGL_FIXED_MUL and COGL_FIXED_DIV macros.

The fixed point API is available since COGL 1.0.

Functions

COGL_FIXED_FROM_FLOAT()

#define COGL_FIXED_FROM_FLOAT(x)        ((float) cogl_double_to_fixed (x))

Converts x from a floating point to a fixed point notation.

Parameters

x

a floating point number

 

Since: 1.0


COGL_FIXED_TO_FLOAT()

#define COGL_FIXED_TO_FLOAT(x)          ((float) ((int)(x) / 65536.0))

Converts x from a fixed point to a floating point notation, in single precision.

Parameters

x

a CoglFixed number

 

Since: 1.0


COGL_FIXED_FROM_INT()

#define COGL_FIXED_FROM_INT(x)          ((x) << COGL_FIXED_Q)

Converts x from an integer to a fixed point notation.

Parameters

x

an integer number

 

Since: 1.0


COGL_FIXED_TO_INT()

#define COGL_FIXED_TO_INT(x)            ((x) >> COGL_FIXED_Q)

Converts x from a fixed point notation to an integer, dropping the fractional part without rounding.

Parameters

x

a CoglFixed number

 

Since: 1.0


COGL_FIXED_FROM_DOUBLE()

#define COGL_FIXED_FROM_DOUBLE(x)       (cogl_double_to_fixed (x))

Converts x from a double precision, floating point to a fixed point notation.

Parameters

x

a floating point number

 

Since: 1.0


COGL_FIXED_TO_DOUBLE()

#define COGL_FIXED_TO_DOUBLE(x)         ((double) ((int)(x) / 65536.0))

Converts x from a fixed point to a floating point notation, in double precision.

Parameters

x

a CoglFixed number

 

Since: 1.0


COGL_FLOAT_TO_INT()

#define COGL_FLOAT_TO_INT(x)            (cogl_double_to_int ((x)))

Converts x from a floating point notation to a signed integer.

Parameters

x

a floatint point number

 

Since: 1.0


COGL_FLOAT_TO_UINT()

#define COGL_FLOAT_TO_UINT(x)           (cogl_double_to_uint ((x)))

Converts x from a floating point notation to an unsigned integer.

Parameters

x

a floatint point number

 

Since: 1.0


COGL_FIXED_MUL()

#define COGL_FIXED_MUL(a,b)             (cogl_fixed_mul ((a), (b)))

Computes (a * b).

Parameters

a

a CoglFixed number

 

b

a CoglFixed number

 

Since: 1.0


COGL_FIXED_DIV()

#define COGL_FIXED_DIV(a,b)             (cogl_fixed_div ((a), (b)))

Computes (a / b).

Parameters

a

a CoglFixed number

 

b

a CoglFixed number

 

Since: 1.0


COGL_FIXED_MUL_DIV()

#define COGL_FIXED_MUL_DIV(a,b,c)       (cogl_fixed_mul_div ((a), (b), (c)))

Computes ((a * b) / c). It is logically equivalent to:

1
res = COGL_FIXED_DIV (COGL_FIXED_MUL (a, b), c);

But it is shorter to type.

Parameters

a

a CoglFixed number

 

b

a CoglFixed number

 

c

a CoglFixed number

 

Since: 1.0


COGL_FIXED_FAST_DIV()

#define COGL_FIXED_FAST_DIV(a,b)        ((((a) << 8) / (b)) << 8)

Fast version of COGL_FIXED_DIV, implemented as a macro.

This macro might lose precision. If the precision of the result is important use COGL_FIXED_DIV instead.

Parameters

a

a CoglFixed number

 

b

a CoglFixed number

 

Since: 1.0


COGL_FIXED_FAST_MUL()

#define COGL_FIXED_FAST_MUL(a,b)        ((a) >> 8) * ((b) >> 8)

Fast version of COGL_FIXED_MUL, implemented as a macro.

This macro might lose precision. If the precision of the result is important use COGL_FIXED_MUL instead.

Parameters

a

a CoglFixed number

 

b

a CoglFixed number

 

Since: 1.0


COGL_FIXED_FRACTION()

#define COGL_FIXED_FRACTION(x)          ((x) & ((1 << COGL_FIXED_Q) - 1))

Retrieves the fractionary part of x .

Parameters

x

a CoglFixed number

 

Since: 1.0


COGL_FIXED_FLOOR()

#define             COGL_FIXED_FLOOR(x)

Rounds down a fixed point number to the previous integer.

Parameters

x

a CoglFixed number

 

Since: 1.0


COGL_FIXED_CEIL()

#define COGL_FIXED_CEIL(x)              (COGL_FIXED_FLOOR ((x) + 0xffff))

Rounds up a fixed point number to the next integer.

Parameters

x

a CoglFixed number

 

Since: 1.0


cogl_fixed_atan2 ()

CoglFixed
cogl_fixed_atan2 (CoglFixed a,
                  CoglFixed b);

Computes the arc tangent of a / b but uses the sign of both arguments to return the angle in right quadrant.

Parameters

a

the numerator as a CoglFixed number

 

b

the denominator as a CoglFixed number

 

Returns

the arc tangent of the passed fraction, in fixed point notation

Since: 1.0


cogl_fixed_atan ()

CoglFixed
cogl_fixed_atan (CoglFixed a);

Computes the arc tangent of a .

Parameters

a

a CoglFixed number

 

Returns

the arc tangent of the passed value, in fixed point notation

Since: 1.0


cogl_fixed_cos ()

CoglFixed
cogl_fixed_cos (CoglFixed angle);

Computes the cosine of angle .

Parameters

angle

a CoglFixed number

 

Returns

the cosine of the passed angle, in fixed point notation

Since: 1.0


cogl_fixed_sin ()

CoglFixed
cogl_fixed_sin (CoglFixed angle);

Computes the sine of angle .

Parameters

angle

a CoglFixed number

 

Returns

the sine of the passed angle, in fixed point notation

Since: 1.0


cogl_fixed_tan ()

CoglFixed
cogl_fixed_tan (CoglFixed angle);

Computes the tangent of angle .

Parameters

angle

a CoglFixed number

 

Returns

the tangent of the passed angle, in fixed point notation

Since: 1.0


cogl_fixed_div ()

CoglFixed
cogl_fixed_div (CoglFixed a,
                CoglFixed b);

cogl_fixed_mul ()

CoglFixed
cogl_fixed_mul (CoglFixed a);

cogl_fixed_mul_div ()

CoglFixed
cogl_fixed_mul_div (CoglFixed a,
                    CoglFixed b,
                    CoglFixed c);

Types and Values

CoglFixed

typedef int32_t CoglFixed;

Fixed point number using a (16.16) notation.


COGL_FIXED_BITS

#define COGL_FIXED_BITS         (32)

Evaluates to the number of bits used by the CoglFixed type.

Since: 1.0


COGL_FIXED_Q

#define COGL_FIXED_Q            (COGL_FIXED_BITS - 16)

Evaluates to the number of bits used for the non-integer part of the CoglFixed type.

Since: 1.0


COGL_FIXED_MAX

#define COGL_FIXED_MAX          (0x7fffffff)

The biggest number representable using CoglFixed

Since: 1.0


COGL_FIXED_MIN

#define COGL_FIXED_MIN          (0x80000000)

The smallest number representable using CoglFixed

Since: 1.0


COGL_FIXED_EPSILON

#define COGL_FIXED_EPSILON      (1)

A very small number expressed as a CoglFixed number.

Since: 1.0


COGL_FIXED_1

#define COGL_FIXED_1            (1 << COGL_FIXED_Q)

The number 1 expressed as a CoglFixed number.

Since: 1.0


COGL_FIXED_0_5

#define COGL_FIXED_0_5          (32768)

The number 0.5 expressed as a CoglFixed number.

Since: 1.0


COGL_FIXED_30

#define COGL_FIXED_30           (COGL_FIXED_FROM_INT (30))

Evaluates to the number 30 in fixed point notation.

Since: 1.0


COGL_FIXED_45

#define COGL_FIXED_45           (COGL_FIXED_FROM_INT (45))

Evaluates to the number 45 in fixed point notation.

Since: 1.0


COGL_FIXED_60

#define COGL_FIXED_60           (COGL_FIXED_FROM_INT (60))

Evaluates to the number 60 in fixed point notation.

Since: 1.0


COGL_FIXED_90

#define COGL_FIXED_90           (COGL_FIXED_FROM_INT (90))

Evaluates to the number 90 in fixed point notation.

Since: 1.0


COGL_FIXED_120

#define COGL_FIXED_120          (COGL_FIXED_FROM_INT (120))

Evaluates to the number 120 in fixed point notation.

Since: 1.0


COGL_FIXED_180

#define COGL_FIXED_180          (COGL_FIXED_FROM_INT (180))

Evaluates to the number 180 in fixed point notation.

Since: 1.0


COGL_FIXED_240

#define COGL_FIXED_240          (COGL_FIXED_FROM_INT (240))

Evaluates to the number 240 in fixed point notation.

Since: 1.0


COGL_FIXED_255

#define COGL_FIXED_255          (COGL_FIXED_FROM_INT (255))

Evaluates to the number 255 in fixed point notation.

Since: 1.0


COGL_FIXED_270

#define COGL_FIXED_270          (COGL_FIXED_FROM_INT (270))

Evaluates to the number 270 in fixed point notation.

Since: 1.0


COGL_FIXED_360

#define COGL_FIXED_360          (COGL_FIXED_FROM_INT (360))

Evaluates to the number 360 in fixed point notation.

Since: 1.0


COGL_FIXED_2_PI

#define COGL_FIXED_2_PI         (0x0006487f)

Two times pi, expressed as a CoglFixed number.

Since: 1.0


COGL_FIXED_PI

#define COGL_FIXED_PI           (0x0003243f)

The number pi, expressed as a CoglFixed number.

Since: 1.0


COGL_FIXED_PI_2

#define COGL_FIXED_PI_2         (0x00019220)

Half pi, expressed as a CoglFixed number.

Since: 1.0


COGL_FIXED_PI_4

#define COGL_FIXED_PI_4         (0x0000c910)

pi / 4, expressed as CoglFixed number.

Since: 1.0


COGL_RADIANS_TO_DEGREES

#define COGL_RADIANS_TO_DEGREES (0x394bb8)

Evaluates to 180 / pi in fixed point notation.

Since: 1.0


CoglAngle

typedef int32_t CoglAngle;

Integer representation of an angle such that 1024 corresponds to full circle (i.e., 2 * pi).

Since: 1.0