| 661 | struct PowerChecked { |
| 662 | template <typename T, typename Arg0, typename Arg1> |
| 663 | static enable_if_integer_value<T> Call(KernelContext*, Arg0 base, Arg1 exp, |
| 664 | Status* st) { |
| 665 | if (exp < 0) { |
| 666 | *st = Status::Invalid("integers to negative integer powers are not allowed"); |
| 667 | return 0; |
| 668 | } else if (exp == 0) { |
| 669 | return 1; |
| 670 | } |
| 671 | // left to right O(logn) power with overflow checks |
| 672 | bool overflow = false; |
| 673 | uint64_t bitmask = 1ULL << (63 - std::countl_zero(static_cast<uint64_t>(exp))); |
| 674 | T pow = 1; |
| 675 | while (bitmask) { |
| 676 | overflow |= MultiplyWithOverflow(pow, pow, &pow); |
| 677 | if (exp & bitmask) { |
| 678 | overflow |= MultiplyWithOverflow(pow, base, &pow); |
| 679 | } |
| 680 | bitmask >>= 1; |
| 681 | } |
| 682 | if (overflow) { |
| 683 | *st = Status::Invalid("overflow"); |
| 684 | } |
| 685 | return pow; |
| 686 | } |
| 687 | |
| 688 | template <typename T, typename Arg0, typename Arg1> |
| 689 | static enable_if_floating_value<T> Call(KernelContext*, Arg0 base, Arg1 exp, Status*) { |
nothing calls this directly
no test coverage detected