zpoint/CPython-Internals

little endian and big endian

MambaWong opened this issue · 1 comments

@zpoint
您好,您在 long.md 中有这样一段描述

notice, because the digit is the smallest unit in the CPython abstract level, order between bytes inside a single ob_digit are represent in most-important-bit-in-the-left-most order(big-endian order)
order between digit in the ob_digit array are represent in most-important-digit-in-the-right-most order(little endian order)

请问您的测试CPU是大端还是小端?
因为在我的PC(小端)上 Debug 看出来的结果与您第一条结论是相反的,是按小端模式排放。而且 PyLong_FromLong(long ival) 函数中的定义:

        digit *p = v->ob_digit;
        Py_SIZE(v) = ndigits*sign;
        t = abs_ival;
        while (t) {
            *p++ = Py_SAFE_DOWNCAST(
                t & PyLong_MASK, unsigned long, digit);
            t >>= PyLong_SHIFT;
        }

根据上面的定义,可以看出:

  1. ob_digit[i] 中字节的排放方式应该是跟CPU的大小端一致的。那么 order between bytes inside a single ob_digit are represent in most-important-bit-in-the-left-most order(big-endian order) 这里的描述应该改为与CPU的一致。
  2. ob_digit[i] 之间是按小端模式排放,与设备无关。即 order between digit in the ob_digit array are represent in most-important-digit-in-the-right-most order(little endian order)

@MambaWong 感谢指正, 已修改, Thanks♪(・ω・)ノ