Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: include/core/SkFixed.h

Issue 1439483002: Fix code that left shifts a negative value. This has undefined behavior. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add comment about unsigned cast. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/ClampRangeTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkFixed_DEFINED 8 #ifndef SkFixed_DEFINED
9 #define SkFixed_DEFINED 9 #define SkFixed_DEFINED
10 10
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) 53 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
54 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) 54 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
55 55
56 /** Converts an integer to a SkFixed, asserting that the result does not overflo w 56 /** Converts an integer to a SkFixed, asserting that the result does not overflo w
57 a 32 bit signed integer 57 a 32 bit signed integer
58 */ 58 */
59 #ifdef SK_DEBUG 59 #ifdef SK_DEBUG
60 inline SkFixed SkIntToFixed(int n) 60 inline SkFixed SkIntToFixed(int n)
61 { 61 {
62 SkASSERT(n >= -32768 && n <= 32767); 62 SkASSERT(n >= -32768 && n <= 32767);
63 return n << 16; 63 // Left shifting a negative value has undefined behavior in C, so we cas t to unsigned before
64 // shifting.
65 return (unsigned)n << 16;
64 } 66 }
65 #else 67 #else
66 // force the cast to SkFixed to ensure that the answer is signed (like the debug version) 68 // Left shifting a negative value has undefined behavior in C, so we cast to unsigned before
67 #define SkIntToFixed(n) (SkFixed)((n) << 16) 69 // shifting. Then we force the cast to SkFixed to ensure that the answer is signed (like the
70 // debug version).
71 #define SkIntToFixed(n) (SkFixed)((unsigned)(n) << 16)
68 #endif 72 #endif
69 73
70 #define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16) 74 #define SkFixedRoundToInt(x) (((x) + SK_FixedHalf) >> 16)
71 #define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16) 75 #define SkFixedCeilToInt(x) (((x) + SK_Fixed1 - 1) >> 16)
72 #define SkFixedFloorToInt(x) ((x) >> 16) 76 #define SkFixedFloorToInt(x) ((x) >> 16)
73 77
74 #define SkFixedRoundToFixed(x) (((x) + SK_FixedHalf) & 0xFFFF0000) 78 #define SkFixedRoundToFixed(x) (((x) + SK_FixedHalf) & 0xFFFF0000)
75 #define SkFixedCeilToFixed(x) (((x) + SK_Fixed1 - 1) & 0xFFFF0000) 79 #define SkFixedCeilToFixed(x) (((x) + SK_Fixed1 - 1) & 0xFFFF0000)
76 #define SkFixedFloorToFixed(x) ((x) & 0xFFFF0000) 80 #define SkFixedFloorToFixed(x) ((x) & 0xFFFF0000)
77 81
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16) 155 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16)
152 156
153 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { 157 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) {
154 return static_cast<float>(x * 1.5258789e-5); // x * (1.0f / (1 << 16)) 158 return static_cast<float>(x * 1.5258789e-5); // x * (1.0f / (1 << 16))
155 } 159 }
156 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16))) 160 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16)))
157 161
158 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x) 162 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x)
159 163
160 #endif 164 #endif
OLDNEW
« no previous file with comments | « no previous file | tests/ClampRangeTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698