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 
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.
16 // Algorithms that rely on denormalized numbers need alternative implementations.
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
21 #endif
22 
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  */
SkApplySign(int32_t n,int32_t sign)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 */
SkCopySign32(int32_t x,int32_t 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  */
SkClampUMax(unsigned value,unsigned max)45 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
46     if (value > max) {
47         value = max;
48     }
49     return value;
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 
54 /** Return a*b/255, truncating away any fractional bits. Only valid if both
55  a and b are 0..255
56  */
SkMulDiv255Trunc(U8CPU a,U8CPU b)57 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
58     SkASSERT((uint8_t)a == a);
59     SkASSERT((uint8_t)b == b);
60     unsigned prod = a*b + 1;
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  */
SkMulDiv255Ceiling(U8CPU a,U8CPU b)67 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
68     SkASSERT((uint8_t)a == a);
69     SkASSERT((uint8_t)b == b);
70     unsigned prod = a*b + 255;
71     return (prod + (prod >> 8)) >> 8;
72 }
73 
74 /** Just the rounding step in SkDiv255Round: round(value / 255)
75  */
SkDiv255Round(unsigned prod)76 static inline unsigned SkDiv255Round(unsigned prod) {
77     prod += 128;
78     return (prod + (prod >> 8)) >> 8;
79 }
80 
SkPinToUnitFloat(float x)81 static inline float SkPinToUnitFloat(float x) {
82     return SkTMin(SkTMax(x, 0.0f), 1.0f);
83 }
84 
85 /**
86  * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
87  */
88 #if defined(_MSC_VER)
89     #include <intrin.h>
SkBSwap32(uint32_t v)90     static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
91 #else
SkBSwap32(uint32_t v)92     static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
93 #endif
94 
95 #endif
96