/src/cmp_tool/lib/common/cmp_cal_up_model.h
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file cmp_cal_up_model.h |
3 | | * @author Dominik Loidolt (dominik.loidolt@univie.ac.at) |
4 | | * @date 2023 |
5 | | * |
6 | | * @copyright GPLv2 |
7 | | * This program is free software; you can redistribute it and/or modify it |
8 | | * under the terms and conditions of the GNU General Public License, |
9 | | * version 2, as published by the Free Software Foundation. |
10 | | * |
11 | | * This program is distributed in the hope it will be useful, but WITHOUT |
12 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
14 | | * more details. |
15 | | * |
16 | | * @brief functions to calculate the updated (new) model |
17 | | */ |
18 | | |
19 | | #ifndef CMP_CAL_UP_MODEL_H |
20 | | #define CMP_CAL_UP_MODEL_H |
21 | | |
22 | | #include <stdint.h> |
23 | | |
24 | | |
25 | | /* the maximal model values used in the update equation for the new model */ |
26 | 531k | #define MAX_MODEL_VALUE 16U |
27 | | |
28 | | |
29 | | /** |
30 | | * @brief method for lossy rounding |
31 | | * @note This function is implemented as a macro for the sake of performance |
32 | | * |
33 | | * @param value the value to round |
34 | | * @param round rounding parameter |
35 | | * |
36 | | * @return rounded value |
37 | | */ |
38 | | |
39 | 1.26M | #define round_fwd(value, round) ((uint32_t)(value) >> (round)) |
40 | | |
41 | | |
42 | | /** |
43 | | * @brief inverse method for lossy rounding |
44 | | * @note This function is implemented as a macro for the sake of performance |
45 | | * |
46 | | * @param value the value to round back |
47 | | * @param round rounding parameter |
48 | | * |
49 | | * @return back rounded value |
50 | | */ |
51 | | |
52 | 316k | #define round_inv(value, round) ((uint32_t)(value) << (round)) |
53 | | |
54 | | |
55 | | /** |
56 | | * @brief implementation of the model update equation |
57 | | * @note check before that model_value is not greater than MAX_MODEL_VALUE |
58 | | * |
59 | | * @warning: Do not use this macro with types larger than uint32_t |
60 | | * |
61 | | * @param data data to process |
62 | | * @param model (current) model of the data to process |
63 | | * @param model_value model weighting parameter |
64 | | * @param round routing parameter |
65 | | * |
66 | | * @returns (new) updated model |
67 | | */ |
68 | | |
69 | | #define cmp_up_model(data, model, model_value, round) \ |
70 | 207k | __extension__ \ |
71 | 207k | ({ \ |
72 | 207k | uint32_t __ret; \ |
73 | 207k | switch (sizeof(data)) { \ |
74 | 11.0k | case sizeof(uint8_t): \ |
75 | 100k | case sizeof(uint16_t): \ |
76 | 100k | __ret = cmp_up_model16(data, model, model_value, round); \ |
77 | 100k | break; \ |
78 | 107k | case sizeof(uint32_t): \ |
79 | 107k | __ret = cmp_up_model32(data, model, model_value, round); \ |
80 | 107k | break; \ |
81 | 207k | } \ |
82 | 207k | (__typeof__(data))__ret; \ |
83 | 207k | }) |
84 | | |
85 | | |
86 | | /* fast calculation for data size smaller that uint32_t */ |
87 | | static __inline uint16_t cmp_up_model16(uint32_t data, uint32_t model, |
88 | | unsigned int model_value, unsigned int round) |
89 | 100k | { |
90 | | /* round and round back input because for decompression the accurate |
91 | | * data values are not available |
92 | | */ |
93 | 100k | uint32_t const weighted_data = round_inv(round_fwd(data, round), round) |
94 | 100k | * (MAX_MODEL_VALUE - model_value); |
95 | 100k | uint32_t const weighted_model = model * model_value; |
96 | | |
97 | | /* truncation is intended */ |
98 | 100k | return (uint16_t)((weighted_model + weighted_data) / MAX_MODEL_VALUE); |
99 | 100k | } Unexecuted instantiation: cmp_support.c:cmp_up_model16 Line | Count | Source | 89 | 9.28k | { | 90 | | /* round and round back input because for decompression the accurate | 91 | | * data values are not available | 92 | | */ | 93 | 9.28k | uint32_t const weighted_data = round_inv(round_fwd(data, round), round) | 94 | 9.28k | * (MAX_MODEL_VALUE - model_value); | 95 | 9.28k | uint32_t const weighted_model = model * model_value; | 96 | | | 97 | | /* truncation is intended */ | 98 | 9.28k | return (uint16_t)((weighted_model + weighted_data) / MAX_MODEL_VALUE); | 99 | 9.28k | } |
Line | Count | Source | 89 | 90.7k | { | 90 | | /* round and round back input because for decompression the accurate | 91 | | * data values are not available | 92 | | */ | 93 | 90.7k | uint32_t const weighted_data = round_inv(round_fwd(data, round), round) | 94 | 90.7k | * (MAX_MODEL_VALUE - model_value); | 95 | 90.7k | uint32_t const weighted_model = model * model_value; | 96 | | | 97 | | /* truncation is intended */ | 98 | 90.7k | return (uint16_t)((weighted_model + weighted_data) / MAX_MODEL_VALUE); | 99 | 90.7k | } |
Unexecuted instantiation: cmp_rdcu_cfg.c:cmp_up_model16 |
100 | | |
101 | | |
102 | | /* slow calculation for uint32_t data size */ |
103 | | static __inline uint32_t cmp_up_model32(uint32_t data, uint32_t model, |
104 | | unsigned int model_value, unsigned int round) |
105 | 119k | { |
106 | | /* round and round back input because for decompression the accurate |
107 | | * data values are not available |
108 | | * cast to uint64_t to prevent overflow in the multiplication |
109 | | */ |
110 | 119k | uint64_t const weighted_data = (uint64_t)round_inv(round_fwd(data, round), round) |
111 | 119k | * (MAX_MODEL_VALUE - model_value); |
112 | 119k | uint64_t const weighted_model = (uint64_t)model * model_value; |
113 | | |
114 | | /* truncation is intended */ |
115 | 119k | return (uint32_t)((weighted_model + weighted_data) / MAX_MODEL_VALUE); |
116 | 119k | } Unexecuted instantiation: cmp_support.c:cmp_up_model32 Line | Count | Source | 105 | 17.9k | { | 106 | | /* round and round back input because for decompression the accurate | 107 | | * data values are not available | 108 | | * cast to uint64_t to prevent overflow in the multiplication | 109 | | */ | 110 | 17.9k | uint64_t const weighted_data = (uint64_t)round_inv(round_fwd(data, round), round) | 111 | 17.9k | * (MAX_MODEL_VALUE - model_value); | 112 | 17.9k | uint64_t const weighted_model = (uint64_t)model * model_value; | 113 | | | 114 | | /* truncation is intended */ | 115 | 17.9k | return (uint32_t)((weighted_model + weighted_data) / MAX_MODEL_VALUE); | 116 | 17.9k | } |
Line | Count | Source | 105 | 102k | { | 106 | | /* round and round back input because for decompression the accurate | 107 | | * data values are not available | 108 | | * cast to uint64_t to prevent overflow in the multiplication | 109 | | */ | 110 | 102k | uint64_t const weighted_data = (uint64_t)round_inv(round_fwd(data, round), round) | 111 | 102k | * (MAX_MODEL_VALUE - model_value); | 112 | 102k | uint64_t const weighted_model = (uint64_t)model * model_value; | 113 | | | 114 | | /* truncation is intended */ | 115 | 102k | return (uint32_t)((weighted_model + weighted_data) / MAX_MODEL_VALUE); | 116 | 102k | } |
Unexecuted instantiation: cmp_rdcu_cfg.c:cmp_up_model32 |
117 | | |
118 | | #endif /* CMP_CAL_UP_MODEL */ |