Commit | Line | Data |
---|---|---|

f9d42b20 RK |
1 | /* |

2 | * This file is part of DisOrder | |

3 | * Copyright (C) 2008 Richard Kettlewell | |

4 | * | |

5 | * This program is free software; you can redistribute it and/or modify | |

6 | * it under the terms of the GNU General Public License as published by | |

7 | * the Free Software Foundation; either version 2 of the License, or | |

8 | * (at your option) any later version. | |

9 | * | |

10 | * This program is distributed in the hope that it will be useful, but | |

11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |

12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |

13 | * General Public License for more details. | |

14 | * | |

15 | * You should have received a copy of the GNU General Public License | |

16 | * along with this program; if not, write to the Free Software | |

17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |

18 | * USA | |

19 | */ | |

20 | ||

21 | /** @file lib/bits.c | |

22 | * @brief Bit operations | |

23 | */ | |

24 | ||

05b75f8d | 25 | #include "common.h" |

f9d42b20 RK |
26 | |

27 | #include <math.h> | |

28 | ||

29 | #include "bits.h" | |

30 | ||

7c8c2fc9 | 31 | #if !HAVE_FLS |

f9d42b20 RK |
32 | /** @brief Compute index of leftmost 1 bit |

33 | * @param n Integer | |

34 | * @return Index of leftmost 1 bit or -1 | |

35 | * | |

36 | * For positive @p n we return the index of the leftmost bit of @p n. For | |

37 | * instance @c leftmost_bit(1) returns 0, @c leftmost_bit(15) returns 3, etc. | |

38 | * | |

39 | * If @p n is zero then -1 is returned. | |

40 | */ | |

41 | int leftmost_bit(uint32_t n) { | |

42 | /* See e.g. Hacker's Delight s5-3 (p81) for where the idea comes from. | |

43 | * Warren is computing the number of leading zeroes, but that's not quite | |

44 | * what I wanted. Also this version should be more portable than his, which | |

45 | * inspects the bytes of the floating point number directly. | |

46 | */ | |

47 | int x; | |

48 | frexp((double)n, &x); | |

49 | /* This gives: n = m * 2^x, where 0.5 <= m < 1 and x is an integer. | |

50 | * | |

51 | * If we take log2 of either side then we have: | |

52 | * log2(n) = x + log2 m | |

53 | * | |

54 | * We know that 0.5 <= m < 1 => -1 <= log2 m < 0. So we floor either side: | |

55 | * | |

56 | * floor(log2(n)) = x - 1 | |

57 | * | |

58 | * What is floor(log2(n))? Well, consider that: | |

59 | * | |

60 | * 2^k <= z < 2^(k+1) => floor(log2(z)) = k. | |

61 | * | |

62 | * But 2^k <= z < 2^(k+1) is the same as saying that the leftmost bit of z is | |

63 | * bit k. | |

64 | * | |

65 | * | |

66 | * Warren adds 0.5 first, to deal with the case when n=0. However frexp() | |

67 | * guarantees to return x=0 when n=0, so we get the right answer without that | |

68 | * step. | |

69 | */ | |

70 | return x - 1; | |

71 | } | |

7c8c2fc9 | 72 | #endif |

f9d42b20 RK |
73 | |

74 | /* | |

75 | Local Variables: | |

76 | c-basic-offset:2 | |

77 | comment-column:40 | |

78 | fill-column:79 | |

79 | indent-tabs-mode:nil | |

80 | End: | |

81 | */ |