1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
// Copyright (c) 2006-2009 Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/next/Algebraic_kernel_d/include/CGAL/Algebraic_kernel_d/Real_embeddable_extension.h $
// $Id: Real_embeddable_extension.h 67093 2012-01-13 11:22:39Z lrineau $
//
//
// Author(s) : Michael Hemmer <hemmer@mpi-inf.mpg.de>
//
// ============================================================================
// TODO: Some comments are original EXACUS comments and aren't adapted. So
// they may be wrong now.
#ifndef CGAL_ALGEBRAIC_KERNEL_D_REAL_EMBEDDABLE_EXTENSION_H
#define CGAL_ALGEBRAIC_KERNEL_D_REAL_EMBEDDABLE_EXTENSION_H
#include <CGAL/basic.h>
#include <CGAL/Algebraic_kernel_d/Float_traits.h>
#ifdef CGAL_USE_LEDA
#include <CGAL/leda_integer.h>
#include <CGAL/leda_bigfloat.h>
#include <CGAL/leda_bigfloat_interval.h>
#include <LEDA/numbers/digit.h>
#endif
#ifdef CGAL_USE_CORE
#include <CGAL/CORE_BigFloat.h>
#endif
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
#include <CGAL/Gmpq.h>
#endif
#ifdef CGAL_USE_MPFR
#include <CGAL/Gmpfr.h>
#endif
#ifdef CGAL_USE_MPFI
#include <CGAL/Gmpfi.h>
#endif
namespace CGAL {
namespace internal {
// TODO: Implement array in source code file
// extern const signed char floor_log2_4bit[16]; // see src/floor_log2_4bit.C
// Don't define default, results in more convinient compiler messages
template< class Type > class Real_embeddable_extension;
// {
// public:
// typedef Null_functor Ceil_log2_abs;
// typedef Null_functor Floor_log2_abs;
// typedef Null_functor Floor;
// typedef Null_functor Ceil;
// };
// Functor adapting functions
template< class NT >
long floor_log2_abs( const NT& x ) {
return typename Real_embeddable_extension< NT >::Floor_log2_abs()( x );
}
template< class NT >
long ceil_log2_abs( const NT& x ) {
return typename Real_embeddable_extension< NT >::Ceil_log2_abs()( x );
}
template< class NT >
typename Real_embeddable_extension<NT>::Floor::result_type
floor (const NT& x) {
return typename Real_embeddable_extension<NT>::Floor() (x);
}
template< class NT >
typename Real_embeddable_extension<NT>::Ceil::result_type
ceil (const NT& x) {
return typename Real_embeddable_extension<NT>::Ceil() (x);
}
// Specialization for long
template<>
class Real_embeddable_extension< long > {
public:
struct Ceil_log2_abs
: public std::unary_function< long, long > {
long operator()( long x ) {
if (x < 0) x = -x;
CGAL_precondition(x > 0);
if (x == 1) return 0;
return Floor_log2_abs()(x-1) + 1;
}
};
struct Floor_log2_abs
: public std::unary_function< long, long > {
private:
signed char floor_log2_4bit[16];
public:
Floor_log2_abs() {
floor_log2_4bit[ 0] = -42;
floor_log2_4bit[ 1] = 0;
floor_log2_4bit[ 2] = 1;
floor_log2_4bit[ 3] = 1;
floor_log2_4bit[ 4] = 2;
floor_log2_4bit[ 5] = 2;
floor_log2_4bit[ 6] = 2;
floor_log2_4bit[ 7] = 2;
floor_log2_4bit[ 8] = 3;
floor_log2_4bit[ 9] = 3;
floor_log2_4bit[10] = 3;
floor_log2_4bit[11] = 3;
floor_log2_4bit[12] = 3;
floor_log2_4bit[13] = 3;
floor_log2_4bit[14] = 3;
floor_log2_4bit[15] = 3;
}
long operator()( long x ) {
if (x < 0) x = -x;
CGAL_precondition(x > 0);
result_type l = 0;
while (x > 0xFFFF) { l += 16; x >>= 16; }
if (x > 0xFF) { l += 8; x >>= 8; }
if (x > 0xF) { l += 4; x >>= 4; }
CGAL_assertion(x > 0 && x < 16);
return l + int(floor_log2_4bit[x]);
}
};
struct Floor
: public std::unary_function< long, long > {
long operator() (long x) { return x;}
};
struct Ceil
: public std::unary_function< long, long > {
long operator() (long x) { return x;}
};
};
#ifdef CGAL_USE_LEDA
// Specialization for leda_integer
template<>
class Real_embeddable_extension< leda_integer > {
public:
typedef leda_integer Type;
struct Ceil_log2_abs
: public std::unary_function< leda_integer, long > {
long operator()( const leda_integer& x ) const {
CGAL_precondition(x != leda_integer(0));
::leda::digit_sz ldgzeros = ::leda::digLeadingZeros(x.highword());
result_type l =
x.used_words() * ::leda::DIGIT_LENGTH - 1 - ldgzeros;
// look if additional 1-bits force to round up
::leda::digit h = 1;
h <<= ::leda::DIGIT_LENGTH - 1 - ldgzeros;
int i = x.used_words() - 1;
CGAL_assertion(x.contents(i) >= h);
if (x.contents(i) > h) return l+1;
while (--i >= 0) {
if (x.contents(i) != 0) return l+1;
}
return l;
}
};
struct Floor_log2_abs
: public std::unary_function< leda_integer, long > {
long operator()( const leda_integer& x ) const {
CGAL_precondition(x != leda_integer(0));
::leda::digit_sz ldgzeros
= ::leda::digLeadingZeros(x.highword());
result_type l =
x.used_words() * ::leda::DIGIT_LENGTH - 1 - ldgzeros;
return l;
}
};
struct Floor
: public std::unary_function< leda_integer, leda_integer > {
leda_integer operator() (const leda_integer& x) const { return x;}
};
struct Ceil
: public std::unary_function< leda_integer, leda_integer > {
leda_integer operator() (const leda_integer& x) const { return x;}
};
};
template<>
class Real_embeddable_extension< leda_bigfloat > {
public:
typedef leda_bigfloat Type;
struct Floor_log2_abs
: public std::unary_function< leda_bigfloat, long > {
long operator()( const leda_bigfloat& x ) const {
CGAL_precondition(CGAL::sign(x) != CGAL::ZERO);
::leda::integer abs_sign = abs(x.get_significant());
return (x.get_exponent() + ::leda::log(abs_sign)).to_long();
}
};
struct Ceil_log2_abs
: public std::unary_function< leda_bigfloat, long > {
long operator()( const leda_bigfloat& x ) const {
CGAL_precondition(CGAL::sign(x) != CGAL::ZERO);
return ::leda::ilog2(x).to_long();
}
};
struct Floor
: public std::unary_function< leda_bigfloat, leda_integer > {
leda_integer operator() ( const leda_bigfloat& x ) const {
return leda::to_integer( x, leda::TO_N_INF );
}
};
struct Ceil
: public std::unary_function< leda_bigfloat, leda_integer > {
leda_integer operator() ( const leda_bigfloat& x ) const {
return leda::to_integer( x, leda::TO_P_INF );
}
};
};
template<>
class Real_embeddable_extension< leda_bigfloat_interval > {
public:
typedef leda_bigfloat_interval Type;
struct Floor_log2_abs
: public std::unary_function< leda_bigfloat_interval, long > {
result_type operator() (const argument_type& x) const {
CGAL_precondition(! ::boost::numeric::in_zero(x));
return internal::floor_log2_abs(::boost::numeric::abs(x).lower());
}
};
struct Ceil_log2_abs
: public std::unary_function< leda_bigfloat_interval, long > {
long operator()( const leda_bigfloat_interval& x ) const {
CGAL_precondition(!(::boost::numeric::in_zero(x) &&
::boost::numeric::singleton(x)));
return internal::ceil_log2_abs(::boost::numeric::abs(x).upper());
}
};
struct Floor
: public std::unary_function< leda_bigfloat_interval, leda_integer > {
leda_integer operator() ( const leda_bigfloat_interval& x )
const {
return internal::floor( x.lower() );
}
};
struct Ceil
: public std::unary_function< leda_bigfloat_interval, leda_integer > {
leda_integer operator() ( const leda_bigfloat_interval& x )
const {
return internal::ceil( x.upper() );
}
};
};
#endif
#ifdef CGAL_USE_CORE
// Specialization for CORE::BigInt
template<>
class Real_embeddable_extension< CORE::BigInt > {
public:
typedef CORE::BigInt Type;
struct Floor_log2_abs
: public std::unary_function< CORE::BigInt, long > {
long operator()( const CORE::BigInt& x ) const {
return CORE::floorLg(x);
}
};
struct Ceil_log2_abs
: public std::unary_function< CORE::BigInt, long > {
long operator()( const CORE::BigInt& x ) const {
return CORE::ceilLg(x);
}
};
struct Floor
: public std::unary_function< CORE::BigInt, CORE::BigInt > {
CORE::BigInt operator() (const CORE::BigInt& x) const {
return x;
}
};
struct Ceil
: public std::unary_function< CORE::BigInt, CORE::BigInt > {
CORE::BigInt operator() (const CORE::BigInt& x) const {
return x;
}
};
};
// Specialization for CORE::BigFloat
template<>
class Real_embeddable_extension< CORE::BigFloat > {
public:
typedef CORE::BigFloat Type;
struct Floor_log2_abs
: public std::unary_function< CORE::BigFloat, long > {
long operator()( CORE::BigFloat x ) const {
CGAL_precondition(!CGAL::zero_in(x));
x = CGAL::abs(x);
return CORE::floorLg(x.m()-x.err())+x.exp()*CORE::CHUNK_BIT;
}
};
struct Ceil_log2_abs
: public std::unary_function< CORE::BigFloat, long > {
long operator()( CORE::BigFloat x ) const {
// (already commented out in EXACUS)...
// NiX_precond(!(NiX::in_zero(x) && NiX::singleton(x)));
x = CGAL::abs(x);
return CORE::ceilLg(x.m()+x.err())+x.exp()*CORE::CHUNK_BIT;
}
};
struct Floor
: public std::unary_function< CORE::BigFloat, CORE::BigInt > {
CORE::BigInt operator() ( const CORE::BigFloat& x ) const {
CORE::BigInt xi = x.BigIntValue();
if(x.sign() < 0 && x.cmp(xi)!=0) {
xi--;
}
return xi;
}
};
struct Ceil
: public std::unary_function< CORE::BigFloat, CORE::BigInt > {
CORE::BigInt operator() ( const CORE::BigFloat& x ) const {
CORE::BigInt xi = x.BigIntValue();
if(x.sign() >0 && x.cmp(xi)!=0) {
xi++;
}
return xi;
}
};
};
#endif // CORE
#if CGAL_USE_GMP
// Specialization for Gmpz
template<>
class Real_embeddable_extension< Gmpz > {
public:
typedef Gmpz Type;
struct Floor_log2_abs
: public std::unary_function< Gmpz, long > {
long operator()( const Gmpz& x ) const {
CGAL_precondition(!CGAL::is_zero(x));
return mpz_sizeinbase(x.mpz(),2)-1;
}
};
struct Ceil_log2_abs
: public std::unary_function< Gmpz, long > {
long operator()( const Gmpz& x ) const {
long pos = mpz_scan1(x.mpz(),0);
long size = mpz_sizeinbase(x.mpz(),2);
if (pos == size-1)
return size-1;
else
return size;
}
};
struct Floor
: public std::unary_function< Gmpz, Gmpz > {
Gmpz operator() (const Gmpz& x) const {
return x;
}
};
struct Ceil
: public std::unary_function< Gmpz, Gmpz > {
Gmpz operator() (const Gmpz& x) const {
return x;
}
};
};
#endif
#ifdef CGAL_USE_MPFR
template<>
class Real_embeddable_extension< Gmpfr > {
public:
typedef Gmpfr Type;
struct Floor_log2_abs
: public std::unary_function< Gmpfr, long > {
long operator()( const Gmpfr& x ) const {
Float_traits<Gmpfr>::Get_mantissa get_mantissa;
Float_traits<Gmpfr>::Get_exponent get_exponent;
CGAL_precondition(!CGAL::is_zero(x));
Real_embeddable_extension<Gmpz>::Floor_log2_abs floor_log2_abs;
return floor_log2_abs(get_mantissa(x))+get_exponent(x);
}
};
struct Ceil_log2_abs
: public std::unary_function< Gmpfr, long > {
long operator()( const Gmpfr& x ) const {
Float_traits<Gmpfr>::Get_mantissa get_mantissa;
Float_traits<Gmpfr>::Get_exponent get_exponent;
CGAL_precondition(!CGAL::is_zero(x));
Real_embeddable_extension<Gmpz>::Ceil_log2_abs ceil_log2_abs;
return ceil_log2_abs(get_mantissa(x))+get_exponent(x);
}
};
struct Floor
: public std::unary_function< Gmpfr, Gmpz > {
Gmpz operator() ( const Gmpfr& x ) const {
Gmpz result;
mpfr_get_z (result.mpz(),x.fr(),GMP_RNDD);
return result;
}
};
struct Ceil
: public std::unary_function< Gmpfr, Gmpz > {
Gmpz operator() ( const Gmpfr& x ) const {
Gmpz result;
mpfr_get_z (result.mpz(),x.fr(),GMP_RNDU);
return result;
}
};
};
#endif
#ifdef CGAL_USE_MPFI
template<>
class Real_embeddable_extension< Gmpfi > {
public:
typedef Gmpfi Type;
struct Floor_log2_abs
: public std::unary_function< Gmpfi, long > {
result_type operator() (const argument_type& x) const {
CGAL_precondition(!x.is_zero());
return internal::floor_log2_abs(x.abs().inf());
}
};
struct Ceil_log2_abs
: public std::unary_function< Gmpfi, long > {
long operator()( const Gmpfi& x ) const {
CGAL_precondition(!x.inf().is_zero() || !x.sup().is_zero());
return internal::ceil_log2_abs(x.abs().sup());
}
};
struct Floor
: public std::unary_function< Gmpfi, Gmpz > {
Gmpz operator() ( const Gmpfi& x )
const {
return internal::floor( x.inf() );
}
};
struct Ceil
: public std::unary_function< Gmpfi, Gmpz > {
Gmpz operator() ( const Gmpfi& x )
const {
return internal::ceil( x.sup() );
}
};
};
#endif
} //namespace internal
} //namespace CGAL
#endif // CGAL_ALGEBRAIC_KERNEL_D_REAL_EMBEDDABLE_EXTENSION_H
|