| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . E X P O N U -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
function System.Exponu (Left : Int; Right : Natural) return Int |
| 33 |
with SPARK_Mode |
| 34 |
is |
| 35 |
-- Preconditions, postconditions, ghost code, loop invariants and |
| 36 |
-- assertions in this unit are meant for analysis only, not for run-time |
| 37 |
-- checking, as it would be too costly otherwise. This is enforced by |
| 38 |
-- setting the assertion policy to Ignore. |
| 39 |
|
| 40 |
pragma Assertion_Policy (Pre => Ignore, |
| 41 |
Post => Ignore, |
| 42 |
Ghost => Ignore, |
| 43 |
Loop_Invariant => Ignore, |
| 44 |
Assert => Ignore); |
| 45 |
|
| 46 |
-- Note that negative exponents get a constraint error because the |
| 47 |
-- subtype of the Right argument (the exponent) is Natural. |
| 48 |
|
| 49 |
Result : Int := 1; |
| 50 |
Factor : Int := Left; |
| 51 |
Exp : Natural := Right; |
| 52 |
|
| 53 |
begin |
| 54 |
-- We use the standard logarithmic approach, Exp gets shifted right |
| 55 |
-- testing successive low order bits and Factor is the value of the |
| 56 |
-- base raised to the next power of 2. |
| 57 |
|
| 58 |
-- Note: it is not worth special casing base values -1, 0, +1 since |
| 59 |
-- the expander does this when the base is a literal, and other cases |
| 60 |
-- will be extremely rare. |
| 61 |
|
| 62 |
if Exp /= 0 then |
| 63 |
loop |
| 64 |
pragma Loop_Invariant (Exp > 0); |
| 65 |
pragma Loop_Invariant (Result * Factor ** Exp = Left ** Right); |
| 66 |
pragma Loop_Variant (Decreases => Exp); |
| 67 |
|
| 68 |
if Exp rem 2 /= 0 then |
| 69 |
pragma Assert |
| 70 |
(Result * (Factor * Factor ** (Exp - 1)) = Left ** Right); |
| 71 |
pragma Assert |
| 72 |
((Result * Factor) * Factor ** (Exp - 1) = Left ** Right); |
| 73 |
|
| 74 |
Result := Result * Factor; |
| 75 |
end if; |
| 76 |
|
| 77 |
Exp := Exp / 2; |
| 78 |
exit when Exp = 0; |
| 79 |
Factor := Factor * Factor; |
| 80 |
end loop; |
| 81 |
end if; |
| 82 |
|
| 83 |
return Result; |
| 84 |
end System.Exponu; |