~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/include/impl/math.h

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
 
2
 * 
 
3
 * Shared math routines.
 
4
 *
 
5
 * Copyright (c) 2006-2010 Miru Limited.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#if !defined (__PGM_IMPL_FRAMEWORK_H_INSIDE__) && !defined (PGM_COMPILATION)
 
23
#       error "Only <framework.h> can be included directly."
 
24
#endif
 
25
 
 
26
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 
27
#       pragma once
 
28
#endif
 
29
#ifndef __PGM_IMPL_MATH_H__
 
30
#define __PGM_IMPL_MATH_H__
 
31
 
 
32
#include <pgm/types.h>
 
33
 
 
34
PGM_BEGIN_DECLS
 
35
 
 
36
/* fast log base 2 of power of 2
 
37
 */
 
38
 
 
39
static inline unsigned pgm_power2_log2 (unsigned) PGM_GNUC_CONST;
 
40
 
 
41
static inline
 
42
unsigned
 
43
pgm_power2_log2 (
 
44
        unsigned        v
 
45
        )
 
46
{
 
47
        static const unsigned int b[] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
 
48
        unsigned int r = (v & b[0]) != 0;
 
49
#if defined(__STDC_VERSION__) && (__STDC_VERSION >= 199901L)
 
50
/* C99 version */
 
51
        for (unsigned i = 4; i > 0; i--) {
 
52
                r |= ((v & b[i]) != 0) << i;
 
53
        }
 
54
#else
 
55
/* C89 version */
 
56
        {
 
57
        unsigned i;
 
58
        for (i = 4; i > 0; i--) {
 
59
                r |= ((v & b[i]) != 0) << i;
 
60
        }
 
61
        }
 
62
#endif
 
63
        return r;
 
64
}
 
65
 
 
66
/* nearest power of 2
 
67
 */
 
68
 
 
69
static inline size_t pgm_nearest_power (size_t, size_t) PGM_GNUC_CONST;
 
70
 
 
71
static inline
 
72
size_t
 
73
pgm_nearest_power (
 
74
        size_t          b,
 
75
        size_t          v
 
76
        )
 
77
{
 
78
        if (v > (SIZE_MAX/2))
 
79
                return SIZE_MAX;
 
80
        while (b < v)
 
81
                b <<= 1;
 
82
        return b;
 
83
}
 
84
 
 
85
unsigned pgm_spaced_primes_closest (unsigned) PGM_GNUC_PURE;
 
86
 
 
87
PGM_END_DECLS
 
88
 
 
89
#endif /* __PGM_IMPL_MATH_H__ */