source: git/factory/cf_util.cc @ 33dd62

spielwiese
Last change on this file since 33dd62 was 40094f, checked in by Hans Schoenemann <hannes@…>, 4 years ago
move SI_LOG2 to factory/si_log2.h
  • 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
18
19/** int ipower ( int b, int m )
20 *
21 * ipower() - calculate b^m in standard integer arithmetic.
22 *
23 * Note: Beware of overflows.
24 *
25**/
26int ipower ( int b, int m )
27{
28    int prod = 1;
29
30    while ( m != 0 )
31    {
32        if ( m % 2 != 0 )
33            prod *= b;
34        m /= 2;
35        if ( m != 0 )
36            b *= b;
37    }
38    return prod;
39}
40
41int ilog2 (int v)
42{
43  const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
44  const unsigned int S[] = {1, 2, 4, 8, 16};
45
46  unsigned int r = 0; // result of log2(v) will go here
47  if (v & b[4]) { v >>= S[4]; r |= S[4]; }
48  if (v & b[3]) { v >>= S[3]; r |= S[3]; }
49  if (v & b[2]) { v >>= S[2]; r |= S[2]; }
50  if (v & b[1]) { v >>= S[1]; r |= S[1]; }
51  if (v & b[0]) { v >>= S[0]; r |= S[0]; }
52  return (int)r;
53}
54
55int igcd( int a, int b )
56{
57    if ( a < 0 ) a = -a;
58    if ( b < 0 ) b = -b;
59
60    int c;
61
62    while ( b != 0 )
63    {
64        c = a % b;
65        a = b;
66        b = c;
67    }
68    return a;
69}
70
71#include<stdio.h>
72#include<stdlib.h>
73
74void factoryError_intern(const char *s)
75{
76  fputs(s,stderr);
77  abort();
78}
79VAR void (*factoryError)(const char *s) = factoryError_intern;
80
81
Note: See TracBrowser for help on using the repository browser.