Commit 9dd35ee2 authored by erik.corry@gmail.com's avatar erik.corry@gmail.com

ARM improvements to constant div, mod and mul.

* Fast runtime calls for div and mod.
* Fix assembly and disassembly of multiply instructions.
* Strength reduce and inline multiplications to shift-add.
* Strength reduce and inline mod by power of 2.
* Strength reduce mod by other small integers to mul.
* Strength reduce div by 2 and 3.
Review URL: http://codereview.chromium.org/155047

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2355 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 1a3d633e
...@@ -837,6 +837,7 @@ void Assembler::mla(Register dst, Register src1, Register src2, Register srcA, ...@@ -837,6 +837,7 @@ void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
void Assembler::mul(Register dst, Register src1, Register src2, void Assembler::mul(Register dst, Register src1, Register src2,
SBit s, Condition cond) { SBit s, Condition cond) {
ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc)); ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
// dst goes in bits 16-19 for this instruction!
emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code()); emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
} }
...@@ -888,7 +889,7 @@ void Assembler::umull(Register dstL, ...@@ -888,7 +889,7 @@ void Assembler::umull(Register dstL,
Condition cond) { Condition cond) {
ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc)); ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
ASSERT(!dstL.is(dstH)); ASSERT(!dstL.is(dstH));
emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 | emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
src2.code()*B8 | B7 | B4 | src1.code()); src2.code()*B8 | B7 | B4 | src1.code());
} }
......
This diff is collapsed.
...@@ -186,6 +186,8 @@ class CodeGenerator: public AstVisitor { ...@@ -186,6 +186,8 @@ class CodeGenerator: public AstVisitor {
bool in_spilled_code() const { return in_spilled_code_; } bool in_spilled_code() const { return in_spilled_code_; }
void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; } void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
static const int kUnknownIntValue = -1;
private: private:
// Construction/Destruction // Construction/Destruction
CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval); CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval);
...@@ -291,7 +293,9 @@ class CodeGenerator: public AstVisitor { ...@@ -291,7 +293,9 @@ class CodeGenerator: public AstVisitor {
void ToBoolean(JumpTarget* true_target, JumpTarget* false_target); void ToBoolean(JumpTarget* true_target, JumpTarget* false_target);
void GenericBinaryOperation(Token::Value op, OverwriteMode overwrite_mode); void GenericBinaryOperation(Token::Value op,
OverwriteMode overwrite_mode,
int known_rhs = kUnknownIntValue);
void Comparison(Condition cc, void Comparison(Condition cc,
Expression* left, Expression* left,
Expression* right, Expression* right,
......
...@@ -438,7 +438,19 @@ int Decoder::FormatOption(Instr* instr, const char* format) { ...@@ -438,7 +438,19 @@ int Decoder::FormatOption(Instr* instr, const char* format) {
return 6; return 6;
} }
case 'u': { // 'u: signed or unsigned multiplies case 'u': { // 'u: signed or unsigned multiplies
if (instr->Bit(22) == 1) { // The manual gets the meaning of bit 22 backwards in the multiply
// instruction overview on page A3.16.2. The instructions that
// exist in u and s variants are the following:
// smull A4.1.87
// umull A4.1.129
// umlal A4.1.128
// smlal A4.1.76
// For these 0 means u and 1 means s. As can be seen on their individual
// pages. The other 18 mul instructions have the bit set or unset in
// arbitrary ways that are unrelated to the signedness of the instruction.
// None of these 18 instructions exist in both a 'u' and an 's' variant.
if (instr->Bit(22) == 0) {
Print("u"); Print("u");
} else { } else {
Print("s"); Print("s");
...@@ -494,11 +506,16 @@ void Decoder::DecodeType01(Instr* instr) { ...@@ -494,11 +506,16 @@ void Decoder::DecodeType01(Instr* instr) {
// multiply instructions // multiply instructions
if (instr->Bit(23) == 0) { if (instr->Bit(23) == 0) {
if (instr->Bit(21) == 0) { if (instr->Bit(21) == 0) {
Format(instr, "mul'cond's 'rd, 'rm, 'rs"); // Mul calls it Rd. Everyone else calls it Rn.
Format(instr, "mul'cond's 'rn, 'rm, 'rs");
} else { } else {
Format(instr, "mla'cond's 'rd, 'rm, 'rs, 'rn"); // In the manual the order is rd, rm, rs, rn. But mla swaps the
// positions of rn and rd in the encoding.
Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
} }
} else { } else {
// In the manual the order is RdHi, RdLo, Rm, Rs.
// RdHi is what other instructions call Rn and RdLo is Rd.
Format(instr, "'um'al'cond's 'rn, 'rd, 'rm, 'rs"); Format(instr, "'um'al'cond's 'rn, 'rd, 'rm, 'rs");
} }
} else { } else {
......
...@@ -1080,41 +1080,44 @@ void Simulator::DecodeType01(Instr* instr) { ...@@ -1080,41 +1080,44 @@ void Simulator::DecodeType01(Instr* instr) {
// multiply instruction or extra loads and stores // multiply instruction or extra loads and stores
if (instr->Bits(7, 4) == 9) { if (instr->Bits(7, 4) == 9) {
if (instr->Bit(24) == 0) { if (instr->Bit(24) == 0) {
// multiply instructions // Multiply instructions have Rd in a funny place.
int rd = instr->RdField(); int rd = instr->RnField();
int rm = instr->RmField(); int rm = instr->RmField();
int rs = instr->RsField(); int rs = instr->RsField();
int32_t rs_val = get_register(rs); int32_t rs_val = get_register(rs);
int32_t rm_val = get_register(rm); int32_t rm_val = get_register(rm);
if (instr->Bit(23) == 0) { if (instr->Bit(23) == 0) {
if (instr->Bit(21) == 0) { if (instr->Bit(21) == 0) {
// Format(instr, "mul'cond's 'rd, 'rm, 'rs"); // Format(instr, "mul'cond's 'rn, 'rm, 'rs");
int32_t alu_out = rm_val * rs_val; int32_t alu_out = rm_val * rs_val;
set_register(rd, alu_out); set_register(rd, alu_out);
if (instr->HasS()) { if (instr->HasS()) {
SetNZFlags(alu_out); SetNZFlags(alu_out);
} }
} else { } else {
Format(instr, "mla'cond's 'rd, 'rm, 'rs, 'rn"); UNIMPLEMENTED(); // mla is not used by V8.
} }
} else { } else {
// Format(instr, "'um'al'cond's 'rn, 'rd, 'rs, 'rm"); // Format(instr, "'um'al'cond's 'rn, 'rd, 'rs, 'rm");
int rn = instr->RnField(); int rd_lo = instr->RdField();
int32_t hi_res = 0; int32_t hi_res = 0;
int32_t lo_res = 0; int32_t lo_res = 0;
if (instr->Bit(22) == 0) { if (instr->Bit(22) == 1) {
// signed multiply int64_t left_op = static_cast<int32_t>(rm_val);
UNIMPLEMENTED(); int64_t right_op = static_cast<int32_t>(rs_val);
uint64_t result = left_op * right_op;
hi_res = static_cast<int32_t>(result >> 32);
lo_res = static_cast<int32_t>(result & 0xffffffff);
} else { } else {
// unsigned multiply // unsigned multiply
uint64_t left_op = rm_val; uint64_t left_op = static_cast<uint32_t>(rm_val);
uint64_t right_op = rs_val; uint64_t right_op = static_cast<uint32_t>(rs_val);
uint64_t result = left_op * right_op; uint64_t result = left_op * right_op;
hi_res = static_cast<int32_t>(result >> 32); hi_res = static_cast<int32_t>(result >> 32);
lo_res = static_cast<int32_t>(result & 0xffffffff); lo_res = static_cast<int32_t>(result & 0xffffffff);
} }
set_register(rn, hi_res); set_register(rd_lo, lo_res);
set_register(rd, lo_res); set_register(rd, hi_res);
if (instr->HasS()) { if (instr->HasS()) {
UNIMPLEMENTED(); UNIMPLEMENTED();
} }
......
...@@ -608,6 +608,16 @@ static double mul_two_doubles(double x, double y) { ...@@ -608,6 +608,16 @@ static double mul_two_doubles(double x, double y) {
} }
static double div_two_doubles(double x, double y) {
return x / y;
}
static double mod_two_doubles(double x, double y) {
return fmod(x, y);
}
static int native_compare_doubles(double x, double y) { static int native_compare_doubles(double x, double y) {
if (x == y) return 0; if (x == y) return 0;
return x < y ? 1 : -1; return x < y ? 1 : -1;
...@@ -628,6 +638,12 @@ ExternalReference ExternalReference::double_fp_operation( ...@@ -628,6 +638,12 @@ ExternalReference ExternalReference::double_fp_operation(
case Token::MUL: case Token::MUL:
function = &mul_two_doubles; function = &mul_two_doubles;
break; break;
case Token::DIV:
function = &div_two_doubles;
break;
case Token::MOD:
function = &mod_two_doubles;
break;
default: default:
UNREACHABLE(); UNREACHABLE();
} }
......
...@@ -712,9 +712,17 @@ void ExternalReferenceTable::PopulateTable() { ...@@ -712,9 +712,17 @@ void ExternalReferenceTable::PopulateTable() {
UNCLASSIFIED, UNCLASSIFIED,
13, 13,
"mul_two_doubles"); "mul_two_doubles");
Add(ExternalReference::compare_doubles().address(), Add(ExternalReference::double_fp_operation(Token::DIV).address(),
UNCLASSIFIED, UNCLASSIFIED,
14, 14,
"div_two_doubles");
Add(ExternalReference::double_fp_operation(Token::MOD).address(),
UNCLASSIFIED,
15,
"mod_two_doubles");
Add(ExternalReference::compare_doubles().address(),
UNCLASSIFIED,
16,
"compare_doubles"); "compare_doubles");
} }
......
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Test fast div and mod.
function divmod(div_func, mod_func, x, y) {
var div_answer = (div_func)(x);
assertEquals(x / y, div_answer, x + "/" + y);
var mod_answer = (mod_func)(x);
assertEquals(x % y, mod_answer, x + "%" + y);
var minus_div_answer = (div_func)(-x);
assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y);
var minus_mod_answer = (mod_func)(-x);
assertEquals(-x % y, minus_mod_answer, "-" + x + "%" + y);
}
function run_tests_for(divisor) {
print("(function(left) { return left / " + divisor + "; })");
var div_func = this.eval("(function(left) { return left / " + divisor + "; })");
var mod_func = this.eval("(function(left) { return left % " + divisor + "; })");
var exp;
// Strange number test.
divmod(div_func, mod_func, 0, divisor);
divmod(div_func, mod_func, 1 / 0, divisor);
// Floating point number test.
for (exp = -1024; exp <= 1024; exp += 4) {
divmod(div_func, mod_func, Math.pow(2, exp), divisor);
divmod(div_func, mod_func, 0.9999999 * Math.pow(2, exp), divisor);
divmod(div_func, mod_func, 1.0000001 * Math.pow(2, exp), divisor);
}
// Integer number test.
for (exp = 0; exp <= 32; exp++) {
divmod(div_func, mod_func, 1 << exp, divisor);
divmod(div_func, mod_func, (1 << exp) + 1, divisor);
divmod(div_func, mod_func, (1 << exp) - 1, divisor);
}
divmod(div_func, mod_func, Math.floor(0x1fffffff / 3), divisor);
divmod(div_func, mod_func, Math.floor(-0x20000000 / 3), divisor);
}
var divisors = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
// These ones in the middle don't add much apart from slowness to the test.
0x1000000,
0x2000000,
0x4000000,
0x8000000,
0x10000000,
0x20000000,
0x40000000,
12,
60,
100,
1000 * 60 * 60 * 24];
for (var i = 0; i < divisors.length; i++) {
run_tests_for(divisors[i]);
}
...@@ -37,64 +37,64 @@ var minus_four = -4; ...@@ -37,64 +37,64 @@ var minus_four = -4;
// variable op variable // variable op variable
assertEquals(one / (-zero), -Infinity); assertEquals(one / (-zero), -Infinity, "one / -0 I");
assertEquals(one / (zero * minus_one), -Infinity); assertEquals(one / (zero * minus_one), -Infinity, "one / -1");
assertEquals(one / (minus_one * zero), -Infinity); assertEquals(one / (minus_one * zero), -Infinity, "one / -0 II");
assertEquals(one / (zero * zero), Infinity); assertEquals(one / (zero * zero), Infinity, "one / 0 I");
assertEquals(one / (minus_one * minus_one), 1); assertEquals(one / (minus_one * minus_one), 1, "one / 1");
assertEquals(one / (zero / minus_one), -Infinity); assertEquals(one / (zero / minus_one), -Infinity, "one / -0 III");
assertEquals(one / (zero / one), Infinity); assertEquals(one / (zero / one), Infinity, "one / 0 II");
assertEquals(one / (minus_four % two), -Infinity); assertEquals(one / (minus_four % two), -Infinity, "foo");
assertEquals(one / (minus_four % minus_two), -Infinity); assertEquals(one / (minus_four % minus_two), -Infinity, "foo");
assertEquals(one / (four % two), Infinity); assertEquals(one / (four % two), Infinity, "foo");
assertEquals(one / (four % minus_two), Infinity); assertEquals(one / (four % minus_two), Infinity, "foo");
// literal op variable // literal op variable
assertEquals(one / (0 * minus_one), -Infinity); assertEquals(one / (0 * minus_one), -Infinity, "bar");
assertEquals(one / (-1 * zero), -Infinity); assertEquals(one / (-1 * zero), -Infinity, "bar");
assertEquals(one / (0 * zero), Infinity); assertEquals(one / (0 * zero), Infinity, "bar");
assertEquals(one / (-1 * minus_one), 1); assertEquals(one / (-1 * minus_one), 1, "bar");
assertEquals(one / (0 / minus_one), -Infinity); assertEquals(one / (0 / minus_one), -Infinity, "baz");
assertEquals(one / (0 / one), Infinity); assertEquals(one / (0 / one), Infinity, "baz");
assertEquals(one / (-4 % two), -Infinity); assertEquals(one / (-4 % two), -Infinity, "baz");
assertEquals(one / (-4 % minus_two), -Infinity); assertEquals(one / (-4 % minus_two), -Infinity, "baz");
assertEquals(one / (4 % two), Infinity); assertEquals(one / (4 % two), Infinity, "baz");
assertEquals(one / (4 % minus_two), Infinity); assertEquals(one / (4 % minus_two), Infinity, "baz");
// variable op literal // variable op literal
assertEquals(one / (zero * -1), -Infinity); assertEquals(one / (zero * -1), -Infinity, "fizz");
assertEquals(one / (minus_one * 0), -Infinity); assertEquals(one / (minus_one * 0), -Infinity, "fizz");
assertEquals(one / (zero * 0), Infinity); assertEquals(one / (zero * 0), Infinity, "fizz");
assertEquals(one / (minus_one * -1), 1); assertEquals(one / (minus_one * -1), 1, "fizz");
assertEquals(one / (zero / -1), -Infinity); assertEquals(one / (zero / -1), -Infinity, "buzz");
assertEquals(one / (zero / 1), Infinity); assertEquals(one / (zero / 1), Infinity, "buzz");
assertEquals(one / (minus_four % 2), -Infinity); assertEquals(one / (minus_four % 2), -Infinity, "buzz");
assertEquals(one / (minus_four % -2), -Infinity); assertEquals(one / (minus_four % -2), -Infinity, "buzz");
assertEquals(one / (four % 2), Infinity); assertEquals(one / (four % 2), Infinity, "buzz");
assertEquals(one / (four % -2), Infinity); assertEquals(one / (four % -2), Infinity, "buzz");
// literal op literal // literal op literal
assertEquals(one / (-0), -Infinity); assertEquals(one / (-0), -Infinity, "fisk1");
assertEquals(one / (0 * -1), -Infinity); assertEquals(one / (0 * -1), -Infinity, "fisk2");
assertEquals(one / (-1 * 0), -Infinity); assertEquals(one / (-1 * 0), -Infinity, "fisk3");
assertEquals(one / (0 * 0), Infinity); assertEquals(one / (0 * 0), Infinity, "fisk4");
assertEquals(one / (-1 * -1), 1); assertEquals(one / (-1 * -1), 1, "fisk5");
assertEquals(one / (0 / -1), -Infinity); assertEquals(one / (0 / -1), -Infinity, "hest");
assertEquals(one / (0 / 1), Infinity); assertEquals(one / (0 / 1), Infinity, "hest");
assertEquals(one / (-4 % 2), -Infinity); assertEquals(one / (-4 % 2), -Infinity, "fiskhest");
assertEquals(one / (-4 % -2), -Infinity); assertEquals(one / (-4 % -2), -Infinity, "fiskhest");
assertEquals(one / (4 % 2), Infinity); assertEquals(one / (4 % 2), Infinity, "fiskhest");
assertEquals(one / (4 % -2), Infinity); assertEquals(one / (4 % -2), Infinity, "fiskhest");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment