2014-08-15 13:13:21 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
2015-05-20 04:59:52 +08:00
|
|
|
// Copyright (C) 2014-2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
|
2014-08-15 13:13:21 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <Eigen/CXX11/Tensor>
|
|
|
|
|
|
|
|
|
2015-03-31 14:26:45 +08:00
|
|
|
void test_signed_32bit()
|
2014-08-15 13:13:21 +08:00
|
|
|
{
|
2015-11-19 03:37:58 +08:00
|
|
|
// Divide by one
|
2015-11-20 07:52:51 +08:00
|
|
|
const Eigen::internal::TensorIntDivisor<int32_t, false> div_by_one(1);
|
2015-11-19 03:37:58 +08:00
|
|
|
|
|
|
|
for (int32_t j = 0; j < 25000; ++j) {
|
2015-11-20 07:52:51 +08:00
|
|
|
const int32_t fast_div = j / div_by_one;
|
2015-11-19 03:37:58 +08:00
|
|
|
const int32_t slow_div = j / 1;
|
|
|
|
VERIFY_IS_EQUAL(fast_div, slow_div);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard divide by 2 or more
|
|
|
|
for (int32_t i = 2; i < 25000; ++i) {
|
|
|
|
const Eigen::internal::TensorIntDivisor<int32_t, false> div(i);
|
|
|
|
|
|
|
|
for (int32_t j = 0; j < 25000; ++j) {
|
|
|
|
const int32_t fast_div = j / div;
|
|
|
|
const int32_t slow_div = j / i;
|
|
|
|
VERIFY_IS_EQUAL(fast_div, slow_div);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimized divide by 2 or more
|
2015-05-20 04:59:52 +08:00
|
|
|
for (int32_t i = 2; i < 25000; ++i) {
|
2015-11-19 03:37:58 +08:00
|
|
|
const Eigen::internal::TensorIntDivisor<int32_t, true> div(i);
|
2014-08-15 13:13:21 +08:00
|
|
|
|
|
|
|
for (int32_t j = 0; j < 25000; ++j) {
|
|
|
|
const int32_t fast_div = j / div;
|
|
|
|
const int32_t slow_div = j / i;
|
|
|
|
VERIFY_IS_EQUAL(fast_div, slow_div);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-31 14:26:45 +08:00
|
|
|
void test_unsigned_32bit()
|
2014-08-15 13:13:21 +08:00
|
|
|
{
|
|
|
|
for (uint32_t i = 1; i < 25000; ++i) {
|
|
|
|
const Eigen::internal::TensorIntDivisor<uint32_t> div(i);
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < 25000; ++j) {
|
|
|
|
const uint32_t fast_div = j / div;
|
|
|
|
const uint32_t slow_div = j / i;
|
|
|
|
VERIFY_IS_EQUAL(fast_div, slow_div);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-31 14:26:45 +08:00
|
|
|
void test_signed_64bit()
|
2014-08-15 13:13:21 +08:00
|
|
|
{
|
2015-11-19 03:37:58 +08:00
|
|
|
for (int64_t i = 1; i < 25000; ++i) {
|
2014-08-15 13:13:21 +08:00
|
|
|
const Eigen::internal::TensorIntDivisor<int64_t> div(i);
|
|
|
|
|
|
|
|
for (int64_t j = 0; j < 25000; ++j) {
|
|
|
|
const int64_t fast_div = j / div;
|
|
|
|
const int64_t slow_div = j / i;
|
|
|
|
VERIFY_IS_EQUAL(fast_div, slow_div);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-31 14:26:45 +08:00
|
|
|
void test_unsigned_64bit()
|
2014-08-15 13:13:21 +08:00
|
|
|
{
|
2015-11-19 03:37:58 +08:00
|
|
|
for (uint64_t i = 1; i < 25000; ++i) {
|
2014-08-15 13:13:21 +08:00
|
|
|
const Eigen::internal::TensorIntDivisor<uint64_t> div(i);
|
|
|
|
|
|
|
|
for (uint64_t j = 0; j < 25000; ++j) {
|
|
|
|
const uint64_t fast_div = j / div;
|
|
|
|
const uint64_t slow_div = j / i;
|
|
|
|
VERIFY_IS_EQUAL(fast_div, slow_div);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 08:02:30 +08:00
|
|
|
void test_powers_32bit() {
|
|
|
|
for (int expon = 1; expon < 31; expon++) {
|
2015-07-30 02:48:38 +08:00
|
|
|
int32_t div = (1 << expon);
|
2015-07-23 08:02:30 +08:00
|
|
|
for (int num_expon = 0; num_expon < 32; num_expon++) {
|
2015-07-30 02:48:38 +08:00
|
|
|
int32_t start_num = (1 << num_expon) - 100;
|
|
|
|
int32_t end_num = (1 << num_expon) + 100;
|
2015-07-23 08:02:30 +08:00
|
|
|
if (start_num < 0)
|
|
|
|
start_num = 0;
|
2015-07-30 02:48:38 +08:00
|
|
|
for (int32_t num = start_num; num < end_num; num++) {
|
2015-07-23 08:02:30 +08:00
|
|
|
Eigen::internal::TensorIntDivisor<int32_t> divider =
|
|
|
|
Eigen::internal::TensorIntDivisor<int32_t>(div);
|
|
|
|
int32_t result = num/div;
|
|
|
|
int32_t result_op = divider.divide(num);
|
|
|
|
VERIFY_IS_EQUAL(result_op, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 13:13:21 +08:00
|
|
|
|
2015-07-23 08:02:30 +08:00
|
|
|
void test_powers_64bit() {
|
|
|
|
for (int expon = 0; expon < 63; expon++) {
|
|
|
|
int64_t div = (1ull << expon);
|
|
|
|
for (int num_expon = 0; num_expon < 63; num_expon++) {
|
|
|
|
int64_t start_num = (1ull << num_expon) - 10;
|
|
|
|
int64_t end_num = (1ull << num_expon) + 10;
|
|
|
|
if (start_num < 0)
|
|
|
|
start_num = 0;
|
|
|
|
for (int64_t num = start_num; num < end_num; num++) {
|
2015-11-20 06:07:50 +08:00
|
|
|
Eigen::internal::TensorIntDivisor<int64_t> divider(div);
|
2015-07-23 08:02:30 +08:00
|
|
|
int64_t result = num/div;
|
|
|
|
int64_t result_op = divider.divide(num);
|
|
|
|
VERIFY_IS_EQUAL(result_op, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_specific() {
|
|
|
|
// A particular combination that was previously failing
|
2015-07-14 02:16:37 +08:00
|
|
|
int64_t div = 209715200;
|
2016-05-10 10:24:41 +08:00
|
|
|
int64_t num = 3238002688ll;
|
2015-11-20 06:07:50 +08:00
|
|
|
Eigen::internal::TensorIntDivisor<int64_t> divider(div);
|
2015-07-14 02:16:37 +08:00
|
|
|
int64_t result = num/div;
|
|
|
|
int64_t result_op = divider.divide(num);
|
|
|
|
VERIFY_IS_EQUAL(result, result_op);
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(cxx11_tensor_intdiv)
|
2014-08-15 13:13:21 +08:00
|
|
|
{
|
2015-03-31 14:26:45 +08:00
|
|
|
CALL_SUBTEST_1(test_signed_32bit());
|
|
|
|
CALL_SUBTEST_2(test_unsigned_32bit());
|
|
|
|
CALL_SUBTEST_3(test_signed_64bit());
|
|
|
|
CALL_SUBTEST_4(test_unsigned_64bit());
|
2015-07-23 08:02:30 +08:00
|
|
|
CALL_SUBTEST_5(test_powers_32bit());
|
|
|
|
CALL_SUBTEST_6(test_powers_64bit());
|
|
|
|
CALL_SUBTEST_7(test_specific());
|
2014-08-15 13:13:21 +08:00
|
|
|
}
|