## iof-bird-daemon / lib / bitops.c @ 62e64905

History | View | Annotate | Download (1.67 KB)

1 | 18c8241a | Martin Mares | ```
/*
``` |
---|---|---|---|

2 | ```
* BIRD Library -- Generic Bit Operations
``` |
||

3 | ```
*
``` |
||

4 | ```
* (c) 1998 Martin Mares <mj@ucw.cz>
``` |
||

5 | ```
*
``` |
||

6 | ```
* Can be freely distributed and used under the terms of the GNU GPL.
``` |
||

7 | ```
*/
``` |
||

8 | |||

9 | #include "nest/bird.h" |
||

10 | #include "bitops.h" |
||

11 | |||

12 | 7722938d | Martin Mares | ```
/**
``` |

13 | ```
* u32_mkmask - create a bit mask
``` |
||

14 | ```
* @n: number of bits
``` |
||

15 | ```
*
``` |
||

16 | ```
* u32_mkmask() returns an unsigned 32-bit integer which binary
``` |
||

17 | ```
* representation consists of @n ones followed by zeroes.
``` |
||

18 | ```
*/
``` |
||

19 | 18c8241a | Martin Mares | u32 |

20 | ae80a2de | Pavel TvrdĂk | u32_mkmask(uint n) |

21 | 18c8241a | Martin Mares | { |

22 | return n ? ~((1 << (32 - n)) - 1) : 0; |
||

23 | } |
||

24 | |||

25 | 7722938d | Martin Mares | ```
/**
``` |

26 | ```
* u32_masklen - calculate length of a bit mask
``` |
||

27 | ```
* @x: bit mask
``` |
||

28 | ```
*
``` |
||

29 | ```
* This function checks whether the given integer @x represents
``` |
||

30 | ```
* a valid bit mask (binary representation contains first ones, then
``` |
||

31 | fe9f1a6d | Ondrej Zajicek (work) | ```
* zeroes) and returns the number of ones or 255 if the mask is invalid.
``` |

32 | 7722938d | Martin Mares | ```
*/
``` |

33 | fe9f1a6d | Ondrej Zajicek (work) | uint |

34 | 18c8241a | Martin Mares | u32_masklen(u32 x) |

35 | { |
||

36 | int l = 0; |
||

37 | u32 n = ~x; |
||

38 | |||

39 | fe9f1a6d | Ondrej Zajicek (work) | if (n & (n+1)) return 255; |

40 | 18c8241a | Martin Mares | if (x & 0x0000ffff) { x &= 0x0000ffff; l += 16; } |

41 | if (x & 0x00ff00ff) { x &= 0x00ff00ff; l += 8; } |
||

42 | if (x & 0x0f0f0f0f) { x &= 0x0f0f0f0f; l += 4; } |
||

43 | if (x & 0x33333333) { x &= 0x33333333; l += 2; } |
||

44 | if (x & 0x55555555) l++; |
||

45 | if (x & 0xaaaaaaaa) l++; |
||

46 | ```
return l;
``` |
||

47 | } |
||

48 | b1a597e0 | Ondrej Zajicek | |

49 | ```
/**
``` |
||

50 | ```
* u32_log2 - compute a binary logarithm.
``` |
||

51 | ```
* @v: number
``` |
||

52 | ```
*
``` |
||

53 | ```
* This function computes a integral part of binary logarithm of given
``` |
||

54 | ```
* integer @v and returns it. The computed value is also an index of the
``` |
||

55 | c60cdd8c | Ondrej Zajicek | ```
* most significant non-zero bit position.
``` |

56 | b1a597e0 | Ondrej Zajicek | ```
*/
``` |

57 | |||

58 | u32 |
||

59 | u32_log2(u32 v) |
||

60 | { |
||

61 | c60cdd8c | Ondrej Zajicek | ```
/* The code from http://www-graphics.stanford.edu/~seander/bithacks.html */
``` |

62 | b1a597e0 | Ondrej Zajicek | u32 r, shift; |

63 | r = (v > 0xFFFF) << 4; v >>= r; |
||

64 | shift = (v > 0xFF ) << 3; v >>= shift; r |= shift; |
||

65 | shift = (v > 0xF ) << 2; v >>= shift; r |= shift; |
||

66 | shift = (v > 0x3 ) << 1; v >>= shift; r |= shift; |
||

67 | ```
r |= (v >> 1);
``` |
||

68 | ```
return r;
``` |
||

69 | } |