| 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 | static INLINE bool clean_boolean(int b) |
| 29 | { |
| 30 | return !!b; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | clean_float(float f) |
| 35 | |
| 36 | Implementation notes: |
| 37 | |
| 38 | 'Cleans' a floating point number by converting -INF to -FLT_MAX, INF to FLT_MAX and |
| 39 | any NaN to zero. Khronos spec says nothing about what happens if an unclean float is |
| 40 | passed to a function, so this at least guarantees reasonable behaviour. |
| 41 | |
| 42 | Preconditions: |
| 43 | |
| 44 | - |
| 45 | |
| 46 | Postconditions: |
| 47 | |
| 48 | result r is not NaN or infinite |
| 49 | */ |
| 50 | |
| 51 | static INLINE float clean_float(float f) |
| 52 | { |
| 53 | uint32_t u = float_to_bits(f); |
| 54 | |
| 55 | if (u == 0x7f800000) |
| 56 | return FLT_MAX; |
| 57 | if (u == (uint32_t)0xff800000) |
| 58 | return -FLT_MAX; |
| 59 | if ((u & 0x7f800000) == 0x7f800000) |
| 60 | return 0; // force NaN to zero |
| 61 | |
| 62 | return f; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | clampf(float x, float l, float u) |
| 67 | |
| 68 | Implementation notes: |
| 69 | |
| 70 | Khronos makes frequent use of the datatype clampf, which is defined as |
| 71 | |
| 72 | floating-point value clamped to [0, 1] |
| 73 | |
| 74 | This function is used in many places to clamp a floating point value to lie in a range |
| 75 | (almost always 0..1) and also to implicitly remove any NaNs or infinities. |
| 76 | |
| 77 | Preconditions: |
| 78 | |
| 79 | l and u are not NaN or infinity and l <= u |
| 80 | |
| 81 | Postconditions: |
| 82 | |
| 83 | result r is not NaN or an infinity, and l <= r <= u |
| 84 | */ |
| 85 | |
| 86 | static INLINE float clampf(float x, float l, float u) |
| 87 | { |
| 88 | vcos_assert(l <= u); |
| 89 | |
| 90 | x = clean_float(x); |
| 91 | |
| 92 | return _maxf(_minf(x, u), l); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | fixed_to_float(int f) |
| 97 | |
| 98 | Implementation notes: |
| 99 | |
| 100 | OpenGL ES 1.1 makes use of the datatype 'fixed' which is defined as a: |
| 101 | |
| 102 | signed 2s complement 16.16 scaled integer |
| 103 | |
| 104 | This function is used to convert a fixed value to its floating point equivalent, with |
| 105 | some loss of precision. We believe this is justified as the state tables in the spec |
| 106 | talk about storing all these values as type R (floating-point number). |
| 107 | |
| 108 | Preconditions: |
| 109 | |
| 110 | - |
| 111 | |
| 112 | Postconditions: |
| 113 | |
| 114 | result r is not NaN or an infinity |
| 115 | */ |
| 116 | |
| 117 | static INLINE float fixed_to_float(int f) |
| 118 | { |
| 119 | return (float)f / 65536.0f; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | clampi(int x, int l, int u) |
| 124 | |
| 125 | Implementation notes: |
| 126 | |
| 127 | Preconditions: |
| 128 | |
| 129 | l <= u |
| 130 | |
| 131 | Postconditions: |
| 132 | |
| 133 | result r satisfies l <= r <= u |
| 134 | */ |
| 135 | |
| 136 | static INLINE int clampi(int x, int l, int u) |
| 137 | { |
| 138 | vcos_assert(l <= u); |
| 139 | |
| 140 | return (int) _max(_min( (int32_t)x, (int32_t)u), (int32_t)l); |
| 141 | } |
| 142 | |