Debian Codegraph @

lib/colorhug/ch-math.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011-2013 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <math.h>
26
27 #include "ch-math.h"
28 #include "ch-common.h"
29
30 /**
31 * ch_packed_float_get_value:
32 *
33 * @pf: A %ChPackedFloat
34 *
35 * Return value: packed value to host byte order
36 *
37 * Since: 0.1.29
38 **/
39 gint32
40 ch_packed_float_get_value (const ChPackedFloat *pf)
41 {
42 return (pf->bytes[3] << 24) |
43 (pf->bytes[2] << 16) |
44 (pf->bytes[1] << 8) |
45 (pf->bytes[0]);
46 }
47
48 /**
49 * ch_packed_float_set_value:
50 *
51 * @pf: A %ChPackedFloat
52 * @value: a value in packed format in host byte order.
53 *
54 * Stores value in host byte order into packed format for device.
55 *
56 * Since: 0.1.29
57 **/
58 void
59 ch_packed_float_set_value (ChPackedFloat *pf, const gint32 value)
60 {
61 pf->bytes[0] = (value & 0xff);
62 pf->bytes[1] = ((value >> 8) & 0xff);
63 pf->bytes[2] = ((value >> 16) & 0xff);
64 pf->bytes[3] = ((value >> 24) & 0xff);
65 }
66
67 /**
68 * ch_packed_float_to_double:
69 *
70 * @pf: A %ChPackedFloat
71 * @value: a value in IEEE floating point format
72 *
73 * Converts a packed float to a double.
74 *
75 * Since: 0.1.29
76 **/
77 void
78 ch_packed_float_to_double (const ChPackedFloat *pf, gdouble *value)
79 {
80 g_return_if_fail (value != NULL);
81 g_return_if_fail (pf != NULL);
82 *value = ch_packed_float_get_value (pf) / (gdouble) 0x10000;
83 }
84
85 /**
86 * ch_double_to_packed_float:
87 *
88 * @pf: A %ChPackedFloat
89 * @value: a value in IEEE floating point format
90 *
91 * Converts a double number to a packed float.
92 *
93 * Since: 0.1.29
94 **/
95 void
96 ch_double_to_packed_float (gdouble value, ChPackedFloat *pf)
97 {
98 g_return_if_fail (pf != NULL);
99 g_return_if_fail (value <= 0x8000);
100 g_return_if_fail (value >= -0x8000);
101 ch_packed_float_set_value (pf, value * (gdouble) 0x10000);
102 }
103
104 /**
105 * ch_packed_float_add:
106 *
107 * @pf1: A %ChPackedFloat
108 * @pf1: A %ChPackedFloat
109 * @result: A %ChPackedFloat
110 *
111 * Adds two packed floats together using only integer maths.
112 *
113 * Return value: an error code
114 *
115 * Since: 0.1.29
116 **/
117 ChError
118 ch_packed_float_add (const ChPackedFloat *pf1,
119 const ChPackedFloat *pf2,
120 ChPackedFloat *result)
121 {
122 gint32 pf1_tmp;
123 gint32 pf2_tmp;
124
125 g_return_val_if_fail (pf1 != NULL, CH_ERROR_INVALID_VALUE);
126 g_return_val_if_fail (pf2 != NULL, CH_ERROR_INVALID_VALUE);
127 g_return_val_if_fail (result != NULL, CH_ERROR_INVALID_VALUE);
128
129 /* check overflow */
130 pf1_tmp = ch_packed_float_get_value (pf1) / 0x10000;
131 pf2_tmp = ch_packed_float_get_value (pf2) / 0x10000;
132 if (pf1_tmp + pf2_tmp > 0x8000)
133 return CH_ERROR_OVERFLOW_ADDITION;
134
135 /* do the proper result */
136 ch_packed_float_set_value (result,
137 ch_packed_float_get_value (pf1) +
138 ch_packed_float_get_value (pf2));
139 return CH_ERROR_NONE;
140 }
141
142 /**
143 * ch_packed_float_multiply:
144 *
145 * @pf1: A %ChPackedFloat
146 * @pf1: A %ChPackedFloat
147 * @result: A %ChPackedFloat
148 *
149 * Multiplies two packed floats together using only integer maths.
150 *
151 * Return value: an error code
152 *
153 * Since: 0.1.29
154 **/
155 ChError
156 ch_packed_float_multiply (const ChPackedFloat *pf1,
157 const ChPackedFloat *pf2,
158 ChPackedFloat *result)
159 {
160 ChPackedFloat pf1_tmp;
161 ChPackedFloat pf2_tmp;
162 gint32 tmp;
163
164 g_return_val_if_fail (pf1 != NULL, CH_ERROR_INVALID_VALUE);
165 g_return_val_if_fail (pf2 != NULL, CH_ERROR_INVALID_VALUE);
166 g_return_val_if_fail (result != NULL, CH_ERROR_INVALID_VALUE);
167
168 /* make positive */
169 pf1_tmp.raw = ABS(ch_packed_float_get_value (pf1));
170 pf2_tmp.raw = ABS(ch_packed_float_get_value (pf2));
171
172 /* check for overflow */
173 if (pf1_tmp.offset > 0 &&
174 0x8000 / pf1_tmp.offset < pf2_tmp.offset)
175 return CH_ERROR_OVERFLOW_MULTIPLY;
176
177 /* do long multiplication on each 16 bit part */
178 tmp = ((guint32) pf1_tmp.fraction *
179 (guint32) pf2_tmp.fraction) / 0x10000;
180 tmp += ((guint32) pf1_tmp.offset *
181 (guint32) pf2_tmp.offset) * 0x10000;
182 tmp += (guint32) pf1_tmp.fraction *
183 (guint32) pf2_tmp.offset;
184 tmp += (guint32) pf1_tmp.offset *
185 (guint32) pf2_tmp.fraction;
186
187 /* correct sign bit */
188 if ((pf1->raw < 0) ^ (pf2->raw < 0))
189 tmp = -tmp;
190 ch_packed_float_set_value (result, tmp);
191 return CH_ERROR_NONE;
192 }