/[dtapublic]/to_be_filed/uculib01/src/stm8/cosmic/modxx/atu16ratadjrrxx/src/atu16ratadjrrxx.c
ViewVC logotype

Contents of /to_be_filed/uculib01/src/stm8/cosmic/modxx/atu16ratadjrrxx/src/atu16ratadjrrxx.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download)
Sat Oct 8 07:22:17 2016 UTC (7 years, 8 months ago) by dashley
File MIME type: text/plain
File size: 3713 byte(s)
Initial commit.
1 //-------------------------------------------------------------------------------
2 //$Header: /home/dashley/cvsrep/uculib01/uculib01/src/stm8/cosmic/modxx/atu16ratadjrrxx/src/atu16ratadjrrxx.c,v 1.4 2010/02/09 01:42:32 dashley Exp $
3 //-------------------------------------------------------------------------------
4 //Copyright (c)2010 David T. Ashley
5 //
6 //Permission is hereby granted, free of charge, to any person obtaining a copy
7 //of this software source code and associated documentation files (the
8 //"Software"), to deal in the Software without restriction, including without
9 //limitation the rights to use, copy, modify, merge, publish, distribute,
10 //sublicense, and/or sell copies of the Software, and to permit persons to whom
11 //the Software is furnished to do so, subject to the following conditions:
12 //
13 //The above copyright notice and this permission notice shall be included in
14 //all copies or substantial portions of the Software.
15 //
16 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 //THE SOFTWARE.
23 //-------------------------------------------------------------------------------
24
25 #include "uculib.h"
26
27
28 UCU_UINT16 UcuAtU16RatAdjRRxx(UCU_UINT16 arg, UCU_UINT16 arg_max, UCU_UINT16 adj_in, UCU_UINT16 adj_nom)
29 {
30 UCU_UINT32 rv;
31
32 if (adj_in == 0)
33 {
34 if (arg == 0)
35 {
36 return(0);
37 }
38 else
39 {
40 return(arg_max);
41 }
42 }
43 else if (adj_nom == 0)
44 {
45 return(0);
46 }
47 else
48 {
49 rv = (UCU_UINT32)arg * (UCU_UINT32)adj_nom;
50 rv += ((adj_in - 1) >> 1);
51 //adj_in can't be 0 because of the test above and so can't roll over
52 //less than zero.
53 //
54 //The addition step above can't overflow. Let W be the number of bit
55 //patterns for an unsigned (16 bits), so that the unsigned value itself will be
56 //in the range [0..W-1]. For a variable twice as long (32 bits), the number of
57 //bit patterns is W**2, so that the variable itself is [0..W**2-1].
58 //In a worst case, we are squaring the maximum 16-bit integer and adding the
59 //maximum 16-bit integer, i.e.
60 // (W-1)**2 + W - 1
61 //and we want to know if we can exceed
62 // W**2 - 1.
63 //Doing the algebra:
64 // (W-1)**2 + W - 1
65 // = W**2 - 2W + 1 + W - 1
66 // = W**2 - W.
67 //Clearly (W**2 - W) < (W**2 - 1), so this is OK.
68
69 rv /= adj_in;
70
71 if (rv > arg_max)
72 {
73 rv = arg_max;
74 }
75
76 return(rv);
77 }
78 }
79
80
81 //-------------------------------------------------------------------------------
82 //End of $Id: atu16ratadjrrxx.c,v 1.4 2010/02/09 01:42:32 dashley Exp $
83 //-------------------------------------------------------------------------------
84 //$Log: atu16ratadjrrxx.c,v $
85 //Revision 1.4 2010/02/09 01:42:32 dashley
86 //Rounding logic simplified.
87 //
88 //Revision 1.3 2010/01/30 20:27:58 dashley
89 //Addition of U16 ratiometric adjustment function.
90 //
91 //Revision 1.2 2010/01/22 17:51:48 dashley
92 //Extra line removed.
93 //
94 //Revision 1.1 2010/01/22 17:51:30 dashley
95 //Initial checkin.
96 //-------------------------------------------------------------------------------
97

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25