2018-07-25 22:46:13 +02:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef FORTRAN_EVALUATE_INT_POWER_H_
|
|
|
|
#define FORTRAN_EVALUATE_INT_POWER_H_
|
|
|
|
|
|
|
|
// Computes an integer power of a real or complex value.
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
|
|
|
template<typename REAL, typename INT>
|
2018-11-14 01:02:52 +01:00
|
|
|
ValueWithRealFlags<REAL> TimesIntPowerOf(const REAL &factor, const REAL &base,
|
|
|
|
const INT &power, Rounding rounding = defaultRounding) {
|
|
|
|
ValueWithRealFlags<REAL> result{factor};
|
2018-07-25 22:46:13 +02:00
|
|
|
if (base.IsNotANumber()) {
|
2018-08-04 01:02:05 +02:00
|
|
|
result.value = REAL::NotANumber();
|
2018-11-14 01:02:52 +01:00
|
|
|
result.flags.set(RealFlag::InvalidArgument);
|
2018-07-25 22:46:13 +02:00
|
|
|
} else if (power.IsZero()) {
|
|
|
|
if (base.IsZero() || base.IsInfinite()) {
|
|
|
|
result.flags.set(RealFlag::InvalidArgument);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool negativePower{power.IsNegative()};
|
|
|
|
INT absPower{power.ABS().value};
|
2018-11-14 01:02:52 +01:00
|
|
|
REAL squares{base};
|
2018-07-25 22:46:13 +02:00
|
|
|
int nbits{INT::bits - absPower.LEADZ()};
|
2018-11-14 01:02:52 +01:00
|
|
|
for (int j{0}; j < nbits; ++j) {
|
2018-07-25 22:46:13 +02:00
|
|
|
if (absPower.BTEST(j)) {
|
2018-11-14 01:02:52 +01:00
|
|
|
if (negativePower) {
|
|
|
|
result.value =
|
|
|
|
result.value.Divide(squares).AccumulateFlags(result.flags);
|
|
|
|
} else {
|
|
|
|
result.value =
|
|
|
|
result.value.Multiply(squares).AccumulateFlags(result.flags);
|
|
|
|
}
|
2018-07-25 22:46:13 +02:00
|
|
|
}
|
2018-11-14 01:02:52 +01:00
|
|
|
squares = squares.Multiply(squares).AccumulateFlags(result.flags);
|
2018-07-25 22:46:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-11-14 01:02:52 +01:00
|
|
|
|
|
|
|
template<typename REAL, typename INT>
|
|
|
|
ValueWithRealFlags<REAL> IntPower(
|
|
|
|
const REAL &base, const INT &power, Rounding rounding = defaultRounding) {
|
|
|
|
REAL one{REAL::FromInteger(INT{1}).value};
|
|
|
|
return TimesIntPowerOf(one, base, power, rounding);
|
|
|
|
}
|
2018-10-25 14:55:23 +02:00
|
|
|
}
|
2018-07-25 22:46:13 +02:00
|
|
|
#endif // FORTRAN_EVALUATE_INT_POWER_H_
|