| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 half = | 154 half = |
| 155 (static_cast<uint16>(sign) << 15) | | 155 (static_cast<uint16>(sign) << 15) | |
| 156 static_cast<uint16>( | 156 static_cast<uint16>( |
| 157 (exponent - kHalfFloatMinBiasedExpAsSingleFpExponent) >> 13) | | 157 (exponent - kHalfFloatMinBiasedExpAsSingleFpExponent) >> 13) | |
| 158 static_cast<uint16>(mantissa >> 13); | 158 static_cast<uint16>(mantissa >> 13); |
| 159 } | 159 } |
| 160 return half; | 160 return half; |
| 161 } | 161 } |
| 162 | 162 |
| 163 float HalfToFloat(uint16 half) { | 163 float HalfToFloat(uint16 half) { |
| 164 unsigned int value; | |
| 165 unsigned int sign = static_cast<unsigned int>(half >> 15); | 164 unsigned int sign = static_cast<unsigned int>(half >> 15); |
| 166 unsigned int mantissa = static_cast<unsigned int>(half & ((1 << 10) - 1)); | 165 unsigned int mantissa = static_cast<unsigned int>(half & ((1 << 10) - 1)); |
| 167 unsigned int exponent = static_cast<unsigned int>( | 166 unsigned int exponent = static_cast<unsigned int>( |
| 168 half & kHalfFloatMaxBiasedExponent); | 167 half & kHalfFloatMaxBiasedExponent); |
| 169 | 168 |
| 170 if (exponent == kHalfFloatMaxBiasedExponent) { | 169 if (exponent == kHalfFloatMaxBiasedExponent) { |
| 171 // we have a half-float NaN or Inf | 170 // we have a half-float NaN or Inf |
| 172 // half-float NaNs will be converted to a single precision NaN | 171 // half-float NaNs will be converted to a single precision NaN |
| 173 // half-float Infs will be converted to a single precision Inf | 172 // half-float Infs will be converted to a single precision Inf |
| 174 exponent = kFloatMaxBiasedExponent; | 173 exponent = kFloatMaxBiasedExponent; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 191 // shift left to generate single-precision mantissa of 23-bits | 190 // shift left to generate single-precision mantissa of 23-bits |
| 192 mantissa <<= 13; | 191 mantissa <<= 13; |
| 193 } | 192 } |
| 194 } else { | 193 } else { |
| 195 // shift left to generate single-precision mantissa of 23-bits | 194 // shift left to generate single-precision mantissa of 23-bits |
| 196 mantissa <<= 13; | 195 mantissa <<= 13; |
| 197 // generate single precision biased exponent value | 196 // generate single precision biased exponent value |
| 198 exponent = (exponent << 13) + kHalfFloatMinBiasedExpAsSingleFpExponent; | 197 exponent = (exponent << 13) + kHalfFloatMinBiasedExpAsSingleFpExponent; |
| 199 } | 198 } |
| 200 | 199 |
| 201 value = (sign << 31) | exponent | mantissa; | 200 union { |
| 202 return *((float *)&value); | 201 unsigned int int_value; |
| 202 float float_value; |
| 203 } value; |
| 204 value.int_value = (sign << 31) | exponent | mantissa; |
| 205 return value.float_value; |
| 203 } | 206 } |
| 204 | 207 |
| 205 } // namespace Vectormath | 208 } // namespace Vectormath |
| 206 } // namespace Aos | 209 } // namespace Aos |
| 207 | 210 |
| 208 namespace o3d { | 211 namespace o3d { |
| 209 float FrobeniusNorm(const Matrix3& matrix) { | 212 float FrobeniusNorm(const Matrix3& matrix) { |
| 210 Matrix3 elementsSquared = mulPerElem(matrix, matrix); | 213 Matrix3 elementsSquared = mulPerElem(matrix, matrix); |
| 211 Vector3 ones(1, 1, 1); | 214 Vector3 ones(1, 1, 1); |
| 212 float sumOfElementsSquared = 0.0f; | 215 float sumOfElementsSquared = 0.0f; |
| 213 for (int i = 0; i < 3; ++i) { | 216 for (int i = 0; i < 3; ++i) { |
| 214 sumOfElementsSquared += dot(ones, elementsSquared.getCol(i)); | 217 sumOfElementsSquared += dot(ones, elementsSquared.getCol(i)); |
| 215 } | 218 } |
| 216 return sqrtf(sumOfElementsSquared); | 219 return sqrtf(sumOfElementsSquared); |
| 217 } | 220 } |
| 218 | 221 |
| 219 float FrobeniusNorm(const Matrix4& matrix) { | 222 float FrobeniusNorm(const Matrix4& matrix) { |
| 220 Matrix4 elementsSquared = mulPerElem(matrix, matrix); | 223 Matrix4 elementsSquared = mulPerElem(matrix, matrix); |
| 221 Vector4 ones(1, 1, 1, 1); | 224 Vector4 ones(1, 1, 1, 1); |
| 222 float sumOfElementsSquared = 0.0f; | 225 float sumOfElementsSquared = 0.0f; |
| 223 for (int i = 0; i < 4; ++i) { | 226 for (int i = 0; i < 4; ++i) { |
| 224 sumOfElementsSquared += dot(ones, elementsSquared.getCol(i)); | 227 sumOfElementsSquared += dot(ones, elementsSquared.getCol(i)); |
| 225 } | 228 } |
| 226 return sqrtf(sumOfElementsSquared); | 229 return sqrtf(sumOfElementsSquared); |
| 227 } | 230 } |
| 231 |
| 232 const float kPi = ::acosf(-1.0f); |
| 228 } // namespace o3d | 233 } // namespace o3d |
| OLD | NEW |