| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <limits.h> |
| 29 | #include "interface/mmal/util/mmal_util_rational.h" |
| 30 | |
| 31 | #define Q16_ONE (1 << 16) |
| 32 | |
| 33 | #define ABS(v) (((v) < 0) ? -(v) : (v)) |
| 34 | |
| 35 | /** Calculate the greatest common denominator between 2 integers. |
| 36 | * Avoids division. */ |
| 37 | static int32_t gcd(int32_t a, int32_t b) |
| 38 | { |
| 39 | int shift; |
| 40 | |
| 41 | if (a == 0 || b == 0) |
| 42 | return 1; |
| 43 | |
| 44 | a = ABS(a); |
| 45 | b = ABS(b); |
| 46 | for (shift = 0; !((a | b) & 0x01); shift++) |
| 47 | a >>= 1, b >>= 1; |
| 48 | |
| 49 | while (a > 0) |
| 50 | { |
| 51 | while (!(a & 0x01)) |
| 52 | a >>= 1; |
| 53 | while (!(b & 0x01)) |
| 54 | b >>= 1; |
| 55 | if (a >= b) |
| 56 | a = (a - b) >> 1; |
| 57 | else |
| 58 | b = (b - a) >> 1; |
| 59 | } |
| 60 | return b << shift; |
| 61 | } |
| 62 | |
| 63 | /** Calculate a + b. */ |
| 64 | MMAL_RATIONAL_T mmal_rational_add(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b) |
| 65 | { |
| 66 | MMAL_RATIONAL_T result; |
| 67 | int32_t g = gcd(a.den, b.den); |
| 68 | a.den /= g; |
| 69 | a.num = a.num * (b.den / g) + b.num * a.den; |
| 70 | g = gcd(a.num, g); |
| 71 | a.num /= g; |
| 72 | a.den *= b.den / g; |
| 73 | |
| 74 | result.num = a.num; |
| 75 | result.den = a.den; |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /** Calculate a - b. */ |
| 80 | MMAL_RATIONAL_T mmal_rational_subtract(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b) |
| 81 | { |
| 82 | b.num = -b.num; |
| 83 | return mmal_rational_add(a, b); |
| 84 | } |
| 85 | |
| 86 | /** Calculate a * b */ |
| 87 | MMAL_RATIONAL_T mmal_rational_multiply(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b) |
| 88 | { |
| 89 | MMAL_RATIONAL_T result; |
| 90 | int32_t gcd1 = gcd(a.num, b.den); |
| 91 | int32_t gcd2 = gcd(b.num, a.den); |
| 92 | result.num = (a.num / gcd1) * (b.num / gcd2); |
| 93 | result.den = (a.den / gcd2) * (b.den / gcd1); |
| 94 | |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | /** Calculate a / b */ |
| 99 | MMAL_RATIONAL_T mmal_rational_divide(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b) |
| 100 | { |
| 101 | MMAL_RATIONAL_T result; |
| 102 | int32_t gcd1, gcd2; |
| 103 | |
| 104 | if (b.num == 0) |
| 105 | { |
| 106 | vcos_assert(0); |
| 107 | return a; |
| 108 | } |
| 109 | |
| 110 | if (a.num == 0) |
| 111 | return a; |
| 112 | |
| 113 | gcd1 = gcd(a.num, b.num); |
| 114 | gcd2 = gcd(b.den, a.den); |
| 115 | result.num = (a.num / gcd1) * (b.den / gcd2); |
| 116 | result.den = (a.den / gcd2) * (b.num / gcd1); |
| 117 | |
| 118 | return result; |
| 119 | } |
| 120 | |
| 121 | /** Convert a rational number to a signed 32-bit Q16 number. */ |
| 122 | int32_t mmal_rational_to_fixed_16_16(MMAL_RATIONAL_T rational) |
| 123 | { |
| 124 | int64_t result = (int64_t)rational.num << 16; |
| 125 | if (rational.den) |
| 126 | result /= rational.den; |
| 127 | |
| 128 | if (result > INT_MAX) |
| 129 | result = INT_MAX; |
| 130 | else if (result < INT_MIN) |
| 131 | result = INT_MIN; |
| 132 | |
| 133 | return (int32_t)result; |
| 134 | } |
| 135 | |
| 136 | /** Convert a rational number to a signed 32-bit Q16 number. */ |
| 137 | MMAL_RATIONAL_T mmal_rational_from_fixed_16_16(int32_t fixed) |
| 138 | { |
| 139 | MMAL_RATIONAL_T result = { fixed, Q16_ONE }; |
| 140 | mmal_rational_simplify(&result); |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /** Reduce a rational number to it's simplest form. */ |
| 145 | void mmal_rational_simplify(MMAL_RATIONAL_T *rational) |
| 146 | { |
| 147 | int g = gcd(rational->num, rational->den); |
| 148 | rational->num /= g; |
| 149 | rational->den /= g; |
| 150 | } |
| 151 | |
| 152 | /** Tests for equality */ |
| 153 | MMAL_BOOL_T mmal_rational_equal(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b) |
| 154 | { |
| 155 | if (a.num != b.num && a.num * (int64_t)b.num == 0) |
| 156 | return MMAL_FALSE; |
| 157 | return a.num * (int64_t)b.den == b.num * (int64_t)a.den; |
| 158 | } |
| 159 | |