bithelp.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* bithelp.h - Some bit manipulation helpers
  2. * Copyright (C) 1999, 2002 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser general Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  19. */
  20. #ifndef G10_BITHELP_H
  21. #define G10_BITHELP_H
  22. /****************
  23. * Rotate the 32 bit unsigned integer X by N bits left/right
  24. */
  25. #if defined(__GNUC__) && defined(__i386__)
  26. static inline u32
  27. rol( u32 x, int n)
  28. {
  29. __asm__("roll %%cl,%0"
  30. :"=r" (x)
  31. :"0" (x),"c" (n));
  32. return x;
  33. }
  34. #else
  35. #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
  36. #endif
  37. #if defined(__GNUC__) && defined(__i386__)
  38. static inline u32
  39. ror(u32 x, int n)
  40. {
  41. __asm__("rorl %%cl,%0"
  42. :"=r" (x)
  43. :"0" (x),"c" (n));
  44. return x;
  45. }
  46. #else
  47. #define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
  48. #endif
  49. #endif /*G10_BITHELP_H*/