balign.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @file balign.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Integer alignment macros.
  25. */
  26. #ifndef BADVPN_MISC_BALIGN_H
  27. #define BADVPN_MISC_BALIGN_H
  28. #include <stdlib.h>
  29. /**
  30. * Aligns the integer x up to n bytes.
  31. */
  32. #define BALIGN_UP_N(x,n) \
  33. ({\
  34. typeof (x) _x = (x);\
  35. typeof (n) _n = (n);\
  36. typeof (x) _r = _x % _n;\
  37. _r ? _x + (_n - _r) : _x;\
  38. })
  39. /**
  40. * Aligns the integer x down to n bytes.
  41. */
  42. #define BALIGN_DOWN_N(x,n) \
  43. ({\
  44. typeof (x) _x = (x);\
  45. typeof (n) _n = (n);\
  46. _x - (_x % _n);\
  47. })
  48. /**
  49. * Calculates the quotient of integers a and b, rounded up.
  50. */
  51. #define BDIVIDE_UP(a,b) \
  52. ({\
  53. typeof (a) _a = (a);\
  54. typeof (b) _b = (b);\
  55. typeof (a) _r = _a % _b;\
  56. _r > 0 ? _a / _b + 1 : _a / _b;\
  57. })
  58. #endif