BWAPI
|
00001 // Copyright (c) 2008 Max-Planck-Institute Saarbruecken (Germany). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public License as 00006 // published by the Free Software Foundation; version 2.1 of the License. 00007 // See the file LICENSE.LGPL distributed with CGAL. 00008 // 00009 // Licensees holding a valid commercial license may use this file in 00010 // accordance with the commercial license agreement provided with the software. 00011 // 00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00014 // 00015 // $URL: svn+ssh://afabri@scm.gforge.inria.fr/svn/cgal/trunk/Polynomial/include/CGAL/Polynomial.h $ 00016 // $Id: Polynomial.h 47254 2008-12-06 21:18:27Z afabri $ 00017 // 00018 // 00019 // Author(s) : Michael Hemmer 00020 // 00021 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00022 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00023 // 00024 // $URL:$ 00025 00026 #ifndef CGAL_IPOWER_H 00027 #define CGAL_IPOWER_H 00028 00029 CGAL_BEGIN_NAMESPACE 00030 00031 template <typename NT> 00032 inline 00033 NT ipower(const NT& base, int expn) { 00034 // compute base^expn using square-and-multiply 00035 CGAL_precondition(expn >= 0); 00036 00037 // handle trivial cases efficiently 00038 if (expn == 0) return NT(1); 00039 if (expn == 1) return base; 00040 00041 // find the most significant non-zero bit of expn 00042 int e = expn, msb = 0; 00043 while (e >>= 1) msb++; 00044 00045 // computing base^expn by square-and-multiply 00046 NT res = base; 00047 int b = 1<<msb; 00048 while (b >>= 1) { // is there another bit right of what we saw so far? 00049 res *= res; 00050 if (expn & b) res *= base; 00051 } 00052 return res; 00053 } 00054 00055 template <typename NT> 00056 inline 00057 NT ipower(const NT& base, long expn) { 00058 // compute base^expn using square-and-multiply 00059 CGAL_precondition(expn >= 0); 00060 00061 // handle trivial cases efficiently 00062 if (expn == 0) return NT(1); 00063 if (expn == 1) return base; 00064 00065 // find the most significant non-zero bit of expn 00066 int e = expn, msb = 0; 00067 while (e >>= 1) msb++; 00068 00069 // computing base^expn by square-and-multiply 00070 NT res = base; 00071 int b = 1<<msb; 00072 while (b >>= 1) { // is there another bit right of what we saw so far? 00073 res *= res; 00074 if (expn & b) res *= base; 00075 } 00076 return res; 00077 } 00078 00079 CGAL_END_NAMESPACE 00080 00081 #endif // CGAL_IPOWER_H