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