eGUI alias D4D  Release 3.0
Reference Manual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
d4d_math.c
Go to the documentation of this file.
1 /**************************************************************************
2 *
3 * Copyright 2014 by Petr Gargulak. eGUI Community.
4 * Copyright 2009-2013 by Petr Gargulak. Freescale Semiconductor, Inc.
5 *
6 ***************************************************************************
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License Version 3
9 * or later (the "LGPL").
10 *
11 * As a special exception, the copyright holders of the eGUI project give you
12 * permission to link the eGUI sources with independent modules to produce an
13 * executable, regardless of the license terms of these independent modules,
14 * and to copy and distribute the resulting executable under terms of your
15 * choice, provided that you also meet, for each linked independent module,
16 * the terms and conditions of the license of that module.
17 * An independent module is a module which is not derived from or based
18 * on this library.
19 * If you modify the eGUI sources, you may extend this exception
20 * to your version of the eGUI sources, but you are not obligated
21 * to do so. If you do not wish to do so, delete this
22 * exception statement from your version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 *
28 * You should have received a copy of the GNU General Public License
29 * and the GNU Lesser General Public License along with this program.
30 * If not, see <http://www.gnu.org/licenses/>.
31 *
32 ***************************************************************************/
46 #include "d4d.h"
48 
49 const sByte d4d_sinTbl[64] =
50 {
51  0x00,
52  0x03,
53  0x06,
54  0x09,
55  0x0D,
56  0x10,
57  0x13,
58  0x16,
59  0x19,
60  0x1C,
61  0x1F,
62  0x22,
63  0x25,
64  0x28,
65  0x2B,
66  0x2E,
67  0x31,
68  0x34,
69  0x37,
70  0x3A,
71  0x3D,
72  0x40,
73  0x42,
74  0x45,
75  0x48,
76  0x4A,
77  0x4D,
78  0x4F,
79  0x52,
80  0x54,
81  0x56,
82  0x59,
83  0x5B,
84  0x5D,
85  0x5F,
86  0x61,
87  0x63,
88  0x65,
89  0x67,
90  0x69,
91  0x6B,
92  0x6C,
93  0x6E,
94  0x70,
95  0x71,
96  0x72,
97  0x74,
98  0x75,
99  0x76,
100  0x77,
101  0x78,
102  0x79,
103  0x7A,
104  0x7B,
105  0x7C,
106  0x7C,
107  0x7D,
108  0x7D,
109  0x7E,
110  0x7E,
111  0x7E,
112  0x7E,
113  0x7F,
114  0x7F
115 };
116 
117 /***************************************************************************/
132 /*
133 #pragma NO_ENTRY
134 #pragma NO_EXIT
135 #pragma NO_RETURN
136 #pragma NO_FRAME
137 */
138 static Byte D4D_Divide8(Word num, Byte div)
139 {
140  D4D_UNUSED(num);
141  D4D_UNUSED(div);
142 
143 #if (D4D_MCU_TYPE == D4D_HC08) || (D4D_MCU_TYPE == D4D_HCS08)
144  asm
145  {
146  PSHA
147  PSHX
148  PSHH
149 
150  LSRA /* div/2 */
151  ADD 2,SP /* add div/2 to num on the stack */
152  STA 2,SP
153  CLRA
154  ADC 1,SP
155  STA 1,SP
156 
157  PULH
158  PULA /* HA = num + div/2 */
159  PULX /* X = div */
160 
161  DIV /* A=HA/X, H=HA%X */
162  BCC ok /* return A when okay */
163  LDA #0xff /* return 255 on overflow */
164  ok: RTS
165  }
166 
167  /* ignored dead code (just for C syntax) */
168  return 0;
169 #elif (D4D_MCU_TYPE == D4D_HC12) || (D4D_MCU_TYPE == D4D_HCS12) || (D4D_MCU_TYPE == D4D_HCS12X)
170  return (Byte)(num/div);
171 #elif (D4D_MCU_TYPE == D4D_MCF51) || (D4D_MCU_TYPE == D4D_MCF52) || (D4D_MCU_TYPE == D4D_MPC51) || (D4D_MCU_TYPE == D4D_MK)
172  return (Byte)(num/div);
173 #else
174  #error "Not supported MCU for D4D math divide!"
175 #endif
176 
177  // ignored dead code (just for C syntax)
178 
179 }
180 
181 static Word D4D_Divide16(LWord num, Word div)
182 {
183  return (Word)(num/div);
184 
185 }
186 
187 /******************************************************************************
188 * Begin of D4D_MATH public functions
189 */
193 /***************************************************************************/
207 {
208  Word n = (Word)(((Word)u1 * (Word)u2) + (d / 2));
209  return D4D_Divide8(n, d);
210 }
211 
212 /***************************************************************************/
226 {
227  Word n;
228  Byte r;
229 
230  if(s >= 0)
231  {
232  n = (Word)(((Byte)s * (Word)u) + (d / 2));
233  r = D4D_Divide8(n, d);
234  return (sByte)((r > (Byte)127)? 127 : r);
235  }
236  else
237  {
238  n = (Word)((((Byte)-s) * (Word)u) + (d / 2));
239  r = D4D_Divide8(n, d);
240  return (sByte)(r > 128 ? -128 : (sByte)(-1 * ((sByte)r)));
241  }
242 }
243 
244 /***************************************************************************/
258 {
259  LWord n = (LWord)((LWord)((LWord)u1 * (LWord)u2) + (d / 2));
260  return D4D_Divide16(n, d);
261 }
262 
263 /***************************************************************************/
277 {
278  LWord n;
279  Word r;
280 
281  if(s >= 0)
282  {
283  n = (LWord)(((Word)s * (LWord)u) + (d / 2));
284  r = D4D_Divide16(n, d);
285  return (sWord)((r > (Word)0x7FFF)? 0x7FFF : r);
286  }
287  else
288  {
289  n = (LWord)((((Word)-s) * (LWord)u)) + (d / 2);
290  r = D4D_Divide16(n, d);
291  return (sWord)(r > 0x8000 ? -32768 : (sWord)(-1 * ((sWord)r)));
292  }
293 }
294 
295 /***************************************************************************/
307 {
308  if(num < 0)
309  num = (sByte)(num * -1);
310 
311  return (Byte)num;
312 }
313 
314 /***************************************************************************/
326 {
327  if(num < 0)
328  num = (sWord)(num * -1);
329 
330  return (Word)num;
331 }
332 
333 /***************************************************************************/
345 {
346  if(num < 0)
347  num = (sLWord)(num * -1);
348 
349  return (LWord)num;
350 }
351 
352 /***************************************************************************/
368 {
369  if(val < min)
370  return min;
371 
372  if(val > max)
373  return max;
374 
375 
376  return val;
377 }
378 
379 /***************************************************************************/
395 {
396  if(val < min)
397  return min;
398 
399  if(val > max)
400  return max;
401 
402 
403  return val;
404 }
405 
406 /***************************************************************************/
421 Byte D4D_LimitU8(Byte val, Byte min, Byte max)
422 {
423  if(val < min)
424  return min;
425 
426  if(val > max)
427  return max;
428 
429 
430  return val;
431 }
432 
433 /***************************************************************************/
449 {
450  if(val < min)
451  return min;
452 
453  if(val > max)
454  return max;
455 
456  return val;
457 }
458 
459 /***************************************************************************/
473 {
474  if(x < 0x80)
475  {
476  if(x <= 0x3F)
477  return d4d_sinTbl[x];
478  else
479  return d4d_sinTbl[0x3F-(x-0x40)];
480  }
481  else
482  {
483  if (x <= 0xBF)
484  return (sByte)(-d4d_sinTbl[x-0x80]);
485  else
486  return (sByte)(-d4d_sinTbl[0x3F-(x-0xC0)]);
487  }
488 }
489 
490 /***************************************************************************/
503 {
504  if(x < 0x80)
505  {
506  if(x <= 0x3F)
507  return d4d_sinTbl[0x3F-x];
508  else
509  return (sByte)(-d4d_sinTbl[x-0x40]);
510  }
511  else
512  {
513  if (x <= 0xBF)
514  return (sByte)(-d4d_sinTbl[0x3F-(x-0x80)]);
515  else
516  return d4d_sinTbl[x-0xC0];
517  }
518 }
519 
520 /******************************************************************************
521 * End of public functions */
523 /******************************************************************************/
524 
525 /******************************************************************************
526 * Private functions *
527 ******************************************************************************/
Word D4D_MulDivUU16(Word u1, Word u2, Word d)
Simple proportion unsigned calculation - 16 bit.
Definition: d4d_math.c:257
const sByte d4d_sinTbl[64]
Definition: d4d_math.c:49
signed long sLWord
Type definition of sLWord (signed 32-bit).
Definition: d4d_types.h:171
Word D4D_Abs16(sWord num)
Calculation of absolute value - 16 bit.
Definition: d4d_math.c:325
D4D Driver main header file.
static Byte D4D_Divide8(Word num, Byte div)
Fast 16/8=8 divide operation with rounding and saturation.
Definition: d4d_math.c:138
sByte D4D_LimitS8(sByte val, sByte min, sByte max)
Calculation of limitation value (signed) - 8 bit.
Definition: d4d_math.c:448
D4D Driver private header file.
static Word D4D_Divide16(LWord num, Word div)
Definition: d4d_math.c:181
unsigned char Byte
Type definition of Byte (unsigned 8-bit).
Definition: d4d_types.h:151
sByte D4D_Sin(Byte x)
Sine calculation using lookup table.
Definition: d4d_math.c:472
unsigned long LWord
Type definition of LWord (unsigned 32-bit).
Definition: d4d_types.h:167
signed short sWord
Type definition of sWord (signed 16-bit).
Definition: d4d_types.h:163
Byte D4D_MulDivUU8(Byte u1, Byte u2, Byte d)
Simple proportion unsigned calculation - 8 bit.
Definition: d4d_math.c:206
Byte D4D_LimitU8(Byte val, Byte min, Byte max)
Calculation of limitation value (unsigned) - 8 bit.
Definition: d4d_math.c:421
sWord D4D_LimitS16(sWord val, sWord min, sWord max)
Calculation of limitation value (signed) - 16 bit.
Definition: d4d_math.c:394
signed char sByte
Type definition of sByte (signed 8-bit).
Definition: d4d_types.h:155
Word D4D_LimitU16(Word val, Word min, Word max)
Calculation of limitation value (unsigned) - 16 bit.
Definition: d4d_math.c:367
#define D4D_UNUSED(x)
Macro used just for notify compiler that the input parameter is not used.
Definition: d4d_base.h:504
sWord D4D_MulDivSU16(sWord s, Word u, Word d)
Simple proportion signed calculation - 8 bit.
Definition: d4d_math.c:276
Byte D4D_Abs(sByte num)
Calculation of absolute value - 8 bit.
Definition: d4d_math.c:306
sByte D4D_Cos(Byte x)
Cosine calculation using lookup table.
Definition: d4d_math.c:502
sByte D4D_MulDivSU8(sByte s, Byte u, Byte d)
Simple proportion signed calculation - 8 bit.
Definition: d4d_math.c:225
LWord D4D_Abs32(sLWord num)
Calculation of absolute value - 32 bit.
Definition: d4d_math.c:344
unsigned short Word
Type definition of Word (unsigned 16-bit).
Definition: d4d_types.h:159