File: cf_inline.cc

package info (click to toggle)
singular 1%3A4.1.1-p2%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 35,860 kB
  • sloc: cpp: 288,280; ansic: 17,387; lisp: 4,242; yacc: 1,654; python: 1,608; makefile: 1,424; lex: 1,387; perl: 632; sh: 567; xml: 182
file content (570 lines) | stat: -rw-r--r-- 15,043 bytes parent folder | download | duplicates (2)
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* emacs edit mode for this file is -*- C++ -*- */

/**
 *
 * @file cf_inline.cc
 *
 * definition of configurable inline
 *   `CanonicalForm' methods.
 *
 * Hierarchy: canonicalform
 *
 * Header file: canonicalform.h
 *
 * Developers note:
 * ----------------
 * The central class in Factory is, of course, `CanonicalForm'.
 * Hence it is a quiet reasonable to assume that inlining its
 * most important methods will improve execution speed.  The same
 * holds for some methods of the `CFIterator' class.  Everything
 * on configurable inline `CanonicalForm' methods explained here
 * applies mutatis mutandis to the `CFIterator' methods.
 *
 * However, inlining `CanonicalForm' methods has two major
 * drawbacks:
 *
 * o If `CanonicalForm' methods simply would have been declared
 *   `inline' it would have been necessary to include the
 *   definition of `InternalCF' in `factory.h'.  This would have
 *   been quite a contradiction to the internal nature of the
 *   class.
 *   Hence it seemed desirable to introduce a mechanism to have
 *   both the inlined versions for internal use and compiled
 *   versions for the library.
 *
 * o Second, inlining in most cases leads to larger object code.
 *   E.g., inlining `CanonicalForm::~CanonicalForm()' increases the
 *   object code by approx. 15% without any effect on computation
 *   speed.
 *   Thus another design aim was to keep things configurable.
 *   That is why the methods defined here are called
 *   "configurable inline methods".
 *
 * The low level solution to both problems is the macro
 * `CF_INLINE' which either expands to `inline' or nothing.  The
 * counterpart `CF_NO_INLINE' exists only for convenience, it
 * always expands to nothing.  `CF_INLINE' is set immediately
 * before defining resp. declaring the methods to exclude any
 * esoteric influences from included files.
 *
 * The high level interface is the macro `CF_USE_INLINE'.  If it
 * is defined any header file that uses configurable inline
 * methods defines them to be `inline', otherwise they are
 * defined as ordinary methods.  `CF_USE_INLINE' is defined in
 * `config.h' only.
 *
 * To switch on (off) all configurable inline methods, it is
 * sufficient to define (undefine) `CF_USE_INLINE' in `config.h'.
 * To switch off separate configurable inline methods it is
 * necessary to prefix their declaration in `canonicalform.h' by
 * `CF_NO_INLINE' instead of `CF_INLINE'.  Furthermore, to avoid
 * duplicate symbols at link time, their definition in this file
 * has to be wrapped by an `#ifndef INCL_CF_INLINE_CC'.
 *
 * It turned out that inlining the following methods (and only
 * them) results in the best time to size ratio on Linux and HP
 * machines:
 * o all `CanonicalForm' constructors
 * o the binary `CanonicalForm' operators `+' and `*'
 *
**/

// check whether we are included or translated and
// define `INCL_CF_INLINE_CC' if we are included
#ifdef INCL_CANONICALFORM_H
#define INCL_CF_INLINE_CC
#endif


#include "config.h"


#include "cf_assert.h"

// temporarily switch off `CF_USE_INLINE' and include
// `canonicalform.h' if we are being translated.
// `CF_USE_INLINE_SAVE' is used to save the state of
// `CF_USE_INLINE'.  It is unset after use.
#ifndef INCL_CF_INLINE_CC
#ifdef CF_USE_INLINE
#define CF_USE_INLINE_SAVE
#undef CF_USE_INLINE
#endif
#include "canonicalform.h"
#ifdef CF_USE_INLINE_SAVE
#define CF_USE_INLINE
#undef CF_USE_INLINE_SAVE
#endif
#endif /* ! INCL_CF_INLINE_CC */

// regular include files
#include "int_cf.h"
#include "imm.h"
#include "cf_factory.h"

