]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
Merge branch 'master' of ssh+git://gpercival@git.sv.gnu.org/srv/git/lilypond
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10
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 Real
38 directed_round (Real f, Direction d)
39 {
40   if (d < 0)
41     return floor (f);
42   else
43     return ceil (f);
44 }
45
46
47
48 /*
49    0 at threshold,  1 at 0, with 1/x falloff.
50  */
51 Real
52 peak_around (Real epsilon,  Real threshold, Real x)
53 {
54   if (x < 0)
55     return 1.0;
56   return max (- epsilon * (x - threshold) / ((x + epsilon)  * threshold), 0.0);
57 }
58
59
60 string
61 camel_case_to_lisp_identifier (string in)
62 {
63   vector<char> out;
64   
65   /* don't add '-' before first character */
66   out.push_back (tolower (in[0]));
67     
68   for (size_t inpos = 1; inpos < in.size (); inpos++)
69     {
70       if (isupper (in[inpos]))
71         out.push_back ('-');
72       out.push_back (tolower (in[inpos]));
73     }
74   
75   string result (&out[0], out.size ());
76   replace_all (result, '_', '-');
77
78   return result;
79 }
80