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

Side by Side Diff: src/ia32/lithium-codegen-ia32.cc

Issue 10382033: x86/x64 port of Math.floor(x/y) to use integer division for specific divisor (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 8 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/ia32/disasm-ia32.cc ('k') | src/ia32/lithium-ia32.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 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 // Sign extend to edx. 1015 // Sign extend to edx.
1016 __ cdq(); 1016 __ cdq();
1017 __ idiv(right_reg); 1017 __ idiv(right_reg);
1018 1018
1019 // Deoptimize if remainder is not 0. 1019 // Deoptimize if remainder is not 0.
1020 __ test(edx, Operand(edx)); 1020 __ test(edx, Operand(edx));
1021 DeoptimizeIf(not_zero, instr->environment()); 1021 DeoptimizeIf(not_zero, instr->environment());
1022 } 1022 }
1023 1023
1024 1024
1025 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) {
1026 ASSERT(instr->InputAt(1)->IsConstantOperand());
1027
1028 Register dividend = ToRegister(instr->InputAt(0));
1029 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->InputAt(1)));
1030 Register result = ToRegister(instr->result());
1031
1032 switch (divisor) {
1033 case 0:
1034 DeoptimizeIf(no_condition, instr->environment());
1035 return;
1036
1037 case 1:
1038 __ Move(result, dividend);
1039 return;
1040
1041 case -1:
1042 __ Move(result, dividend);
1043 __ neg(result);
1044 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1045 DeoptimizeIf(zero, instr->environment());
1046 }
1047 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1048 DeoptimizeIf(overflow, instr->environment());
1049 }
1050 return;
1051 }
1052
1053 uint32_t divisor_abs = abs(divisor);
1054 if (IsPowerOf2(divisor_abs)) {
1055 int32_t power = WhichPowerOf2(divisor_abs);
1056 if (divisor < 0) {
1057 // Input[dividend] is clobbered.
1058 // The sequence is tedious because neg(dividend) might overflow.
1059 __ mov(result, dividend);
1060 __ sar(dividend, 31);
1061 __ neg(result);
1062 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1063 DeoptimizeIf(zero, instr->environment());
1064 }
1065 __ shl(dividend, 32 - power);
1066 __ sar(result, power);
1067 __ not_(dividend);
1068 // Clear result.sign if dividend.sign is set.
1069 __ and_(result, dividend);
1070 } else {
1071 __ Move(result, dividend);
1072 __ sar(result, power);
1073 }
1074 } else {
1075 ASSERT(ToRegister(instr->InputAt(0)).is(eax));
1076 ASSERT(ToRegister(instr->result()).is(edx));
1077 Register scratch = ToRegister(instr->TempAt(0));
1078
1079 // Find b which: 2^b < divisor_abs < 2^(b+1).
1080 unsigned b = 31 - CompilerIntrinsics::CountLeadingZeros(divisor_abs);
1081 unsigned shift = 32 + b; // Precision +1bit (effectively).
1082 double multiplier_f =
1083 static_cast<double>(static_cast<uint64_t>(1) << shift) / divisor_abs;
1084 int64_t multiplier;
1085 if (multiplier_f - floor(multiplier_f) < 0.5) {
1086 multiplier = floor(multiplier_f);
1087 } else {
1088 multiplier = floor(multiplier_f) + 1;
1089 }
1090 // The multiplier is a uint32.
1091 ASSERT(multiplier > 0 &&
1092 multiplier < (static_cast<int64_t>(1) << 32));
1093 __ mov(scratch, dividend);
1094 if (divisor < 0 &&
1095 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1096 __ test(dividend, dividend);
1097 DeoptimizeIf(zero, instr->environment());
1098 }
1099 __ mov(edx, multiplier);
1100 __ imul(edx);
1101 if (static_cast<int32_t>(multiplier) < 0) {
1102 __ add(edx, scratch);
1103 }
1104 Register reg_lo = eax;
1105 Register reg_byte_scratch = scratch;
1106 if (!reg_byte_scratch.is_byte_register()) {
1107 __ xchg(reg_lo, reg_byte_scratch);
1108 reg_lo = scratch;
1109 reg_byte_scratch = eax;
1110 }
1111 if (divisor < 0) {
1112 __ xor_(reg_byte_scratch, reg_byte_scratch);
1113 __ cmp(reg_lo, 0x40000000);
1114 __ setcc(above, reg_byte_scratch);
1115 __ neg(edx);
1116 __ sub(edx, reg_byte_scratch);
1117 } else {
1118 __ xor_(reg_byte_scratch, reg_byte_scratch);
1119 __ cmp(reg_lo, 0xC0000000);
1120 __ setcc(above_equal, reg_byte_scratch);
1121 __ add(edx, reg_byte_scratch);
1122 }
1123 __ sar(edx, shift - 32);
1124 }
1125 }
1126
1127
1025 void LCodeGen::DoMulI(LMulI* instr) { 1128 void LCodeGen::DoMulI(LMulI* instr) {
1026 Register left = ToRegister(instr->InputAt(0)); 1129 Register left = ToRegister(instr->InputAt(0));
1027 LOperand* right = instr->InputAt(1); 1130 LOperand* right = instr->InputAt(1);
1028 1131
1029 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { 1132 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1030 __ mov(ToRegister(instr->TempAt(0)), left); 1133 __ mov(ToRegister(instr->TempAt(0)), left);
1031 } 1134 }
1032 1135
1033 if (right->IsConstantOperand()) { 1136 if (right->IsConstantOperand()) {
1034 // Try strength reductions on the multiplication. 1137 // Try strength reductions on the multiplication.
(...skipping 4194 matching lines...) Expand 10 before | Expand all | Expand 10 after
5229 FixedArray::kHeaderSize - kPointerSize)); 5332 FixedArray::kHeaderSize - kPointerSize));
5230 __ bind(&done); 5333 __ bind(&done);
5231 } 5334 }
5232 5335
5233 5336
5234 #undef __ 5337 #undef __
5235 5338
5236 } } // namespace v8::internal 5339 } } // namespace v8::internal
5237 5340
5238 #endif // V8_TARGET_ARCH_IA32 5341 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/ia32/disasm-ia32.cc ('k') | src/ia32/lithium-ia32.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698