Skip to content

Commit

Permalink
Move Negate slow path out of line
Browse files Browse the repository at this point in the history
Summary:
In the interest of keeping the interpreter simple, and for consistency
with other operations, move the slow path of Negate into
`Interpreter-slowpaths.cpp`.

Reviewed By: tmikov

Differential Revision: D41384542

fbshipit-source-id: 4a5ea79d83908e9871e36ccb295895b0700073b6
  • Loading branch information
neildhar authored and facebook-github-bot committed Nov 18, 2022
1 parent 5a8745f commit 003e9db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/VM/Interpreter-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ CallResult<HermesValue> doIncDecOperSlowPath(Runtime &runtime, Handle<> src);

CallResult<HermesValue> doBitNotSlowPath(Runtime &runtime, Handle<> src);

CallResult<HermesValue> doNegateSlowPath(Runtime &runtime, Handle<> src);

} // namespace vm
} // namespace hermes
#endif // HERMES_VM_INTERPRETER_INTERNAL_H
15 changes: 15 additions & 0 deletions lib/VM/Interpreter-slowpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,21 @@ CallResult<HermesValue> doBitNotSlowPath(Runtime &runtime, Handle<> src) {
return BigIntPrimitive::unaryNOT(runtime, bigint);
}

CallResult<HermesValue> doNegateSlowPath(Runtime &runtime, Handle<> src) {
// Try converting src to a numeric.
auto numRes = toNumeric_RJS(runtime, src);
if (LLVM_UNLIKELY(numRes == ExecutionStatus::EXCEPTION))
return ExecutionStatus::EXCEPTION;
// Test for BigInt since it is cheaper than testing for number. If it is a
// number, negate it and return.
if (LLVM_LIKELY(!numRes->isBigInt()))
return HermesValue::encodeDoubleValue(-numRes->getNumber());

// The result is a BigInt, perform a BigInt bitwise not.
auto bigint = runtime.makeHandle(numRes->getBigInt());
return BigIntPrimitive::unaryMinus(runtime, bigint);
}

} // namespace vm
} // namespace hermes

Expand Down
24 changes: 7 additions & 17 deletions lib/VM/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3054,24 +3054,14 @@ CallResult<HermesValue> Interpreter::interpretFunction(
if (LLVM_LIKELY(O2REG(Negate).isNumber())) {
O1REG(Negate) =
HermesValue::encodeDoubleValue(-O2REG(Negate).getNumber());
} else {
CAPTURE_IP(res = toNumeric_RJS(runtime, Handle<>(&O2REG(Negate))));
if (res == ExecutionStatus::EXCEPTION)
goto exception;
if (res->isNumber()) {
O1REG(Negate) = HermesValue::encodeDoubleValue(-res->getNumber());
} else {
assert(res->isBigInt() && "should be bigint");
CAPTURE_IP_ASSIGN(
auto bigint, runtime.makeHandle(res->getBigInt()));
CAPTURE_IP(res = BigIntPrimitive::unaryMinus(runtime, bigint));
if (res == ExecutionStatus::EXCEPTION) {
goto exception;
}
O1REG(Negate) = HermesValue::encodeBigIntValue(res->getBigInt());
}
gcScope.flushToSmallCount(KEEP_HANDLES);
ip = NEXTINST(Negate);
DISPATCH;
}
CAPTURE_IP(res = doNegateSlowPath(runtime, Handle<>(&O2REG(Negate))));
if (res == ExecutionStatus::EXCEPTION)
goto exception;
O1REG(Negate) = *res;
gcScope.flushToSmallCount(KEEP_HANDLES);
ip = NEXTINST(Negate);
DISPATCH;
}
Expand Down

0 comments on commit 003e9db

Please sign in to comment.