reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkMathPriv_DEFINED |
| 9 | #define SkMathPriv_DEFINED |
| 10 | |
| 11 | #include "SkMath.h" |
| 12 | |
bungeman | 4760f32 | 2015-06-24 13:08:51 -0700 | [diff] [blame] | 13 | #if defined(SK_BUILD_FOR_IOS) && (defined(SK_BUILD_FOR_ARM32) || defined(SK_BUILD_FOR_ARM64)) |
| 14 | // iOS on ARM starts processes with the Flush-To-Zero (FTZ) and |
| 15 | // Denormals-Are-Zero (DAZ) bits in the fpscr register set. |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 16 | // Algorithms that rely on denormalized numbers need alternative implementations. |
bungeman | 4760f32 | 2015-06-24 13:08:51 -0700 | [diff] [blame] | 17 | // This can also be controlled in SSE with the MXCSR register, |
| 18 | // x87 with FSTCW/FLDCW, and mips with FCSR. This should be detected at runtime, |
| 19 | // or the library built one way or the other more generally (by the build). |
| 20 | #define SK_CPU_FLUSH_TO_ZERO |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 21 | #endif |
| 22 | |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 23 | /** Returns -1 if n < 0, else returns 0 |
| 24 | */ |
| 25 | #define SkExtractSign(n) ((int32_t)(n) >> 31) |
| 26 | |
| 27 | /** If sign == -1, returns -n, else sign must be 0, and returns n. |
| 28 | Typically used in conjunction with SkExtractSign(). |
| 29 | */ |
| 30 | static inline int32_t SkApplySign(int32_t n, int32_t sign) { |
| 31 | SkASSERT(sign == 0 || sign == -1); |
| 32 | return (n ^ sign) - sign; |
| 33 | } |
| 34 | |
| 35 | /** Return x with the sign of y */ |
| 36 | static inline int32_t SkCopySign32(int32_t x, int32_t y) { |
| 37 | return SkApplySign(x, SkExtractSign(x ^ y)); |
| 38 | } |
| 39 | |
| 40 | /** Given a positive value and a positive max, return the value |
| 41 | pinned against max. |
| 42 | Note: only works as long as max - value doesn't wrap around |
| 43 | @return max if value >= max, else value |
| 44 | */ |
| 45 | static inline unsigned SkClampUMax(unsigned value, unsigned max) { |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 46 | if (value > max) { |
| 47 | value = max; |
| 48 | } |
| 49 | return value; |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 50 | } |
| 51 | |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
| 54 | /** Return a*b/255, truncating away any fractional bits. Only valid if both |
| 55 | a and b are 0..255 |
| 56 | */ |
| 57 | static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) { |
| 58 | SkASSERT((uint8_t)a == a); |
| 59 | SkASSERT((uint8_t)b == b); |
mtklein | 3848427 | 2015-08-07 08:48:12 -0700 | [diff] [blame] | 60 | unsigned prod = a*b + 1; |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 61 | return (prod + (prod >> 8)) >> 8; |
| 62 | } |
| 63 | |
| 64 | /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if |
| 65 | both a and b are 0..255. The expected result equals (a * b + 254) / 255. |
| 66 | */ |
| 67 | static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) { |
| 68 | SkASSERT((uint8_t)a == a); |
| 69 | SkASSERT((uint8_t)b == b); |
mtklein | 3848427 | 2015-08-07 08:48:12 -0700 | [diff] [blame] | 70 | unsigned prod = a*b + 255; |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 71 | return (prod + (prod >> 8)) >> 8; |
| 72 | } |
| 73 | |
| 74 | /** Just the rounding step in SkDiv255Round: round(value / 255) |
| 75 | */ |
| 76 | static inline unsigned SkDiv255Round(unsigned prod) { |
| 77 | prod += 128; |
| 78 | return (prod + (prod >> 8)) >> 8; |
| 79 | } |
| 80 | |
reed | 353c148 | 2016-01-21 15:29:10 -0800 | [diff] [blame^] | 81 | static inline float SkPinToUnitFloat(float x) { |
| 82 | return SkTMin(SkTMax(x, 0.0f), 1.0f); |
| 83 | } |
| 84 | |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 85 | #endif |