00001
00032 #ifndef TBCI_BASICS_H
00033 #define TBCI_BASICS_H
00034
00035 #include "tbci_version.h"
00036
00037 #ifdef HAVE_TBCICONFIG_H
00038 # include "tbciconfig.h"
00039 #else
00040 # include "config_manual.h"
00041 #endif
00042
00043
00044 #if !defined(__GNUC_PATCHLEVEL__) && defined(__GNUC__SUBVER__)
00045 # define __GNUC_PATCHLEVEL__ __GNUC_SUBVER__
00046 #endif
00047
00048
00049
00050
00051
00052 #undef HAVE_WIN_32
00053 #undef HAVE_UNIX
00054 #ifdef unix
00055 # define HAVE_UNIX 1
00056 #endif
00057 #ifdef _MSC_VER
00058 # define HAVE_WIN_32 1
00059
00060 # pragma warning(disable: 4003)
00061 #endif
00062
00063
00064 #if defined(USE_SMP) && !defined(SMP)
00065 # define SMP
00066 #endif
00067
00068
00069 #if !defined(NO_NS) && !defined(HAVE_BUGGY_NAMESPACE)
00070 # define USE_NS
00071 # undef NO_NS
00072 #else
00073 # undef USE_NS
00074 #endif
00075
00076
00077 #ifdef HAVE_SIGNATURE
00078 # define SIGNATURE signature
00079 #else
00080 # define SIGNATURE class
00081 #endif
00082
00083
00084 #if defined(HAVE_RESTRICT) && !defined(NO_RESTRICT)
00085 # define RESTRICT restrict
00086 #elif defined (HAVE___RESTRICT__) && !defined(NO_RESTRICT)
00087 # define RESTRICT __restrict__
00088 #else
00089 # define RESTRICT
00090 #endif
00091
00096 #if defined(HAVE_BUILTIN_EXPECT) && !defined(NO_EXPECT)
00097 # define LIKELY(expr) __builtin_expect((expr) != 0, 1)
00098 # define UNLIKELY(expr) __builtin_expect((expr) != 0, 0)
00099 #else
00100 # define LIKELY(expr) (expr)
00101 # define UNLIKELY(expr) (expr)
00102 #endif
00103
00104
00105 #ifdef _MSC_VER
00106 # ifdef BUILDING_DLL
00107 # define TBCI_DLLEXPORT __declspec(dllexport)
00108 # else
00109 # define TBCI_DLLEXPORT __declspec(dllimport)
00110 # endif
00111 # define TBCI_DLLLOCAL
00112 #else
00113 # ifdef HAVE_VISIBILITY_ATTR
00114 # define TBCI_DLLEXPORT __attribute__ ((visibility("default")))
00115 # define TBCI_DLLLOCAL __attribute__ ((visibility("hidden")))
00116 # else
00117 # define TBCI_DLLEXPORT
00118 # define TBCI_DLLLOCAL
00119 # endif
00120 #endif
00121
00122
00123 #if defined(HAVE_PRAGMA_IFACE_IMPL) && !defined(NO_PRAGMA_I)
00124 # define PRAGMA_I
00125 #endif
00126
00127
00128 #ifdef HAVE_NEED_FOR_EXPL_TMPLPARM
00129 # define FGD <>
00130 # define FGDT <T>
00131 # define FGDU <U>
00132 # define FGDTD <T,dims>
00133 # define FGDDT <dims,T>
00134 # define FGDCT <cplx<T> >
00135 # define FGDR <rank>
00136 #else
00137 # define FGD
00138 # define FGDT
00139 # define FGDU
00140 # define FGDTD
00141 # define FGDDT
00142 # define FGDCT
00143 # define FGDR
00144 #endif
00145
00146
00147
00148
00149 #if defined(__BORLANDC__) && defined(__linux__)
00150 # define __BEGIN_NAMESPACE_STD
00151 # define __END_NAMESPACE_STD
00152 # define __USING_NAMESPACE_STD(name)
00153 # define __BEGIN_NAMESPACE_C99
00154 # define __END_NAMESPACE_C99
00155 # define __USING_NAMESPACE_C99(name)
00156 #endif
00157
00158
00159 # include <iostream>
00160 # include <fstream>
00161 # include <iomanip>
00162 # include <string>
00163 #ifndef HAVE_NO_NEW_HEADERS_BUG
00164 # include <cstring>
00165 # include <cstdarg>
00166 # include <cstdlib>
00167 # include <cmath>
00168
00169 #else
00170
00171
00172
00173
00174 # include <string.h>
00175 # include <stdarg.h>
00176 # include <stdlib.h>
00177 # include <math.h>
00178
00179 #endif
00180
00181 #ifdef HAVE_UNISTD_H
00182 # include <unistd.h>
00183 #endif
00184
00185 #if defined(C_MEMALLOC) && defined(HAVE_MEMALIGN)
00186 # include <malloc.h>
00187 #endif
00188
00189
00190 #ifdef HAVE_MINMAX_IN_SYS_PARAM_H
00191 # include <sys/param.h>
00192 #endif
00193
00195 #if !defined(HAVE_SSTREAM) || defined(HAVE_BUGGY_SSTREAM)
00196
00197 # define ISTRINGSTREAM istrstream
00198 # define OSTRINGSTREAM ostrstream
00199 # define STRINGSTREAM strstream
00200 #else
00201
00202 # define ISTRINGSTREAM istringstream
00203 # define OSTRINGSTREAM ostringstream
00204 # define STRINGSTREAM stringstream
00205 #endif
00206
00207
00208
00209 #ifdef HAVE_LONG_DOUBLE
00210 # define LONG_DOUBLE long double
00211 #else
00212 # define LONG_DOUBLE double
00213 #endif
00214 #ifdef HAVE_LONG_LONG
00215 # define LONG_LONG long long
00216 #else
00217 # define LONG_LONG long
00218 #endif
00219
00220
00221 #ifdef AUTO_DECL
00222 # define INST(x) _instantiate x
00223 # define INST2(x,y) _instantiate x,y
00224 # define INST3(x,y,z) _instantiate x,y,z
00225 # define INST4(x,y,z,a) _instantiate x,y,z,a
00226 # define INST5(x,y,z,a,b) _instantiate x,y,z,a,b
00227 # define INST6(x,y,z,a,b,c) _instantiate x,y,z,a,b,c
00228 # define NOINST _noinstantiate
00229 # define INSTCTL(x) _instfile x
00230 #else
00231 # define INST(x)
00232 # define INST2(x,y)
00233 # define INST3(x,y,z)
00234 # define INST4(x,y,z,a)
00235 # define INST5(x,y,z,a,b)
00236 # define INST6(x,y,z,a,b,c)
00237 # define NOINST
00238 # define INSTCTL(x)
00239 #endif
00240
00241
00242 #ifndef NULL
00243 # define NULL (0)
00244 #endif
00245
00246
00247
00248
00249
00250 #if defined(ERRCHECK) && ! defined(BOUNDCHECK)
00251 # define BOUNDCHECK
00252 #endif
00253
00254 #ifdef BOUNDCHECK
00255 # ifndef ERRCHECK
00256 # define ERRCHECK
00257 # endif
00258 #else
00259 # undef ERRCHECK
00260
00261 #endif
00262
00263
00264
00265
00266
00271 #if defined(NO_NS)
00272 # define NAMESPACE_TBCI
00273 # define NAMESPACE_STD
00274 # define NAMESPACE_CSTD
00275 # define NAMESPACE_GRID
00276 # define NAMESPACE_CPLX
00277 # define NAMESPACE_END
00278 # define NAMESPACE_STD_END
00279 # define NAMESPACE_CSTD_END
00280 # define NAMESPACE_CPLX_END
00281 # define USING_TBCI
00282 # define USING_GRID
00283 # define USING_FD
00284 # define USING_END
00285 # define USIND_STD
00286 # define FRIEND_TBCI__
00287 # define FRIEND_TBCI2__
00288 # define TBCI__
00289 # define GRID__
00290 # define FD__
00291 # define STD__
00292 # define CSTD__
00293 # define MATH__
00294 # define CPLX__
00295 # define GLBL__ ::
00296 # define GLBL2__ ::
00297 #elif defined(NO_NS_TBCI)
00298 # define NAMESPACE_TBCI
00299 # define NAMESPACE_STD namespace std {
00300 # define NAMESPACE_CSTD namespace std {
00301 # define NAMESPACE_CPLX namespace std {
00302 # define NAMESPACE_GRID
00303 # define NAMESPACE_FD
00304 # define NAMESPACE_END ;
00305 # define NAMESPACE_STD_END }
00306 # define NAMESPACE_CSTD_END }
00307 # define NAMESPACE_CPLX_END }
00308 # define USING_TBCI
00309 # define USING_GRID
00310 # define USING_FD
00311 # define USING_END using namespace std;
00312 # define USING_STD using namespace std;
00313 # define TBCI__
00314 # define FRIEND_TBCI__
00315 # define FRIEND_TCBI2__
00316 # define GRID__
00317 # define FD__
00318 # define STD__ std::
00319 # define MATH__ std::
00320 # define CSTD__ std::
00321 # define CPLX__ std::
00322 # define GLBL__
00323 # define GLBL2__ ::
00324 #else
00325 # define NAMESPACE_TBCI namespace TBCI {
00326 # define NAMESPACE_STD namespace std {
00327 # define NAMESPACE_CSTD namespace std {
00328 # define NAMESPACE_GRID namespace Grid {
00329 # define NAMESPACE_FD namespace Finite_Difference {
00330 # define NAMESPACE_CPLX namespace std {
00331 # define NAMESPACE_END }
00332 # define NAMESPACE_STD_END }
00333 # define NAMESPACE_CSTD_END }
00334 # define NAMESPACE_CPLX_END }
00335 # define USING_TBCI using namespace TBCI;
00336 # define USING_GRID using namespace Grid;
00337 # define USING_FD using namespace Finite_Difference;
00338 # define USING_END using namespace std;
00339 # define USING_STD using namespace std;
00340 # define TBCI__ TBCI::
00341 # define FRIEND_TBCI__
00342 # define FRIEND_TBCI2__
00343 # define GRID__ Grid::
00344 # define FD__ FD::
00345 # define STD__ std::
00346 # define MATH__ std::
00347 # define CSTD__ std::
00348 # define CPLX__ std::
00349 # define GLBL__
00350 # define GLBL2__
00351 #endif
00352
00358 #ifndef NO_NS
00359
00360 # ifdef HAVE_LIBC_GLOBAL_NS_BUG
00361 # undef MATH__
00362 # define MATH__ ::
00363 # undef CSTD__
00364 # define CSTD__ ::
00365 # undef NAMESPACE_CSTD
00366 # define NAMESPACE_CSTD
00367 # undef NAMESPACE_CSTD_END
00368 # define NAMESPACE_CSTD_END
00369 # endif
00370
00371 # ifdef HAVE_CPP_GLOBAL_NS_BUG
00372 # undef STD__
00373 # define STD__ ::
00374 # undef USING_STD
00375 # define USING_STD using namespace;
00376 # undef NAMESPACE_STD
00377 # define NAMESPACE_STD
00378 # undef NAMESPACE_STD_END
00379 # define NAMESPACE_STD_END
00380 # endif
00381
00382 # ifdef HAVE_CPLX_GLOBAL_NS_BUG
00383 # undef CPLX__
00384 # define CPLX__ ::
00385 # undef NAMESPACE_CPLX
00386 # define NAMESPACE_CPLX
00387 # undef NAMESPACE_CPLX_END
00388 # define NAMESPACE_CPLX_END
00389 # endif
00390
00391
00392
00393
00394 # if defined(HAVE_LIBC_GLOBAL_NS_BUG) && !defined(HAVE_CPP_GLOBAL_NS_BUG)
00395 # define HAVE_LIBC_NEQ_CPP_BUG
00396 # endif
00397 # if defined(HAVE_LIBC_GLOBAL_NS_BUG) && !defined(HAVE_CPLX_GLOBAL_NS_BUG)
00398 # define HAVE_LIBC_NEQ_CPLX_BUG
00399 # endif
00400
00401
00402
00403 # if (HAVE_FRIEND_GLOBAL_NS_BUG || HAVE_NEED_FOR_FRIEND_SCOPE) && !defined(AUTO_DECL) && !defined(NO_NS_TBCI)
00404 # undef FRIEND_TBCI__
00405 # define FRIEND_TBCI__ TBCI::
00406 # ifdef HAVE_FRIEND_GLOBAL_NS_BUG
00407 # undef FRIEND_TBCI2__
00408 # define FRIEND_TBCI2__ TBCI::
00409 # endif
00410 # endif
00411
00412 #endif
00413
00414
00415 #ifdef ABORT_ON_ERR
00416 # define ABORT_RET(x) abort ()
00417 # define ABORT_RET_NR abort ()
00418 #else
00419 # define ABORT_RET(x) return x
00420 # define ABORT_RET_NR do {} while (0)
00421 #endif
00422
00427 #ifndef MIN_ALIGN
00428 # define MIN_ALIGN 8
00429 #endif
00430 #ifndef MIN_ALIGN2
00431 # define MIN_ALIGN2 16
00432 #endif
00433
00434 #undef ALIGN
00435 #if defined(HAVE_ALIGN_ATTR) || defined(HAVE_NEW_ALIGN_ATTR)
00436 # ifdef HAVE_NEW_ALIGN_ATTR
00437 # define ALIGN3(v,i,x) v __attribute__ ((aligned(x))) (i)
00438 # else
00439 # define ALIGN3(v,i,x) v(i) __attribute__ ((aligned(x)))
00440 # endif
00441 # define ALIGN2(v,x) v __attribute__ ((aligned(x)))
00442 # define ALIGN(x) __attribute__ ((aligned(x)))
00443 #else
00444 # ifdef HAVE_DECLSPEC_ALIGN
00445 # define ALIGN3(v,i,x) __declspec(align(x)) v(i)
00446 # define ALIGN2(v,x) __declspec(align(x)) v
00447 # define ALIGN(x) __attribute__ ((aligned(x)))
00448 # else
00449 # define ALIGN3(v,i,x) v(i)
00450 # define ALIGN2(v,x) v
00451 # define ALIGN(x)
00452 # endif
00453 #endif
00454 #ifdef HAVE_TEMPL_ALIGN_ATTR
00455 # define TALIGN(x) ALIGN(x)
00456 #else
00457 # define TALIGN(x)
00458 #endif
00459
00460 #ifdef HAVE_CONST_ATTR
00461 # define CONSTA __attribute__ ((const))
00462 # define TBCI_CONST(x) x __attribute__ ((const)); x
00463 #else
00464 # define CONSTA
00465 # define TBCI_CONST(x) x
00466 #endif
00467 #ifdef HAVE_REGPARM_ATTR
00468 # define REGPARMA(n) __attribute__ ((regparm(n)))
00469 # define REGPARM(n,x) x __attribute__ ((regparm(n))); x
00470 #else
00471 # define REGPARMA(n)
00472 # define REGPARM(n,x) x
00473 #endif
00474
00475 #ifdef HAVE_UNUSED_ATTR
00476 # define UNUSED __attribute__((unused))
00477 #else
00478 # define UNUSED
00479 #endif
00480
00481
00482 #ifdef HAVE_WEAK_ATTR
00483 # define WEAKA __attribute__ ((weak))
00484 # define WEAK(x) x __attribute__ ((weak)); x
00485 #ifdef __INTEL_COMPILER // Does not like that attr in templ specializations
00486 # define TWEAK(x) x
00487 #else
00488 # define TWEAK(x) x __attribute__ ((weak)); x
00489 #endif
00490 #else
00491 # define WEAKA
00492 # define WEAK(x) x
00493 # define TWEAK(x) x
00494 #endif
00495
00496 #if defined(HAVE_HOT_ATTR) && defined(USE_HOT)
00497 # define HOT __attribute__ ((hot))
00498 # define COLD __attribute__ ((cold))
00499 # define HOTDECL(x) x __attribute__ ((hot)); x
00500 # define COLDDECL(x) x __attribute__ ((cold)); x
00501 #else
00502 # define HOT
00503 # define COLD
00504 # define HOTDECL(x) x
00505 # define COLDDECL(x) x
00506 #endif
00507
00508
00509
00510 #if defined(NO_EXCEPT) || defined(HAVE_BUGGY_EXCEPTIONS)
00511 # undef EXCEPT
00512 #endif
00513
00514 #ifdef EXCEPT
00515 # include "except.h"
00516 #endif
00517
00518
00519
00520 #ifndef HAVE_PRETTY_FUNCTION
00521 # if defined(HAVE_FUNC) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
00522 # define __PRETTY_FUNCTION__ __func__
00523 # else
00524 # define __PRETTY_FUNCTION__ "<unknown>"
00525 # endif
00526 #endif
00527
00528
00529 #define TBCIERRH1 __PRETTY_FUNCTION__
00530 #define TBCIERRM(cond,exc,txt,ind) \
00531 STD__ string(#exc) + " in " + TBCIERRH1 + ":\n " + #txt + " at "\
00532 + __FILE__ + ":" + TBCI__ ltoa(__LINE__) + ":\n (" + #cond \
00533 + ") == TRUE! (" + #ind + " = " + TBCI__ ltoa(ind) + ")"
00534 #define TBCIERRS(cond,exc,txt,ind) \
00535 #exc << " in " << TBCIERRH1 << ":\n " << #txt << " at " \
00536 << __FILE__ << ":" << __LINE__ << ":\n (" << #cond \
00537 << ") == TRUE! (" << #ind << " = " << ind << ")"
00538
00539
00540 #ifdef ERRCHECK
00541 # ifdef EXCEPT
00542
00572 # define BCHK(cond,exc,txt,ind,rtval) \
00573 if (UNLIKELY(cond)) { \
00574 throw exc ((TBCIERRM(cond,exc,txt,ind)).c_str(), ind); \
00575 }
00576 # define BCHKNR(cond,exc,txt,ind) \
00577 if (UNLIKELY(cond)) { \
00578 throw exc ((TBCIERRM(cond,exc,txt,ind)).c_str(), ind); \
00579 }
00580
00581 # else
00582
00583 # define BCHK(cond,exc,txt,ind,rtval) \
00584 if (UNLIKELY(cond)) { \
00585 STD__ cerr << TBCIERRS(cond,exc,txt,ind) << STD__ endl; \
00586 ABORT_RET (rtval); \
00587 }
00588 # define BCHKNR(cond,exc,txt,ind) \
00589 if (UNLIKELY(cond)) { \
00590 STD__ cerr << TBCIERRS(cond,exc,txt,ind) << STD__ endl; \
00591 ABORT_RET_NR; \
00592 }
00593
00594 # endif
00595
00596 #else
00597
00598 # define BCHK(cond,exc,txt,ind,rtval) do {} while(0)
00599 # define BCHKNR(cond,exc,txt,ind) do {} while(0)
00600
00601 #endif
00602
00603
00604 #if defined(HAVE_ALGORITHM) && defined(HAVE_STD_MINMAX) &&!defined(HAVE_GCC_MINMAX)
00605 # include <algorithm>
00606 #endif
00607
00608 NAMESPACE_TBCI
00612 #ifndef MIN
00613 # ifdef HAVE_GCC_MINMAX
00614 # define MIN(a,b) ((a) <? (b))
00615 # elif defined(HAVE_STD_MINMAX)
00616 # define MIN(a,b) std::min(a,b)
00617 # else
00618
00619
00620 # define MIN(a,b) ((a) < (b)? (a) : (b))
00621 # endif
00622 #endif
00623
00624 #ifndef MAX
00625 # ifdef HAVE_GCC_MINMAX
00626 # define MAX(a,b) ((a) >? (b))
00627 # elif defined(HAVE_STD_MINMAX)
00628 # define MAX(a,b) std::max(a,b)
00629 # else
00630
00631
00632 # define MAX(a,b) ((a) > (b)? (a) : (b))
00633 # endif
00634 #endif
00635
00636
00637
00638 #ifdef DEBUGINFO
00639 # define INFO(x) cout << x
00640 #else
00641 # define INFO(x)
00642 #endif
00643
00644
00645
00646
00647 #if defined(RETVAL_OPT) && defined(HAVE_NAMED_RETVAL)
00648 # define RET(x) return(x);
00649 # define CONSTR(x)
00650 #else
00651 # define RET(x)
00652 # define CONSTR(x) x
00653 #endif
00654
00655
00656 #if defined(_MSC_VER)
00657 # define _VOID (void*)
00658 #else
00659 # define _VOID
00660 #endif
00661
00662
00663
00664 #ifndef HAVE_BCXX_TEMPL_INLINE_MFUNC_BUG
00665 # define INLINE inline
00666 #else
00667 # define INLINE
00668 #endif
00669
00670 NAMESPACE_END
00671
00672 #include "perf_opt.h"
00673 #include "tbci_traits.h"
00674
00675
00676
00677
00678
00679 #if defined(HAVE_BUILTIN_PREFETCH) && !defined(NO_PREFETCH)
00680 # define PREFETCH_R(addr,loc) __builtin_prefetch(addr, 0, loc )
00681 # define PREFETCH_W(addr,loc) __builtin_prefetch(addr, 1, loc )
00682 # define USE_PREFETCH 1
00683 #else
00684
00691 # if defined(__alpha__) && defined(AXP_PREFETCH) && !defined(NO_PREFETCH) && defined(__GNUC__)
00692 # define PREFETCH_R(addr,loc) asm (" lda $31,%0 \n" : : "m" (*(addr)) )
00693 # define PREFETCH_W(addr,loc) asm (" lda $31,%0 \n" : : "m" (*(addr)) )
00694 # define USE_PREFETCH 1
00695 # else
00696 # if defined(__i386__) && defined(SSE_PREFETCH) && !defined(NO_PREFETCH) && (defined(__GNUC__) || defined(__INTEL_COMPILER))
00697 # define PREFETCH_R(addr,loc) asm (" prefetcht0 %0 \n" : : "m" (*(addr)) )
00698 # define PREFETCH_W(addr,loc) asm (" prefetcht0 %0 \n" : : "m" (*(addr)) )
00699 # define USE_PREFETCH 1
00700 # else
00701 # if defined(__i386__) && defined(AMD_PREFETCH) && !defined(NO_PREFETCH) && (defined(__GNUC__) || defined(__INTEL_COMPILER))
00702 # define PREFETCH_R(addr,loc) asm (" prefetch %0 \n" : : "m" (*(addr)) )
00703 # define PREFETCH_W(addr,loc) asm (" prefetchw %0 \n" : : "m" (*(addr)) )
00704 # define USE_PREFETCH 1
00705 # else
00706 # define PREFETCH_R(addr,loc) do {} while (0)
00707 # define PREFETCH_W(addr,loc) do {} while (0)
00708 # undef USE_PREFETCH
00709 # endif
00710 # endif
00711 # endif
00712 #endif
00713
00714
00715 #ifdef HAVE_TLS
00716 # define THREAD__ __thread
00717 #elif defined(HAVE_DTLS)
00718 # define THREAD__ __declspec(thread)
00719 #else
00720 # define THREAD__
00721 #endif
00722
00723
00724 #ifdef HAVE_UNISTD_H
00725 # include "smp.h"
00726 #else
00727 # define MAIN_PID (getpid())
00728 # define num_threads (0)
00729 # define thrno (0)
00730 # define ismainthread (1)
00731 #endif
00732
00733
00734 #ifdef __SSE2__
00735 # define TBCI_SIMD_ALIGN 16
00736 #endif
00737
00738 #ifdef USE_PLAIN_VEC_KERNELS
00739 # include "plain_def.h"
00740 #elif defined(USE_UNR_VEC_KERNELS2)
00741 # include "unroll_prefetch_def2.h"
00742 #else
00743 # include "unroll_prefetch_def.h"
00744 #endif
00745
00746
00747 NAMESPACE_TBCI
00748
00758 template <typename T>
00759 inline void SWAP (T& a, T& b)
00760 {
00761 register T ALIGN(MIN_ALIGN) tmp = a; a = b; b = tmp;
00762 }
00763
00765 #ifdef C_MEMALLOC
00766
00774 # if defined(__GNUC__) && !defined(C_MEMALLOC_WE_KNOW_WHAT_WE_DO)
00775 # warning "Don't define C_MEMALLOC if you use containers with elements which need intitalization"
00776 # warning " or take care yourself ! (Read basics.h:690)"
00777 # endif
00778 # ifdef HAVE_MEMALIGN
00779 # define NEW(t,s) (t*) memalign (sizeof(t)*MIN(4,s>>1), sizeof(t)*(s))
00780 # else
00781 # define NEW(t,s) (t*) CSTD__ malloc (sizeof(t)*s)
00782 # endif
00783 # define TBCIDELETE(t,v,sz) do { BCHKNR(!v,NumErr,free null ptr,0); CSTD__ free (v); v = 0; } while (0)
00784 # define TBCIDELETE_RO(t,v,sz) do { BCHKNR(!v,NumErr,free null ptr,0); CSTD__ free (v); } while (0)
00785 # define REALLOC(v,os,t,s) v = (t*) CSTD__ realloc ((v), sizeof(t)*(s))
00786 #elif !defined(NO_MALLOC_CACHE) //&& !defined(SMP)
00787 NAMESPACE_END
00788 # if defined(PRAGMA_I) && defined(PRAGMA_IMPL_MALLOC_CACHE)
00789 # pragma implementation "malloc_cache.h"
00790 # endif
00791 # include "malloc_cache.h"
00792 NAMESPACE_TBCI
00793 #else
00794 # define NEW(t,s) new t[s]
00795 # define TBCIDELETE(t,v,sz) do { BCHKNR(!v,NumErr,delete[] null ptr,0); delete[] _VOID v; v = 0; } while (0)
00796 # define TBCIDELETE_RO(t,v,sz) do { BCHKNR(!v,NumErr,delete[] null ptr,0); delete[] _VOID v; } while (0)
00797 # ifdef ALLOW_MEMCPY
00798 # define REALLOC(v,os,t,s) do { \
00799 t* _tmp = (v); \
00800 if (LIKELY(s)) (v) = new t[(s)]; else (v) = 0; \
00801 if (LIKELY(_tmp != (v) && _tmp && (v) && (os) && (s))) \
00802 CSTD__ memcpy ((v), _tmp, sizeof(t)*MIN((os),(s))); \
00803 if (LIKELY(_tmp)) delete[] (_tmp); \
00804 } while (0)
00805 # else
00806 # define REALLOC(v,os,t,s) do { \
00807 t* _tmp = (v); \
00808 if (LIKELY(s)) (v) = new t[(s)]; else (v) = 0; \
00809 if (LIKELY(_tmp != (v) && _tmp && (v) && (os) && (s))) { \
00810 for (register unsigned long _i = 0; _i < MIN((s),(os)); _i++) \
00811 (v)[_i] = _tmp[_i]; } \
00812 if (LIKELY(_tmp)) delete[] (_tmp); \
00813 } while (0)
00814 # endif
00815 #endif
00816
00827 #if defined(C_MEMALLOC) || defined(ALLOW_MEMCPY)
00828 # if defined(__GNUC__) && !defined(C_MEMALLOC_WE_KNOW_WHAT_WE_DO)
00829 # warning "Don't define C_MEMALLOC or ALLOW_MEMCPY if you use containers with elements which"
00830 # warning " use dynamic memory allocation or handle with pointers somehow. Copying otherwise"
00831 # warning " will break things. (Read basics.h:740)"
00832 # endif
00833 # define TBCICOPY(n,o,t,s) CSTD__ memcpy ((n),(o),(s)*sizeof(t))
00834 #else
00835 # define VEC_INLINE inline
00836 # define COPY2(res,v1,f1,f2) res = v1
00837 VKERN_TEMPL_2V(_tbci_copy,COPY2)
00838 # define TBCICOPY(n,o,t,s) _tbci_copy < t > (s,n,o)
00839 # undef VEC_INLINE
00840 #endif
00841
00842
00843 #if defined(C_MEMALLOC) || defined(ALLOW_MEMSET)
00844 # define TBCICLEAR(n,t,s) CSTD__ memset ((n), 0, (s)*sizeof(t))
00845 #else
00846 # define VEC_INLINE
00847 # define FILL1(res,f1,f2) res = f2
00848 VKERN_TEMPL_1V_C(_tbci_fill,FILL1)
00849 # define TBCICLEAR(n,t,s) _tbci_fill < t > (s,n,t(0))
00850 # define TBCIFILL(n,v,t,s) _tbci_fill < t > (s,n,v)
00851 # undef VEC_INLINE
00852 #endif
00853
00854
00862 #if defined(C_MEMALLOC) || defined(ALLOW_MEMCMP)
00863 # define TBCICOMP(n,o,t,s) CSTD__ memcmp ((n),(o),(s)*sizeof(t))
00864
00865 # define TBCIFILL(n,v,t,s) \
00866 do { for (register unsigned long _i = 0; _i < (s); _i++) \
00867 (n)[_i] = (t)(v); } while (0)
00868 #else
00869 # if defined(__GNUG__) && !defined(PEDANTIC)
00870 # define TBCICOMP(n,o,t,s) ({ \
00871 register int _r; register unsigned long _i; \
00872 for (_r=0, _i=0; _i<(s) && !_r; _i++) { \
00873 _r = (int)((n)[_i] != (o)[_i]); \
00874 if (_r) break; \
00875 } \
00876 _r; \
00877 })
00878 # else
00879
00880 HOTDECL(template <typename T>
00881 inline int _tbci_comp (T const *n, T const *o, const unsigned long s))
00882 {
00883
00884 for (register unsigned long _i = 0; _i < (s); _i++)
00885 if (UNLIKELY((n)[_i] != (o)[_i])) return 1;
00886 return 0;
00887 }
00888 # define TBCICOMP(n,o,t,s) _tbci_comp ((n),(o),(s))
00889 # endif
00890 #endif
00891
00892
00893
00894 NAMESPACE_END
00895
00919 NAMESPACE_CPLX
00920 #define _REF_
00922 TBCI_CONST(inline int conj (const int _REF_ arg)) {return arg;}
00923 TBCI_CONST(inline unsigned conj (const unsigned _REF_ arg)) {return arg;}
00924 TBCI_CONST(inline long conj (const long _REF_ arg)) {return arg;}
00925 TBCI_CONST(inline short conj (const short _REF_ arg)) {return arg;}
00926 TBCI_CONST(inline char conj (const char _REF_ arg)) {return arg;}
00927 TBCI_CONST(inline float conj (const float _REF_ arg)) {return arg;}
00928 TBCI_CONST(inline double conj (const double _REF_ arg)) {return arg;}
00929
00930 TBCI_CONST(inline int real (const int _REF_ d)) { return d; }
00931 TBCI_CONST(inline unsigned real (const unsigned _REF_ d)) { return d; }
00932 TBCI_CONST(inline float real (const float _REF_ d)) { return d; }
00933 TBCI_CONST(inline double real (const double _REF_ d)) { return d; }
00934
00935 TBCI_CONST(inline int imag (const int _REF_ d)) { return 0; }
00936 TBCI_CONST(inline unsigned imag (const unsigned _REF_ d)) { return 0; }
00937 TBCI_CONST(inline float imag (const float _REF_ d)) { return 0; }
00938 TBCI_CONST(inline double imag (const double _REF_ d)) { return 0; }
00939
00940 #ifdef HAVE_LONG_DOUBLE
00941 TBCI_CONST(inline long double conj (const long double _REF_ arg)) {return arg;}
00942 TBCI_CONST(inline long double real (const long double _REF_ d)) { return d; }
00943 TBCI_CONST(inline long double imag (const long double _REF_ d)) { return 0; }
00944 #endif
00945 #ifdef HAVE_LONG_LONG
00946 TBCI_CONST(inline long long conj (const long long _REF_ arg)) {return arg;}
00947 TBCI_CONST(inline long long real (const long long _REF_ d)) { return d; }
00948 TBCI_CONST(inline long long imag (const long long _REF_ d)) { return 0; }
00949 #endif
00950 #undef _REF_
00951 NAMESPACE_CPLX_END
00952
00953 #ifdef _INCLUDE_CPLX_H
00954 # include "cplx.h"
00955 #endif
00956
00957 #ifdef _INCLUDE_STDCPLX_H
00958 # include "std_cplx.h"
00959 #endif
00960
00961 #ifdef _INCLUDE_BUILTINCPLX_H
00962 # include "builtin_cplx.h"
00963 #endif
00964
00965
00966 NAMESPACE_TBCI
00968
00969 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 600)
00970 template <typename T> inline int sign (const T& x)
00971 #else
00972 TBCI_CONST(template <typename T> inline int sign (const T& x))
00973 #endif
00974 {
00975 if (LIKELY(CPLX__ real(x) > 0)) return 1;
00976 else if(LIKELY(CPLX__ real(x) < 0)) return -1;
00977 else return 0;
00978 }
00979
01016 #ifdef TEMPLATED_FABSSQR
01017 TBCI_CONST(template <typename T> inline double fabssqr (const T& a))
01018 { return CPLX__ real(a * CPLX__ conj(a)); }
01019 #else
01020 # define FABSSQR(T) \
01021 TBCI_CONST(inline double fabssqr (const T a)) \
01022 { return CPLX__ real(a * CPLX__ conj(a)); }
01023
01024 FABSSQR(double)
01025 FABSSQR(float)
01026 FABSSQR(int)
01027 FABSSQR(unsigned)
01028 # ifdef HAVE_LONG_DOUBLE
01029 FABSSQR(long double)
01030 # endif
01031 # ifdef HAVE_LONG_LONG
01032 FABSSQR(long long)
01033 # endif
01034 # undef FABSSQR
01035 #endif
01036
01037
01038 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 600)
01039 template <typename T> inline T sqr (const T& a)
01040 #else
01041 TBCI_CONST(template <typename T> inline T sqr (const T& a))
01042 #endif
01043 { return a*a; }
01044
01045
01046 #if !defined(HAVE_WIN_32) && !defined (NO_NS) // Problems with other dot
01047 # if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 600)
01048 template <typename T> inline T dot (const T& a1, const T& a2)
01049 # else
01050 TBCI_CONST(template <typename T> inline T dot (const T& a1, const T& a2))
01051 # endif
01052 { return a1 * CPLX__ conj (a2); }
01053 #endif
01054
01055 NAMESPACE_END
01056
01057 NAMESPACE_CSTD
01058
01059 #if 0
01060
01061 TBCI_CONST(template <typename T> inline double fabs (const T& a))
01062 { return a<(T)0?(double)-a:(double)a; }
01063
01064 #else
01065
01066
01067 # define FABS(T) \
01068 TBCI_CONST(inline double fabs (const T a)) \
01069 { return a<(T)0?(double)-a:(double)a; }
01070
01071 # define SQRT(T) \
01072 TBCI_CONST(inline double sqrt (const T a)) \
01073 { return MATH__ sqrt ((double)a); }
01074
01075 # ifdef HAVE_LONG_DOUBLE
01076 # if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ < 97
01077 FABS(long double)
01078 SQRT(long double)
01079 # endif
01080 # endif
01081
01082 FABS(int)
01083 SQRT(int)
01084
01085 # ifdef HAVE_LONG_LONG
01086 FABS(long long)
01087 SQRT(long long)
01088 # endif
01089
01090 TBCI_CONST(inline double fabs (const unsigned int a))
01091 { return (double)a; }
01092
01093 # undef FABS
01094 #endif
01095
01096 NAMESPACE_CSTD_END
01097
01098 NAMESPACE_CSTD
01099 #if 0
01100
01101 TBCI_CONST(template <typename T> inline T abs (const T& a))
01102 { return a<(T)0?-a:a; }
01103
01104 #else
01105
01106 # define ABS(T) \
01107 TBCI_CONST(inline T abs (const T a)) \
01108 { return a<(T)0?-a:a; }
01109
01110 # ifdef HAVE_MISS_CSTD_ABS_BUG
01111 # ifdef HAVE_LONG_DOUBLE
01112 ABS(long double)
01113 # endif
01114 ABS(double)
01115 ABS(float)
01116 # ifdef HAVE_LONG_LONG
01117
01118 # endif
01119
01120 # endif
01121
01122 # if 1 //defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 600)
01123 ABS(unsigned)
01124 # endif
01125 # undef ABS
01126 #endif
01127
01128 NAMESPACE_CSTD_END
01129
01130 #ifdef INLINE_VEC_KERNELS
01131 # define VEC_INLINE inline
01132 #else
01133 # define VEC_INLINE
01134 #endif
01135
01136 NAMESPACE_TBCI
01137
01139 enum _vararg { vag0=0, vag1, vag2, vag3, vag4, vag5, vag6, vag7, vag8,
01140 vag9, vag10, vag11, vag12, vag13, vag14, vag15, vag16 };
01141 typedef enum _vararg vararg;
01142
01143 NAMESPACE_END
01144
01145 #ifndef _NO_INCLUDE_COST_H
01146 # include "cost.h"
01147 #endif
01148
01149 #if !defined(NO_NS) && defined (USING_STD_NS)
01150 using std::cin;
01151 using std::cout;
01152 using std::cerr;
01153 using std::endl;
01154 using std::flush;
01155 using std::dec;
01156 using std::hex;
01157 using std::setw;
01158 using std::setprecision;
01159 using std::istream;
01160 using std::ostream;
01161 using std::ifstream;
01162 using std::ofstream;
01163 using std::string;
01164 # if defined(HAVE_SSTREAM) && !defined(HAVE_BUGGY_SSTREAM)
01165 using std::istringstream;
01166 using std::ostringstream;
01167 # else
01168 using std::istrstream;
01169 using std::ostrstream;
01170 # endif
01171 #endif
01172
01173 #endif