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/mips/lithium-codegen-mips.cc

Issue 16951016: MIPS: Optimise Math.floor(x/y) to use integer division for MIPS. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comments & fix test failure: math-floor-of-div Created 7 years, 6 months 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 | Annotate | Revision Log
« no previous file with comments | « src/mips/lithium-codegen-mips.h ('k') | src/mips/lithium-mips.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 1158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 __ mfhi(result_reg); 1169 __ mfhi(result_reg);
1170 1170
1171 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { 1171 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1172 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg)); 1172 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg));
1173 } 1173 }
1174 __ bind(&done); 1174 __ bind(&done);
1175 } 1175 }
1176 } 1176 }
1177 1177
1178 1178
1179 void LCodeGen::EmitSignedIntegerDivisionByConstant(
1180 Register result,
1181 Register dividend,
1182 int32_t divisor,
1183 Register remainder,
1184 Register scratch,
1185 LEnvironment* environment) {
1186 ASSERT(!AreAliased(dividend, scratch, at, no_reg));
1187 ASSERT(LChunkBuilder::HasMagicNumberForDivisor(divisor));
1188
1189 uint32_t divisor_abs = abs(divisor);
1190
1191 int32_t power_of_2_factor =
1192 CompilerIntrinsics::CountTrailingZeros(divisor_abs);
1193
1194 switch (divisor_abs) {
1195 case 0:
1196 DeoptimizeIf(al, environment);
1197 return;
1198
1199 case 1:
1200 if (divisor > 0) {
1201 __ Move(result, dividend);
1202 } else {
1203 __ SubuAndCheckForOverflow(result, zero_reg, dividend, scratch);
1204 DeoptimizeIf(lt, environment, scratch, Operand(zero_reg));
1205 }
1206 // Compute the remainder.
1207 __ Move(remainder, zero_reg);
1208 return;
1209
1210 default:
1211 if (IsPowerOf2(divisor_abs)) {
1212 // Branch and condition free code for integer division by a power
1213 // of two.
1214 int32_t power = WhichPowerOf2(divisor_abs);
1215 if (power > 1) {
1216 __ sra(scratch, dividend, power - 1);
1217 }
1218 __ srl(scratch, scratch, 32 - power);
1219 __ Addu(scratch, dividend, Operand(scratch));
1220 __ sra(result, scratch, power);
1221 // Negate if necessary.
1222 // We don't need to check for overflow because the case '-1' is
1223 // handled separately.
1224 if (divisor < 0) {
1225 ASSERT(divisor != -1);
1226 __ Subu(result, zero_reg, Operand(result));
1227 }
1228 // Compute the remainder.
1229 if (divisor > 0) {
1230 __ sll(scratch, result, power);
1231 __ Subu(remainder, dividend, Operand(scratch));
1232 } else {
1233 __ sll(scratch, result, power);
1234 __ Addu(remainder, dividend, Operand(scratch));
1235 }
1236 return;
1237 } else if (LChunkBuilder::HasMagicNumberForDivisor(divisor)) {
1238 // Use magic numbers for a few specific divisors.
1239 // Details and proofs can be found in:
1240 // - Hacker's Delight, Henry S. Warren, Jr.
1241 // - The PowerPC Compiler Writer's Guide
1242 // and probably many others.
1243 //
1244 // We handle
1245 // <divisor with magic numbers> * <power of 2>
1246 // but not
1247 // <divisor with magic numbers> * <other divisor with magic numbers>
1248 DivMagicNumbers magic_numbers =
1249 DivMagicNumberFor(divisor_abs >> power_of_2_factor);
1250 // Branch and condition free code for integer division by a power
1251 // of two.
1252 const int32_t M = magic_numbers.M;
1253 const int32_t s = magic_numbers.s + power_of_2_factor;
1254
1255 __ li(scratch, Operand(M));
1256 __ mult(dividend, scratch);
1257 __ mfhi(scratch);
1258 if (M < 0) {
1259 __ Addu(scratch, scratch, Operand(dividend));
1260 }
1261 if (s > 0) {
1262 __ sra(scratch, scratch, s);
1263 __ mov(scratch, scratch);
1264 }
1265 __ srl(at, dividend, 31);
1266 __ Addu(result, scratch, Operand(at));
1267 if (divisor < 0) __ Subu(result, zero_reg, Operand(result));
1268 // Compute the remainder.
1269 __ li(scratch, Operand(divisor));
1270 __ Mul(scratch, result, Operand(scratch));
1271 __ Subu(remainder, dividend, Operand(scratch));
1272 } else {
1273 __ li(scratch, Operand(divisor));
1274 __ div(dividend, scratch);
1275 __ mfhi(remainder);
1276 __ mflo(result);
1277 }
1278 }
1279 }
1280
1281
1179 void LCodeGen::DoDivI(LDivI* instr) { 1282 void LCodeGen::DoDivI(LDivI* instr) {
1180 const Register left = ToRegister(instr->left()); 1283 const Register left = ToRegister(instr->left());
1181 const Register right = ToRegister(instr->right()); 1284 const Register right = ToRegister(instr->right());
1182 const Register result = ToRegister(instr->result()); 1285 const Register result = ToRegister(instr->result());
1183 1286
1184 // On MIPS div is asynchronous - it will run in the background while we 1287 // On MIPS div is asynchronous - it will run in the background while we
1185 // check for special cases. 1288 // check for special cases.
1186 __ div(left, right); 1289 __ div(left, right);
1187 1290
1188 // Check for x / 0. 1291 // Check for x / 0.
(...skipping 30 matching lines...) Expand all
1219 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); 1322 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1220 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); 1323 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1221 1324
1222 // This is computed in-place. 1325 // This is computed in-place.
1223 ASSERT(addend.is(ToDoubleRegister(instr->result()))); 1326 ASSERT(addend.is(ToDoubleRegister(instr->result())));
1224 1327
1225 __ madd_d(addend, addend, multiplier, multiplicand); 1328 __ madd_d(addend, addend, multiplier, multiplicand);
1226 } 1329 }
1227 1330
1228 1331
1332 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) {
1333 const Register result = ToRegister(instr->result());
1334 const Register left = ToRegister(instr->left());
1335 const Register remainder = ToRegister(instr->temp());
1336 const Register scratch = scratch0();
1337
1338 Label done;
1339 // We only optimize this for division by constants, because the standard
1340 // integer division routine is usually slower than transitionning to FPU.
Paul Lind 2013/06/17 04:27:28 This statement is not true for mips, where we have
palfia 2013/06/17 11:31:37 Done.
1341 ASSERT(instr->right()->IsConstantOperand());
1342 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->right()));
1343 if (divisor < 0) {
1344 DeoptimizeIf(eq, instr->environment(), left, Operand(zero_reg));
1345 }
1346 EmitSignedIntegerDivisionByConstant(result,
1347 left,
1348 divisor,
1349 remainder,
1350 scratch,
1351 instr->environment());
1352 // We operated a truncating division. Correct the result if necessary.
1353 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1354 __ Xor(scratch , remainder, Operand(divisor));
1355 __ Branch(&done, ge, scratch, Operand(zero_reg));
1356 __ Subu(result, result, Operand(1));
1357 __ bind(&done);
1358 }
1359
1360
1229 void LCodeGen::DoMulI(LMulI* instr) { 1361 void LCodeGen::DoMulI(LMulI* instr) {
1230 Register scratch = scratch0(); 1362 Register scratch = scratch0();
1231 Register result = ToRegister(instr->result()); 1363 Register result = ToRegister(instr->result());
1232 // Note that result may alias left. 1364 // Note that result may alias left.
1233 Register left = ToRegister(instr->left()); 1365 Register left = ToRegister(instr->left());
1234 LOperand* right_op = instr->right(); 1366 LOperand* right_op = instr->right();
1235 1367
1236 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow); 1368 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1237 bool bailout_on_minus_zero = 1369 bool bailout_on_minus_zero =
1238 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero); 1370 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
(...skipping 4442 matching lines...) Expand 10 before | Expand all | Expand 10 after
5681 __ Subu(scratch, result, scratch); 5813 __ Subu(scratch, result, scratch);
5682 __ lw(result, FieldMemOperand(scratch, 5814 __ lw(result, FieldMemOperand(scratch,
5683 FixedArray::kHeaderSize - kPointerSize)); 5815 FixedArray::kHeaderSize - kPointerSize));
5684 __ bind(&done); 5816 __ bind(&done);
5685 } 5817 }
5686 5818
5687 5819
5688 #undef __ 5820 #undef __
5689 5821
5690 } } // namespace v8::internal 5822 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/mips/lithium-codegen-mips.h ('k') | src/mips/lithium-mips.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698