| 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 | #ifndef MMAL_UTIL_RATIONAL_H |
| 29 | #define MMAL_UTIL_RATIONAL_H |
| 30 | |
| 31 | #include "interface/mmal/mmal_types.h" |
| 32 | |
| 33 | /** \defgroup MmalRationalUtilities Rational Utility Functions |
| 34 | * \ingroup MmalUtilities |
| 35 | * The rational utility functions allow easy manipulation of rational numbers. |
| 36 | * |
| 37 | * @{ |
| 38 | */ |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /** Add 2 rational numbers. |
| 45 | * It is assumed that both input rational numbers are in |
| 46 | * their simplest form. |
| 47 | * |
| 48 | * @param a First operand |
| 49 | * @param b Second operand |
| 50 | * |
| 51 | * @return a + b |
| 52 | */ |
| 53 | MMAL_RATIONAL_T mmal_rational_add(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b); |
| 54 | |
| 55 | /** Subtract 2 rational numbers. |
| 56 | * It is assumed that both input rational numbers are in |
| 57 | * their simplest form. |
| 58 | * |
| 59 | * @param a First operand |
| 60 | * @param b Second operand |
| 61 | * |
| 62 | * @return a - b |
| 63 | */ |
| 64 | MMAL_RATIONAL_T mmal_rational_subtract(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b); |
| 65 | |
| 66 | /** Multiply 2 rational numbers. |
| 67 | * It is assumed that both input rational numbers are in |
| 68 | * their simplest form. |
| 69 | * |
| 70 | * @param a First operand |
| 71 | * @param b Second operand |
| 72 | * |
| 73 | * @return a * b |
| 74 | */ |
| 75 | MMAL_RATIONAL_T mmal_rational_multiply(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b); |
| 76 | |
| 77 | /** Divide 2 rational numbers. |
| 78 | * It is assumed that both input rational numbers are in |
| 79 | * their simplest form. |
| 80 | * |
| 81 | * @param a First operand |
| 82 | * @param b Second operand |
| 83 | * |
| 84 | * @return a / b |
| 85 | */ |
| 86 | MMAL_RATIONAL_T mmal_rational_divide(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b); |
| 87 | |
| 88 | /** Convert a rational number to a 32-bit signed Q16 number. |
| 89 | * Saturation will occur for rational numbers with an absolute |
| 90 | * value greater than 32768. |
| 91 | * |
| 92 | * @param rational Rational number to convert |
| 93 | * |
| 94 | * @return 32-bit signed Q16 number |
| 95 | */ |
| 96 | int32_t mmal_rational_to_fixed_16_16(MMAL_RATIONAL_T rational); |
| 97 | |
| 98 | /** Convert a signed 32-bit Q16 number to a rational number. |
| 99 | * |
| 100 | * @param fixed Signed 32-bit Q16 number to convert |
| 101 | * |
| 102 | * @return Rational number |
| 103 | */ |
| 104 | MMAL_RATIONAL_T mmal_rational_from_fixed_16_16(int32_t fixed); |
| 105 | |
| 106 | /** Reduce a rational number to it's simplest form. |
| 107 | * |
| 108 | * @param rational Rational number to simplify |
| 109 | */ |
| 110 | void mmal_rational_simplify(MMAL_RATIONAL_T *rational); |
| 111 | |
| 112 | /** Test 2 rational numbers for equality. |
| 113 | * |
| 114 | * @param a First operand |
| 115 | * @param b Second operand |
| 116 | * |
| 117 | * @return true if equal |
| 118 | */ |
| 119 | MMAL_BOOL_T mmal_rational_equal(MMAL_RATIONAL_T a, MMAL_RATIONAL_T b); |
| 120 | |
| 121 | #ifdef __cplusplus |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | /** @} */ |
| 126 | |
| 127 | #endif |
| 128 | |