source: git/factory/cf_util.cc

spielwiese
Last change on this file was 3edea1, checked in by Hans Schoenemann <hannes@…>, 3 years ago
cygwin port: shared lib libfactory
  • Property mode set to 100644
File size: 1.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3/**
4 *
5 * @file cf_util.cc
6 *
7 * miscellaneous functions, not necessarily related
8 *   to canonical forms.
9 *
10 * Used by: fac_cantzass.cc, gfops.cc
11 *
12**/
13
14
15#include "globaldefs.h"
16#include "config.h"
17#include "cf_util.h"
18
19
20/** int ipower ( int b, int m )
21 *
22 * ipower() - calculate b^m in standard integer arithmetic.
23 *
24 * Note: Beware of overflows.
25 *
26**/
27int ipower ( int b, int m )
28{
29    int prod = 1;
30
31    while ( m != 0 )
32    {
33        if ( m % 2 != 0 )
34            prod *= b;
35        m /= 2;
36        if ( m != 0 )
37            b *= b;
38    }
39    return prod;
40}
41
42int ilog2 (int v)
43{
44  const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
45  const unsigned int S[] = {1, 2, 4, 8, 16};
46
47  unsigned int r = 0; // result of log2(v) will go here
48  if (v & b[4]) { v >>= S[4]; r |= S[4]; }
49  if (v & b[3]) { v >>= S[3]; r |= S[3]; }
50  if (v & b[2]) { v >>= S[2]; r |= S[2]; }
51  if (v & b[1]) { v >>= S[1]; r |= S[1]; }
52  if (v & b[0]) { v >>= S[0]; r |= S[0]; }
53  return (int)r;
54}
55
56int igcd( int a, int b )
57{
58    if ( a < 0 ) a = -a;
59    if ( b < 0 ) b = -b;
60
61    int c;
62
63    while ( b != 0 )
64    {
65        c = a % b;
66        a = b;
67        b = c;
68    }
69    return a;
70}
71
72#include<stdio.h>
73#include<stdlib.h>
74
75void factoryError_intern(const char *s)
76{
77  fputs(s,stderr);
78  abort();
79}
80VAR void (*factoryError)(const char *s) = factoryError_intern;
81
82
Note: See TracBrowser for help on using the repository browser.