// set the value of `CF_INLINE' for the following methods and
// functions
#if defined( CF_USE_INLINE ) && defined( INCL_CF_INLINE_CC )
#undef CF_INLINE
#define CF_INLINE inline
#else
#undef CF_INLINE
#define CF_INLINE
#endif /* ! defined( CF_USE_INLINE ) && defined( INCL_CF_INLINE_CC ) */

// constructors, destructors, assignment
/** CF_INLINE CanonicalForm::CanonicalForm ()
 *
 *
 * CanonicalForm() - create the default canonical form.
 *
 * The canonical form is initialized to zero from the current
 * domain.
 *
**/
CF_INLINE
CanonicalForm::CanonicalForm ()
    : value( CFFactory::basic( 0L ) )
{
}

/** CF_INLINE CanonicalForm::CanonicalForm ( const int i )
 *
 *
 * CanonicalForm() - create a canonical form from an integer.
 *
 * The canonical form is initialized to the "canonical image" of
 * `i' in the current domain.  This is `i' itself for
 * characteristic zero, `i' mod p for finite fields of
 * characteristic p, and `i' mod p^n for prime power domains with
 * p^n elements.
 *
**/
CF_INLINE
CanonicalForm::CanonicalForm ( const int i )
    : value( CFFactory::basic( (const long)i ) )
{
}

CF_INLINE
CanonicalForm::CanonicalForm ( const long i )
    : value( CFFactory::basic( i ) )
{
}

/** CF_INLINE CanonicalForm::CanonicalForm ( const CanonicalForm & cf )
 *
 *
 * CanonicalForm() - create a copy of a canonical form.
 *
 * Type info:
 * ----------
 * cf: Anything
 *
**/
CF_INLINE
CanonicalForm::CanonicalForm ( const CanonicalForm & cf )
    : value( is_imm( cf.value ) ? cf.value : cf.value->copyObject() )
{
}

/** CF_INLINE CanonicalForm::CanonicalForm ( InternalCF * cf )
 *
 *
 * CanonicalForm() - create a canonical form from a pointer to an
 *   internal canonical form.
 *
 * This constructor is reserved for internal usage.
 *
 * Developers note:
 * ----------------
 * The canonical form gets its value immediately from `cf'.
 * `cf's reference counter is not incremented, so be careful with
 * this constructor.
 *
**/
CF_INLINE
CanonicalForm::CanonicalForm ( InternalCF * cf )
    : value( cf )
{
}

/** CF_INLINE CanonicalForm::CanonicalForm ( const Variable & v )
 *
 *
 * CanonicalForm() - create a canonical form from a variable.
 *
 * If `v' is a polynomial variable or an algebraic element the
 * resulting polynomial (or algebraic element) is 1*`v'^1, the
 * one being from the current domain.
 *
 * Variables of level `LEVELBASE' are transformed to one from the
 * current domain.
 *
 * Type info:
 * ----------
 * v: Anything
 *
**/
CF_INLINE
CanonicalForm::CanonicalForm ( const Variable & v )
    : value( CFFactory::poly( v ) )
{
}

/** CF_INLINE CanonicalForm::CanonicalForm ( const Variable & v, int e )
 *
 *
 * CanonicalForm() - create a canonical form from a power of a
 *   variable.
 *
 * If `v' is a polynomial variable or an algebraic element the
 * resulting polynomial (or algebraic element) is 1*`v'^`e', the
 * one being from the current domain.  Algebraic elements are
 * reduced modulo their minimal polynomial.
 *
 * Variables of level `LEVELBASE' are transformed to one from the
 * current domain.
 *
 * Type info:
 * ----------
 * v: Anything
 *
**/
CF_INLINE
CanonicalForm::CanonicalForm ( const Variable & v, int e )
    : value( CFFactory::poly( v, e ) )
{
    //ASSERT( e > 0, "math error: exponent has to be positive" );
}

#ifndef INCL_CF_INLINE_CC
/** CF_INLINE CanonicalForm::~CanonicalForm ()
 *
 *
 * ~CanonicalForm() - delete CO.
 *
 * Type info:
 * ----------
 * CO: Anything
 *
**/
CF_INLINE
CanonicalForm::~CanonicalForm ()
{
    if ( (! is_imm( value )) && value->deleteObject() )
        delete value;
}
#endif

