]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
release: 1.3.48
[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--2000 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 int
14 intlog2(int d)
15 {
16   assert (d);
17   int i=0;
18   while (!(d&1)) 
19     {
20         d/= 2;
21         i++;
22     }
23   assert (!(d/2));
24   return i;
25 }
26
27 double
28 log_2(double x)
29 {
30   return log (x)  /log (2.0);
31 }
32
33
34 static int
35 comp (Real const &a, Real const &b)
36 {
37   return sign (a-b);
38 }
39
40 Interval
41 quantise_iv (Array<Real> positions, Real x)
42 {
43   positions.sort (comp);
44   Real period = positions.top () - positions[0];
45   
46   int n =  int ((x - positions[0]) / period);
47   Real frac = (x - positions[0] ) -  n * period;
48
49   while (frac < 0)
50     {
51       frac += period;
52       n --;
53     }
54   
55   Real px = frac + positions[0];
56   assert ( positions[0] <= px && px <= positions.top ());
57   int i=0;
58   for (; i < positions.size () - 1; i++)
59     {
60       if (positions[i] <= px && px <= positions[i+1])
61         break; 
62     }
63
64   return Interval (positions[i] , positions[i+1]) + period * n;
65 }