[strlen的GNU实现]有趣的代码

本文介绍了一种优化后的strlen函数实现方式,通过快速扫描四个字节来查找字符串终止符,提高字符串长度计算效率。
先搁这吧,晚上回去分析。
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #undef strlen
  4. /*
  5. Return the length of the null-terminated string STR.  Scan for
  6. the null terminator quickly by testing four bytes at a time.  
  7. */
  8. size_t strlen (str)
  9. const char *str;
  10. {
  11.     const char *char_ptr;
  12.     const unsigned long int *longword_ptr;
  13.     unsigned long int longword, magic_bits, himagic, lomagic;
  14.     /*
  15.     Handle the first few characters by reading one character at a time.
  16.     Do this until CHAR_PTR is aligned on a longword boundary.  
  17.     */
  18.     for (char_ptr = str; ((unsigned long int) char_ptr
  19.             & (sizeof (longword) - 1)) != 0;++char_ptr)
  20.     if (*char_ptr == '/0')
  21.             return char_ptr - str;
  22.     /*
  23.     All these elucidatory comments refer to 4-byte longwords,
  24.     but the theory applies equally well to 8-byte longwords.  
  25.     */
  26.     longword_ptr = (unsigned long int *) char_ptr;
  27.     /*
  28.     Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
  29.     the "holes."  Note that there is a hole just to the left of
  30.     each byte, with an extra at the end:
  31.     bits:  01111110 11111110 11111110 11111111
  32.     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
  33.     The 1-bits make sure that carries propagate to the next 0-bit.
  34.     The 0-bits provide holes for carries to fall into.  
  35.     */
  36.     magic_bits = 0x7efefeffL;
  37.     himagic = 0x80808080L;
  38.     lomagic = 0x01010101L;
  39.     if (sizeof (longword) > 4)
  40.     {
  41.             /* 64-bit version of the magic.  */
  42.             /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
  43.             magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
  44.             himagic = ((himagic << 16) << 16) | himagic;
  45.             lomagic = ((lomagic << 16) << 16) | lomagic;
  46.     }
  47.     if (sizeof (longword) > 8)
  48.             abort ();
  49.     /*
  50.     Instead of the traditional loop which tests each character,
  51.     we will test a longword at a time.  The tricky part is testing
  52.     if *any of the four* bytes in the longword in question are zero.  
  53.     */
  54.     for (;;)
  55.     {
  56.         /* We tentatively exit the loop if adding MAGIC_BITS to
  57.         LONGWORD fails to change any of the hole bits of LONGWORD.
  58.         1) Is this safe?  Will it catch all the zero bytes?
  59.         Suppose there is a byte with all zeros.  Any carry bits
  60.         propagating from its left will fall into the hole at its
  61.         least significant bit and stop.  Since there will be no
  62.         carry from its most significant bit, the LSB of the
  63.         byte to the left will be unchanged, and the zero will be
  64.         detected.
  65.         2) Is this worthwhile?  Will it ignore everything except
  66.         zero bytes?  Suppose every byte of LONGWORD has a bit set
  67.         somewhere.  There will be a carry into bit 8.  If bit 8
  68.         is set, this will carry into bit 16.  If bit 8 is clear,
  69.         one of bits 9-15 must be set, so there will be a carry
  70.         into bit 16.  Similarly, there will be a carry into bit
  71.         24.  If one of bits 24-30 is set, there will be a carry
  72.         into bit 31, so all of the hole bits will be changed.
  73.         The one misfire occurs when bits 24-30 are clear and bit
  74.         31 is set; in this case, the hole at bit 31 is not
  75.         changed.  If we had access to the processor carry flag,
  76.         we could close this loophole by putting the fourth hole
  77.         at bit 32!
  78.         So it ignores everything except 128's, when they're aligned
  79.         properly.  */
  80.         longword = *longword_ptr++;
  81.         if (
  82.         #if 0
  83.         /* Add MAGIC_BITS to LONGWORD.  */
  84.         (((longword + magic_bits)
  85.         /* Set those bits that were unchanged by the addition.  */
  86.         ^ ~longword)
  87.         /* Look at only the hole bits.  If any of the hole bits
  88.         are unchanged, most likely one of the bytes was a
  89.         zero.  */
  90.         & ~magic_bits)
  91.         #else
  92.         ((longword - lomagic) & himagic)
  93.         #endif
  94.         != 0)
  95.         {
  96.             /*
  97.             Which of the bytes was the zero?  If none of them were, it was
  98.             a misfire; continue the search.  
  99.             */
  100.             const char *cp = (const char *) (longword_ptr - 1);
  101.             if (cp[0] == 0)
  102.             return cp - str;
  103.             if (cp[1] == 0)
  104.             return cp - str + 1;
  105.             if (cp[2] == 0)
  106.             return cp - str + 2;
  107.             if (cp[3] == 0)
  108.             return cp - str + 3;
  109.             if (sizeof (longword) > 4)
  110.             {
  111.                   if (cp[4] == 0)
  112.                 return cp - str + 4;
  113.                   if (cp[5] == 0)
  114.                 return cp - str + 5;
  115.                   if (cp[6] == 0)
  116.                 return cp - str + 6;
  117.                   if (cp[7] == 0)
  118.                 return cp - str + 7;
  119.             }
  120.         }
  121.     }
  122. }
  123. libc_hidden_builtin_def (strlen)

【电动车】基于多目标优化遗传算法NSGAII的峰谷分时电价引导下的电动汽车充电负荷优化研究(Matlab代码实现)内容概要:本文围绕“基于多目标优化遗传算法NSGA-II的峰谷分时电价引导下的电动汽车充电负荷优化研究”展开,利用Matlab代码实现优化模型,旨在通过峰谷分时电价机制引导电动汽车有序充电,降低电网负荷波动,提升能源利用效率。研究融合了多目标优化思想与遗传算法NSGA-II,兼顾电网负荷均衡性、用户充电成本和充电满意度等多个目标,构建了科学合理的数学模型,并通过仿真验证了方法的有效性与实用性。文中还提供了完整的Matlab代码实现路径,便于复现与进一步研究。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事智能电网、电动汽车调度相关工作的工程技术人员。; 使用场景及目标:①应用于智能电网中电动汽车充电负荷的优化调度;②服务于峰谷电价政策下的需求侧管理研究;③为多目标优化算法在能源系统中的实际应用提供案例参考; 阅读建议:建议读者结合Matlab代码逐步理解模型构建与算法实现过程,重点关注NSGA-II算法在多目标优化中的适应度函数设计、约束处理及Pareto前沿生成机制,同时可尝试调整参数或引入其他智能算法进行对比分析,以深化对优化策略的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值