#ifndef INCL_CF_INLINE_CC
/** CF_INLINE CanonicalForm & CanonicalForm::operator = ( const CanonicalForm & cf )
 *
 *
 * operator =() - assign `cf' to CO.
 *
 * Type info:
 * ----------
 * CO, cf: Anything
 *
**/
CF_INLINE CanonicalForm &
CanonicalForm::operator = ( const CanonicalForm & cf )
{
    if ( this != &cf ) {
        if ( (! is_imm( value )) && value->deleteObject() )
            delete value;
        value = (is_imm( cf.value )) ? cf.value : cf.value->copyObject();
    }
    return *this;
}

/**
 *
 * operator =() - assign long `cf' to CO.
 *
 * `cf' converted to a canonical form as described in the
 * canonical form constructor which creates a canonical form from
 * an integer.
 *
 * Type info:
 * ----------
 * CO: Anything
 *
 * Developers note:
 * ----------------
 * Strictly speaking, this operator is superfluous.  The ordinary
 * assignment operator together with automatic conversion from
 * `int' to `CanonicalForm' would do the job, too.  But this way
 * the common operation of assigning an integer is faster.
 *
**/
CF_INLINE CanonicalForm &
CanonicalForm::operator = ( const long cf )
{
    if ( (! is_imm( value )) && value->deleteObject() )
        delete value;
    value = CFFactory::basic( cf );
    return *this;
}
#endif

// predicates
#ifndef INCL_CF_INLINE_CC
/** CF_INLINE bool CanonicalForm::isOne, isZero () const
 *
 *
 * isOne(), isZero() - test whether a `CanonicalForm' equals one
 *   or zero, resp.
 *
 * The predicates `isOne()' and `isZero()' are much faster than
 * the comparison operators.  Furthermore, a test `f.isZero()' is
 * independent from the current domain, whereas an expression
 * `f == 0' is not.
 *
 * Type info:
 * ----------
 * CO: Anything
 *
 * Internal implementation:
 * ------------------------
 * Note that only immediate objects and objects of class
 * `InternalPrimePower' may equal one or zero, resp.
 *
 * imm_isone(), imm_iszero()
 * Trivial.
 *
 * imm_isone_p(), imm_iszero_p()
 * Trivial.
 *
 * imm_isone_gf(), imm_iszero_gf()
 * Use `gf_isone()' and `gf_iszero()', resp., to test whether CO
 * equals zero or one, resp.
 *
 * InternalCF::isOne(), isZero()
 * Always return false.
 *
 * InternalPrimePower::isOne(), isZero()
 * Use `mpz_cpm_ui()' resp. `mpz_sgn()' to check the underlying
 * mpi.
 *
 * @sa CanonicalForm::isZero()
**/
CF_INLINE bool
CanonicalForm::isOne () const
{
    int what = is_imm( value );

    if ( ! what )
        return value->isOne();
    else  if ( what == INTMARK )
        return imm_isone( value );
    else if ( what == FFMARK )
        return imm_isone_p( value );
    else
        return imm_isone_gf( value );
}

/**
 * @sa CanonicalForm::isOne()
**/
CF_INLINE bool
CanonicalForm::isZero () const
{
    int what = is_imm( value );

    if ( what == 0 )
        return value->isZero();
    else  if ( what == INTMARK )
        return imm_iszero( value );
    else if ( what == FFMARK )
        return imm_iszero_p( value );
    else
        return imm_iszero_gf( value );
}
#endif

