Robert Elz via austin-group-l at The Open Group dixit:

>And of course, that means that even though the >> operator is in Table 1-2
>as one that must be supported, it cannot actually work, as >> is unspecified
>(or even undefined, I forget) on signed values, and POSIX sh arithmetic only

No, only for negative signed values and in some other cases,
as Gabriel Ravier mentioned.

>of signed.   I suspect other shells might do the same.

As for mksh, I got so fed up with UB that I calculate all
operations in unsigned. The “strict POSIX” one (usually
shipped as lksh (built with the Build.sh -L flag), usually
built with -DMKSH_BINSHPOSIX and symlinkable to /bin/sh)
uses the long data type for that, the “proper mksh” one
(usually shipped as mksh) has guaranteed 32-bit arithmetics,
guaranteed 2s complement (though POSIX guarantees that for
the C signed long as well, thankfully), and the shell has
some extra operations (e.g. rotate) there. I plan on adding
a bigint mechanism eventually, to make up for the fact that
it’s limited to 32 bits normally (relying on “long” which
has diverging sizes, making $((1<<31+1)) UB on ILP32, is too
unsafe in my eyes, but POSIX demands it so a (currently)
separate binary does it).

In most cases, I do the operations as unsigned; this works
well for addition, subtraction, even multiplication if 2s
complement and wraparound can be assumed, for division and
modulo I do them by hand on the magnitudes then deal with
the signs later so it’s actually defined for negative values,
etc.

It’s still a work in progress, not yet perfect, but I’ve
extracted the workings into macros, with a testsuite. If
things work out, the use of long can be made a runtime,
not compile-time, decision eventually, too.

mksh also has an “unsigned arithmetics” extension: if the
$(( or ksh-style (( is immediately followed by # the expression
is evaluated in unsigned (using the 2s complement representation
of the variables used). This is major useful for hashes etc.

bye,
//mirabilos
-- 
08:05⎜<XTaran:#grml> mika: Does grml have an tool to read Apple
     ⎜    System Log (asl) files? :)
08:08⎜<ft:#grml> yeah. /bin/rm. ;)       08:09⎜<mrud:#grml> hexdump -C
08:31⎜<XTaran:#grml> ft, mrud: *g*

  • out-of-bounds numbers... Thorsten Glaser via austin-group-l at The Open Group
    • Re: out-of-bound... Geoff Clare via austin-group-l at The Open Group
      • Re: out-of-b... Thorsten Glaser via austin-group-l at The Open Group
        • Re: out-... Geoff Clare via austin-group-l at The Open Group
        • Re: out-... Robert Elz via austin-group-l at The Open Group
          • Re: ... Gabriel Ravier via austin-group-l at The Open Group
          • Re: ... Thorsten Glaser via austin-group-l at The Open Group

Reply via email to