reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 8 | #include "SkMathPriv.h" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 9 | #include "SkFixed.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkFloatBits.h" |
| 11 | #include "SkFloatingPoint.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkScalar.h" |
| 13 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #define sub_shift(zeros, x, n) \ |
| 15 | zeros -= n; \ |
| 16 | x >>= n |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | int SkCLZ_portable(uint32_t x) { |
| 19 | if (x == 0) { |
| 20 | return 32; |
| 21 | } |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | int zeros = 31; |
| 24 | if (x & 0xFFFF0000) { |
| 25 | sub_shift(zeros, x, 16); |
| 26 | } |
| 27 | if (x & 0xFF00) { |
| 28 | sub_shift(zeros, x, 8); |
| 29 | } |
| 30 | if (x & 0xF0) { |
| 31 | sub_shift(zeros, x, 4); |
| 32 | } |
| 33 | if (x & 0xC) { |
| 34 | sub_shift(zeros, x, 2); |
| 35 | } |
| 36 | if (x & 0x2) { |
| 37 | sub_shift(zeros, x, 1); |
| 38 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | |
| 40 | return zeros; |
| 41 | } |
| 42 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf |
| 46 | */ |
| 47 | int32_t SkSqrtBits(int32_t x, int count) { |
| 48 | SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30); |
| 49 | |
| 50 | uint32_t root = 0; |
| 51 | uint32_t remHi = 0; |
| 52 | uint32_t remLo = x; |
| 53 | |
| 54 | do { |
| 55 | root <<= 1; |
| 56 | |
| 57 | remHi = (remHi<<2) | (remLo>>30); |
| 58 | remLo <<= 2; |
| 59 | |
| 60 | uint32_t testDiv = (root << 1) + 1; |
| 61 | if (remHi >= testDiv) { |
| 62 | remHi -= testDiv; |
| 63 | root++; |
| 64 | } |
| 65 | } while (--count >= 0); |
| 66 | |
| 67 | return root; |
| 68 | } |
| 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | float SkScalarSinCos(float radians, float* cosValue) { |
| 73 | float sinValue = sk_float_sin(radians); |
| 74 | |
| 75 | if (cosValue) { |
| 76 | *cosValue = sk_float_cos(radians); |
| 77 | if (SkScalarNearlyZero(*cosValue)) { |
| 78 | *cosValue = 0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (SkScalarNearlyZero(sinValue)) { |
| 83 | sinValue = 0; |
| 84 | } |
| 85 | return sinValue; |
| 86 | } |