关键词搜索

源码搜索 ×
×

漫话Redis源码之三十四

发布2021-12-12浏览652次

详情内容

这种代码,基本很难看,及时搞懂了细节,也没啥用。针对这种函数,最关键点是知道他的功能,而不是细节。

  1. #include <stdint.h>
  2. #define N 16
  3. #define MASK ((1 << (N - 1)) + (1 << (N - 1)) - 1)
  4. #define LOW(x) ((unsigned)(x) & MASK)
  5. #define HIGH(x) LOW((x) >> N)
  6. #define MUL(x, y, z) { int32_t l = (long)(x) * (long)(y); \
  7. (z)[0] = LOW(l); (z)[1] = HIGH(l); }
  8. #define CARRY(x, y) ((int32_t)(x) + (long)(y) > MASK)
  9. #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
  10. #define X0 0x330E
  11. #define X1 0xABCD
  12. #define X2 0x1234
  13. #define A0 0xE66D
  14. #define A1 0xDEEC
  15. #define A2 0x5
  16. #define C 0xB
  17. #define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
  18. #define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
  19. #define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
  20. #define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
  21. return (v);
  22. #define HI_BIT (1L << (2 * N - 1))
  23. static uint32_t x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
  24. static void next(void);
  25. int32_t redisLrand48() {
  26. next();
  27. return (((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
  28. }
  29. void redisSrand48(int32_t seedval) {
  30. SEED(X0, LOW(seedval), HIGH(seedval));
  31. }
  32. static void next(void) {
  33. uint32_t p[2], q[2], r[2], carry0, carry1;
  34. MUL(a[0], x[0], p);
  35. ADDEQU(p[0], c, carry0);
  36. ADDEQU(p[1], carry0, carry1);
  37. MUL(a[0], x[1], q);
  38. ADDEQU(p[1], q[0], carry0);
  39. MUL(a[1], x[0], r);
  40. x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
  41. a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
  42. x[1] = LOW(p[1] + r[0]);
  43. x[0] = LOW(p[0]);
  44. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载