test-hashing.cc 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Copyright 2011 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.

#include <stdlib.h>

30
#include "src/v8.h"
31

32 33 34 35 36
#include "src/code-stubs.h"
#include "src/factory.h"
#include "src/macro-assembler.h"
#include "src/objects.h"
#include "test/cctest/cctest.h"
37 38

#ifdef USE_SIMULATOR
39
#include "src/simulator.h"
40 41 42 43 44 45 46
#endif

using namespace v8::internal;


typedef uint32_t (*HASH_FUNCTION)();

47
#define __ masm->
48 49


50
void generate(MacroAssembler* masm, uint32_t key) {
danno@chromium.org's avatar
danno@chromium.org committed
51
#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
52 53 54 55 56 57
  __ push(ebx);
  __ mov(eax, Immediate(key));
  __ GetNumberHash(eax, ebx);
  __ pop(ebx);
  __ Ret();
#elif V8_TARGET_ARCH_X64
58
  __ pushq(kRootRegister);
59
  __ InitializeRootRegister();
60 61
  __ pushq(rbx);
  __ movp(rax, Immediate(key));
62
  __ GetNumberHash(rax, rbx);
63 64
  __ popq(rbx);
  __ popq(kRootRegister);
65 66 67 68 69 70 71 72
  __ Ret();
#elif V8_TARGET_ARCH_ARM
  __ push(kRootRegister);
  __ InitializeRootRegister();
  __ mov(r0, Operand(key));
  __ GetNumberHash(r0, ip);
  __ pop(kRootRegister);
  __ mov(pc, Operand(lr));
73 74 75
#elif V8_TARGET_ARCH_ARM64
  // The ARM64 assembler usually uses jssp (x28) as a stack pointer, but only
  // csp is initialized by the calling (C++) code.
76 77 78 79 80 81 82 83 84
  Register old_stack_pointer = __ StackPointer();
  __ SetStackPointer(csp);
  __ Push(root, xzr);
  __ InitializeRootRegister();
  __ Mov(x0, key);
  __ GetNumberHash(x0, x10);
  __ Pop(xzr, root);
  __ Ret();
  __ SetStackPointer(old_stack_pointer);
85
#elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
86 87 88 89 90 91 92
  __ push(kRootRegister);
  __ InitializeRootRegister();
  __ li(v0, Operand(key));
  __ GetNumberHash(v0, t1);
  __ pop(kRootRegister);
  __ jr(ra);
  __ nop();
93 94 95 96 97 98 99 100 101
#elif V8_TARGET_ARCH_S390
  __ push(kRootRegister);
  __ push(ip);
  __ InitializeRootRegister();
  __ lhi(r2, Operand(key));
  __ GetNumberHash(r2, ip);
  __ pop(ip);
  __ pop(kRootRegister);
  __ Ret();
102 103 104 105 106 107 108 109
#elif V8_TARGET_ARCH_PPC
  __ function_descriptor();
  __ push(kRootRegister);
  __ InitializeRootRegister();
  __ li(r3, Operand(key));
  __ GetNumberHash(r3, ip);
  __ pop(kRootRegister);
  __ blr();
110 111
#else
#error Unsupported architecture.
112 113 114 115 116
#endif
}


void check(uint32_t key) {
117
  Isolate* isolate = CcTest::i_isolate();
118 119 120
  Factory* factory = isolate->factory();
  HandleScope scope(isolate);

121
  v8::internal::byte buffer[2048];
122 123
  MacroAssembler masm(CcTest::i_isolate(), buffer, sizeof(buffer),
                      v8::internal::CodeObjectRequired::kYes);
124 125 126 127 128

  generate(&masm, key);

  CodeDesc desc;
  masm.GetCode(&desc);
129 130 131 132
  Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
  Handle<Code> code = factory->NewCode(desc,
                                       Code::ComputeFlags(Code::STUB),
                                       undefined);
133 134 135 136
  CHECK(code->IsCode());

  HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
#ifdef USE_SIMULATOR
137 138
  uint32_t codegen_hash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
      CALL_GENERATED_CODE(isolate, hash, 0, 0, 0, 0, 0)));
139 140 141 142
#else
  uint32_t codegen_hash = hash();
#endif

143
  uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed());
144
  CHECK_EQ(runtime_hash, codegen_hash);
145 146 147 148 149 150 151 152 153
}


static uint32_t PseudoRandom(uint32_t i, uint32_t j) {
  return ~(~((i * 781) ^ (j * 329)));
}


TEST(NumberHash) {
154
  v8::Isolate* isolate = CcTest::isolate();
155 156
  v8::HandleScope handle_scope(isolate);
  v8::Context::Scope context_scope(v8::Context::New(isolate));
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

  // Some specific numbers
  for (uint32_t key = 0; key < 42; key += 7) {
    check(key);
  }

  // Some pseudo-random numbers
  static const uint32_t kLimit = 1000;
  for (uint32_t i = 0; i < 5; i++) {
    for (uint32_t j = 0; j < 5; j++) {
      check(PseudoRandom(i, j) % kLimit);
    }
  }
}

172
#undef __