util.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*Copyright (C) 2008-2009 Timothy B. Terriberry (tterribe@xiph.org)
  2. You can redistribute this library and/or modify it under the terms of the
  3. GNU Lesser General Public License as published by the Free Software
  4. Foundation; either version 2.1 of the License, or (at your option) any later
  5. version.*/
  6. #if !defined(_qrcode_util_H)
  7. # define _qrcode_util_H (1)
  8. #include "stdio.h"
  9. #include "stdlib.h"
  10. #include "string.h"
  11. #include "bsp_common.h"
  12. #define QR_MAXI(_a,_b) ((_a)-((_a)-(_b)&-((_b)>(_a))))
  13. #define QR_MINI(_a,_b) ((_a)+((_b)-(_a)&-((_b)<(_a))))
  14. #define QR_SIGNI(_x) (((_x)>0)-((_x)<0))
  15. #define QR_SIGNMASK(_x) (-((_x)<0))
  16. /*Unlike copysign(), simply inverts the sign of _a if _b is negative.*/
  17. #define QR_FLIPSIGNI(_a,_b) ((_a)+QR_SIGNMASK(_b)^QR_SIGNMASK(_b))
  18. #define QR_COPYSIGNI(_a,_b) QR_FLIPSIGNI(abs(_a),_b)
  19. /*Divides a signed integer by a positive value with exact rounding.*/
  20. #define QR_DIVROUND(_x,_y) (((_x)+QR_FLIPSIGNI(_y>>1,_x))/(_y))
  21. #define QR_CLAMPI(_a,_b,_c) (QR_MAXI(_a,QR_MINI(_b,_c)))
  22. #define QR_CLAMP255(_x) ((unsigned char)((((_x)<0)-1)&((_x)|-((_x)>255))))
  23. /*Swaps two integers _a and _b if _a>_b.*/
  24. #define QR_SORT2I(_a,_b) \
  25. do{ \
  26. int t__; \
  27. t__=QR_MINI(_a,_b)^(_a); \
  28. (_a)^=t__; \
  29. (_b)^=t__; \
  30. } \
  31. while(0)
  32. #define QR_ILOG0(_v) (!!((_v)&0x2))
  33. #define QR_ILOG1(_v) (((_v)&0xC)?2+QR_ILOG0((_v)>>2):QR_ILOG0(_v))
  34. #define QR_ILOG2(_v) (((_v)&0xF0)?4+QR_ILOG1((_v)>>4):QR_ILOG1(_v))
  35. #define QR_ILOG3(_v) (((_v)&0xFF00)?8+QR_ILOG2((_v)>>8):QR_ILOG2(_v))
  36. #define QR_ILOG4(_v) (((_v)&0xFFFF0000)?16+QR_ILOG3((_v)>>16):QR_ILOG3(_v))
  37. /*Computes the integer logarithm of a (positive, 32-bit) constant.*/
  38. #define QR_ILOG(_v) ((int)QR_ILOG4((unsigned)(_v)))
  39. /*Multiplies 32-bit numbers _a and _b, adds (possibly 64-bit) number _r, and
  40. takes bits [_s,_s+31] of the result.*/
  41. #define QR_FIXMUL(_a,_b,_r,_s) ((int)((_a)*(long long)(_b)+(_r)>>(_s)))
  42. /*Multiplies 32-bit numbers _a and _b, adds (possibly 64-bit) number _r, and
  43. gives all 64 bits of the result.*/
  44. #define QR_EXTMUL(_a,_b,_r) ((_a)*(long long)(_b)+(_r))
  45. unsigned qr_isqrt(unsigned _val);
  46. unsigned qr_ihypot(int _x,int _y);
  47. int qr_ilog(unsigned _val);
  48. #endif