gfx/skia/trunk/src/core/SkMathPriv.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef SkMathPriv_DEFINED
michael@0 9 #define SkMathPriv_DEFINED
michael@0 10
michael@0 11 #include "SkMath.h"
michael@0 12
michael@0 13 /** Returns -1 if n < 0, else returns 0
michael@0 14 */
michael@0 15 #define SkExtractSign(n) ((int32_t)(n) >> 31)
michael@0 16
michael@0 17 /** If sign == -1, returns -n, else sign must be 0, and returns n.
michael@0 18 Typically used in conjunction with SkExtractSign().
michael@0 19 */
michael@0 20 static inline int32_t SkApplySign(int32_t n, int32_t sign) {
michael@0 21 SkASSERT(sign == 0 || sign == -1);
michael@0 22 return (n ^ sign) - sign;
michael@0 23 }
michael@0 24
michael@0 25 /** Return x with the sign of y */
michael@0 26 static inline int32_t SkCopySign32(int32_t x, int32_t y) {
michael@0 27 return SkApplySign(x, SkExtractSign(x ^ y));
michael@0 28 }
michael@0 29
michael@0 30 /** Given a positive value and a positive max, return the value
michael@0 31 pinned against max.
michael@0 32 Note: only works as long as max - value doesn't wrap around
michael@0 33 @return max if value >= max, else value
michael@0 34 */
michael@0 35 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
michael@0 36 if (value > max) {
michael@0 37 value = max;
michael@0 38 }
michael@0 39 return value;
michael@0 40 }
michael@0 41
michael@0 42 ///////////////////////////////////////////////////////////////////////////////
michael@0 43
michael@0 44 /** Return a*b/255, truncating away any fractional bits. Only valid if both
michael@0 45 a and b are 0..255
michael@0 46 */
michael@0 47 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
michael@0 48 SkASSERT((uint8_t)a == a);
michael@0 49 SkASSERT((uint8_t)b == b);
michael@0 50 unsigned prod = SkMulS16(a, b) + 1;
michael@0 51 return (prod + (prod >> 8)) >> 8;
michael@0 52 }
michael@0 53
michael@0 54 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
michael@0 55 both a and b are 0..255. The expected result equals (a * b + 254) / 255.
michael@0 56 */
michael@0 57 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
michael@0 58 SkASSERT((uint8_t)a == a);
michael@0 59 SkASSERT((uint8_t)b == b);
michael@0 60 unsigned prod = SkMulS16(a, b) + 255;
michael@0 61 return (prod + (prod >> 8)) >> 8;
michael@0 62 }
michael@0 63
michael@0 64 /** Just the rounding step in SkDiv255Round: round(value / 255)
michael@0 65 */
michael@0 66 static inline unsigned SkDiv255Round(unsigned prod) {
michael@0 67 prod += 128;
michael@0 68 return (prod + (prod >> 8)) >> 8;
michael@0 69 }
michael@0 70
michael@0 71 #endif

mercurial