aboutsummaryrefslogtreecommitdiff
path: root/encoder/var_q_operator.h
blob: 5a8068877d473a310809b8e2b23c8ab29488ea6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#ifndef VAR_Q_OPERATOR_H
#define VAR_Q_OPERATOR_H

typedef struct
{
    WORD32 sm; /* MSB 1 bit sign & rest magnitude */
    WORD32 e; /* Q-format */
} number_t;

void mult32_var_q(number_t a, number_t b, number_t *c);

void div32_var_q(number_t a, number_t b, number_t *c);

void add32_var_q(number_t a, number_t b, number_t *c);

void sub32_var_q(number_t a, number_t b, number_t *c);

void sqrt32_var_q(number_t a, number_t *c);

void number_t_to_word32(number_t num_a, WORD32 *a);

void convert_float_to_fix(float a_f, number_t *a);

void convert_fix_to_float(number_t a, float *a_f);

#define SET_VAR_Q(a, b, c)                                                                         \
    {                                                                                              \
        (a).sm = (b);                                                                              \
        (a).e = (c);                                                                               \
    }

/*right shift greated than 32 bit for word32 variable is undefined*/
#define convert_varq_to_fixq(varq, a, q_fact)                                                      \
    {                                                                                              \
        if((varq).e > q_fact)                                                                      \
        {                                                                                          \
            if(((varq).e - q_fact) >= (WORD32)(sizeof(WORD32) * 8))                                \
            {                                                                                      \
                *a = 0;                                                                            \
            }                                                                                      \
            else                                                                                   \
            {                                                                                      \
                *a = (varq).sm >> ((varq).e - q_fact);                                             \
            }                                                                                      \
        }                                                                                          \
        else                                                                                       \
            *a = (varq).sm << (q_fact - (varq).e);                                                 \
    }

#define SET_VARQ_FRM_FIXQ(fixq, var_q, q_fact)                                                     \
    {                                                                                              \
        (var_q).sm = fixq;                                                                         \
        (var_q).e = q_fact;                                                                        \
    }
#endif