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

Side by Side Diff: src/core/SkFloatBits.cpp

Issue 1503423003: ubsan shift fixes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add cast to work around win compiler Created 5 years 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 | « src/core/SkFDot6.h ('k') | src/core/SkScan_AntiPath.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 2011 Google Inc. 2 * Copyright 2011 Google Inc.
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 #include "SkFloatBits.h" 8 #include "SkFloatBits.h"
9 #include "SkMathPriv.h" 9 #include "SkMathPriv.h"
10 10
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 exp = 25; 58 exp = 25;
59 } 59 }
60 value >>= exp; 60 value >>= exp;
61 } 61 }
62 return SkApplySign(value, SkExtractSign(packed)); 62 return SkApplySign(value, SkExtractSign(packed));
63 } 63 }
64 64
65 // same as (int)floor(float) 65 // same as (int)floor(float)
66 int32_t SkFloatBits_toIntFloor(int32_t packed) { 66 int32_t SkFloatBits_toIntFloor(int32_t packed) {
67 // curse you negative 0 67 // curse you negative 0
68 if ((packed << 1) == 0) { 68 if (SkLeftShift(packed, 1) == 0) {
69 return 0; 69 return 0;
70 } 70 }
71 71
72 int exp = unpack_exp(packed) - EXP_BIAS; 72 int exp = unpack_exp(packed) - EXP_BIAS;
73 int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; 73 int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
74 74
75 if (exp >= 0) { 75 if (exp >= 0) {
76 if (exp > 7) { // overflow 76 if (exp > 7) { // overflow
77 value = SK_MaxS32; 77 value = SK_MaxS32;
78 } else { 78 } else {
(...skipping 18 matching lines...) Expand all
97 #endif 97 #endif
98 } 98 }
99 // int add = 0; 99 // int add = 0;
100 return value >> exp; 100 return value >> exp;
101 } 101 }
102 } 102 }
103 103
104 // same as (int)floor(float + 0.5) 104 // same as (int)floor(float + 0.5)
105 int32_t SkFloatBits_toIntRound(int32_t packed) { 105 int32_t SkFloatBits_toIntRound(int32_t packed) {
106 // curse you negative 0 106 // curse you negative 0
107 if ((packed << 1) == 0) { 107 if (SkLeftShift(packed, 1) == 0) {
108 return 0; 108 return 0;
109 } 109 }
110 110
111 int exp = unpack_exp(packed) - EXP_BIAS; 111 int exp = unpack_exp(packed) - EXP_BIAS;
112 int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; 112 int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
113 113
114 if (exp >= 0) { 114 if (exp >= 0) {
115 if (exp > 7) { // overflow 115 if (exp > 7) { // overflow
116 value = SK_MaxS32; 116 value = SK_MaxS32;
117 } else { 117 } else {
118 value <<= exp; 118 value <<= exp;
119 } 119 }
120 // apply the sign after we check for overflow 120 // apply the sign after we check for overflow
121 return SkApplySign(value, SkExtractSign(packed)); 121 return SkApplySign(value, SkExtractSign(packed));
122 } else { 122 } else {
123 // apply the sign before we right-shift 123 // apply the sign before we right-shift
124 value = SkApplySign(value, SkExtractSign(packed)); 124 value = SkApplySign(value, SkExtractSign(packed));
125 exp = -exp; 125 exp = -exp;
126 if (exp > 25) { // underflow 126 if (exp > 25) { // underflow
127 exp = 25; 127 exp = 25;
128 } 128 }
129 int add = 1 << (exp - 1); 129 int add = 1 << (exp - 1);
130 return (value + add) >> exp; 130 return (value + add) >> exp;
131 } 131 }
132 } 132 }
133 133
134 // same as (int)ceil(float) 134 // same as (int)ceil(float)
135 int32_t SkFloatBits_toIntCeil(int32_t packed) { 135 int32_t SkFloatBits_toIntCeil(int32_t packed) {
136 // curse you negative 0 136 // curse you negative 0
137 if ((packed << 1) == 0) { 137 if (SkLeftShift(packed, 1) == 0) {
138 return 0; 138 return 0;
139 } 139 }
140 140
141 int exp = unpack_exp(packed) - EXP_BIAS; 141 int exp = unpack_exp(packed) - EXP_BIAS;
142 int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG; 142 int value = unpack_matissa_dirty(packed) | MATISSA_MAGIC_BIG;
143 143
144 if (exp >= 0) { 144 if (exp >= 0) {
145 if (exp > 7) { // overflow 145 if (exp > 7) { // overflow
146 value = SK_MaxS32; 146 value = SK_MaxS32;
147 } else { 147 } else {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 SkASSERT(zeros >= 0 && zeros <= 23); 193 SkASSERT(zeros >= 0 && zeros <= 23);
194 value <<= zeros; 194 value <<= zeros;
195 shift -= zeros; 195 shift -= zeros;
196 } 196 }
197 197
198 // now value is left-aligned to 24 bits 198 // now value is left-aligned to 24 bits
199 SkASSERT((value >> 23) == 1); 199 SkASSERT((value >> 23) == 1);
200 SkASSERT(shift >= 0 && shift <= 255); 200 SkASSERT(shift >= 0 && shift <= 255);
201 201
202 SkFloatIntUnion data; 202 SkFloatIntUnion data;
203 data.fSignBitInt = (sign << 31) | (shift << 23) | (value & ~MATISSA_MAGIC_BI G); 203 data.fSignBitInt = SkLeftShift(sign, 31) | SkLeftShift(shift, 23) | (value & ~MATISSA_MAGIC_BIG);
204 return data.fFloat; 204 return data.fFloat;
205 } 205 }
OLDNEW
« no previous file with comments | « src/core/SkFDot6.h ('k') | src/core/SkScan_AntiPath.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698