// arithmetic operators
#ifndef INCL_CF_INLINE_CC
/** CF_INLINE CanonicalForm operator - ( const CanonicalForm & cf )
 *
 *
 * operator -() - return additive inverse of `cf'.
 *
 * Returns the additive inverse of `cf'.  One should keep in mind
 * that to negate a canonical form a complete (deep) copy of it
 * has to be created.
 *
 * Type info:
 * ----------
 * cf: CurrentPP
 *
 * In fact, the type is almost `Anything', but it is, e.g., not
 * possible to invert an element from a finite field when the
 * characteristic of the current domain has changed.
 *
 * Internal implementation:
 * ------------------------
 * All internal methods check whether the reference counter
 * equals one.  If so CO is negated in-place.  Otherwise, a new
 * copy of CO is created and negated.
 *
 * imm_neg()
 * Trivial.
 *
 * imm_neg_p()
 * Use `ff_neg()' to negate CO.
 *
 * imm_neg_gf()
 * Use `gf_neg()' to negate CO.
 *
 * InternalInteger::neg()
 * Use `mpz_neg()' to negate the underlying mpi.
 *
 * InternalRational::neg ()
 * Use `mpz_neg()' to negate the denominator.
 *
 * InternalPrimePower::neg()
 * Subtract CO from `primepow' using `mpz_sub'.
 *
 * InternalPoly::neg()
 * If reference counter is one use `negateTermList()' to negate
 * the terms, otherwise create a negated copy using
 * `copyTermList()'.
 *
 * @sa CanonicalForm::operator -=()
**/
CF_INLINE CanonicalForm
operator - ( const CanonicalForm & cf )
{
    CanonicalForm result( cf );
    int what = is_imm( result.value );

    if ( ! what )
        result.value = result.value->neg();
    else  if ( what == INTMARK )
        result.value = imm_neg( result.value );
    else if ( what == FFMARK )
        result.value = imm_neg_p( result.value );
    else
        result.value = imm_neg_gf( result.value );

    return result;
}
#endif

// binary arithmetic operators and functions
/** CF_INLINE CanonicalForm operator +, -, *, /, % ( const CanonicalForm & lhs, const CanonicalForm & rhs )
 *
 *
 * operators +, -, *, /, %(), div(), mod() - binary arithmetic
 *   operators.
 *
 * The binary operators have their standard (mathematical)
 * semantics.  As explained for the corresponding arithmetic
 * assignment operators, the operators `/' and `%' return the
 * quotient resp. remainder of (polynomial) division with
 * remainder, whereas `div()' and `mod()' may be used for exact
 * division and term-wise remaindering, resp.
 *
 * It is faster to use the arithmetic assignment operators (e.g.,
 * `f += g;') instead of the binary operators (`f = f+g;' ).
 *
 * Type info:
 * ----------
 * lhs, rhs: CurrentPP
 *
 * There are weaker preconditions for some cases (e.g.,
 * arithmetic operations with elements from Q or Z work in any
 * domain), but type `CurrentPP' is the only one guaranteed to
 * work for all cases.
 *
 * Developers note:
 * ----------------
 * All binary operators have their corresponding `CanonicalForm'
 * assignment operators (e.g., `operator +()' corresponds to
 * `CanonicalForm::operator +=()', `div()' corresponds to
 * `CanonicalForm::div()).
 *
 * And that is how they are implemented, too: Each of the binary
 * operators first creates a copy of `lhs', adds `rhs' to this
 * copy using the assignment operator, and returns the result.
 *
 * @sa CanonicalForm::operator +=()
**/
CF_INLINE CanonicalForm
operator + ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result += rhs;
    return result;
}

#ifndef INCL_CF_INLINE_CC
CF_INLINE CanonicalForm
operator - ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result -= rhs;
    return result;
}
#endif

/**
 * @sa CanonicalForm::operator *=()
**/
CF_INLINE CanonicalForm
operator * ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result *= rhs;
    return result;
}

#ifndef INCL_CF_INLINE_CC
/**
 * @sa CanonicalForm::operator /=()
**/
CF_INLINE CanonicalForm
operator / ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result /= rhs;
    return result;
}

/**
 * @sa CanonicalForm::operator %=()
**/
CF_INLINE CanonicalForm
operator % ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result %= rhs;
    return result;
}
#endif

#ifndef INCL_CF_INLINE_CC
/** CF_INLINE CanonicalForm div, mod ( const CanonicalForm & lhs, const CanonicalForm & rhs )
 * @sa mod(), operator/(), CanonicalForm::operator /=()
**/
CF_INLINE CanonicalForm
div ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result.div( rhs );
    return result;
}

/**
 * @sa div(), operator%(), CanonicalForm::operator %=()
**/
CF_INLINE CanonicalForm
mod ( const CanonicalForm & lhs, const CanonicalForm & rhs )
{
    CanonicalForm result( lhs );
    result.mod( rhs );
    return result;
}
#endif