]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
release: 1.5.29
[lilypond.git] / lily / misc.cc
1 /*
2   misc.cc -- implement various stuff
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <math.h>
11 #include "misc.hh"
12
13 /*
14   Return the 2-log, rounded down 
15  */
16 int
17 intlog2 (int d)
18 {
19   assert (d);
20   int i=0;
21   while ((d != 1)) 
22     {
23       d/= 2;
24       i++;
25     }
26   
27   assert (! (d/2));
28   return i;
29 }
30
31 double
32 log_2 (double x)
33 {
34   return log (x)  /log (2.0);
35 }
36
37
38 static int
39 comp (Real const &a, Real const &b)
40 {
41   return sign (a-b);
42 }
43
44 Interval
45 quantise_iv (Array<Real> positions, Real x)
46 {
47   positions.sort (comp);
48   Real period = positions.top () - positions[0];
49   
50   int n =  int ((x - positions[0]) / period);
51   Real frac = (x - positions[0]) -  n * period;
52
53   while (frac < 0)
54     {
55       frac += period;
56       n --;
57     }
58   
59   Real px = frac + positions[0];
60   assert (positions[0] <= px && px <= positions.top ());
61   int i=0;
62   for (; i < positions.size () - 1; i++)
63     {
64       if (positions[i] <= px && px <= positions[i+1])
65         break; 
66     }
67
68   return Interval (positions[i] , positions[i+1]) + period * n;
69 }