Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Chemist who falls in acid is absorbed in work.


devel / comp.arch / Encoding saturating arithmetic

SubjectAuthor
* Encoding saturating arithmeticThomas Koenig
+* Re: Encoding saturating arithmeticTimothy McCaffrey
|+* Re: Encoding saturating arithmeticMitchAlsup
||+* Re: Encoding saturating arithmeticStephen Fuld
|||+- Re: Encoding saturating arithmeticScott Lurndal
|||+* Re: Encoding saturating arithmeticMitchAlsup
||||`* Re: Encoding saturating arithmeticDavid Brown
|||| `* Re: Encoding saturating arithmeticGeorge Neuner
||||  +* Re: Encoding saturating arithmeticMitchAlsup
||||  |`* Re: Encoding saturating arithmeticMichael S
||||  | `* Re: Encoding saturating arithmeticThomas Koenig
||||  |  `* Re: Encoding saturating arithmeticMichael S
||||  |   `- Re: Encoding saturating arithmeticThomas Koenig
||||  `* Re: Encoding saturating arithmeticDavid Brown
||||   `- Re: Encoding saturating arithmeticGeorge Neuner
|||+* Re: Encoding saturating arithmeticIvan Godard
||||+* Re: Encoding saturating arithmeticMitchAlsup
|||||+* Re: Encoding saturating arithmeticIvan Godard
||||||+* Re: Encoding saturating arithmeticMitchAlsup
|||||||`* Re: Encoding saturating arithmeticPaul A. Clayton
||||||| `* Re: Encoding saturating arithmeticMitchAlsup
|||||||  `- Re: Encoding saturating arithmeticPaul A. Clayton
||||||`* Re: Encoding saturating arithmeticMitchAlsup
|||||| +- Re: Encoding saturating arithmeticIvan Godard
|||||| `- Re: Encoding saturating arithmeticBGB
|||||+- Re: Encoding saturating arithmeticTerje Mathisen
|||||+- Re: Encoding saturating arithmeticEricP
|||||`- Re: Encoding saturating arithmeticPaul A. Clayton
||||`- Re: Encoding saturating arithmeticMichael S
|||`* Re: Encoding saturating arithmeticJimBrakefield
||| `* Re: Encoding saturating arithmeticluke.l...@gmail.com
|||  `* Re: Encoding saturating arithmeticMitchAlsup
|||   `- Re: Encoding saturating arithmeticJimBrakefield
||`* Re: Encoding saturating arithmeticTerje Mathisen
|| `* Re: Encoding saturating arithmeticBGB
||  `* Re: Encoding saturating arithmeticMitchAlsup
||   `- Re: Encoding saturating arithmeticBGB
|`* Re: Encoding saturating arithmeticThomas Koenig
| +* Re: Encoding saturating arithmeticScott Lurndal
| |`- Re: Encoding saturating arithmeticThomas Koenig
| `- Re: Encoding saturating arithmeticTerje Mathisen
+* Re: Encoding saturating arithmeticDavid Brown
|+* Re: Encoding saturating arithmeticMitchAlsup
||`- Re: Encoding saturating arithmeticNiklas Holsti
|`* Re: Encoding saturating arithmeticNiklas Holsti
| +* Re: Encoding saturating arithmeticDavid Brown
| |+* Re: Encoding saturating arithmeticNiklas Holsti
| ||+- Re: Encoding saturating arithmeticAnton Ertl
| ||`* Re: Encoding saturating arithmeticDavid Brown
| || +- Re: Encoding saturating arithmeticMitchAlsup
| || `* Re: Encoding saturating arithmeticJosh Vanderhoof
| ||  +- Re: Encoding saturating arithmeticDavid Brown
| ||  `- Re: Encoding saturating arithmeticTim Rentsch
| |+* Re: Encoding saturating arithmeticAnton Ertl
| ||+* Re: Encoding saturating arithmeticThomas Koenig
| |||+* Re: Encoding saturating arithmeticMitchAlsup
| ||||+- Re: Encoding saturating arithmeticThomas Koenig
| ||||+* Re: Encoding saturating arithmeticThomas Koenig
| |||||`- Re: Encoding saturating arithmeticMitchAlsup
| ||||`* Re: Encoding saturating arithmeticDavid Brown
| |||| `* Re: Encoding saturating arithmeticScott Lurndal
| ||||  `* Re: Encoding saturating arithmeticMitchAlsup
| ||||   `- Re: Encoding saturating arithmeticThomas Koenig
| |||+* Re: Encoding saturating arithmeticMitchAlsup
| ||||`* Re: Encoding saturating arithmeticDavid Brown
| |||| `* Re: Encoding saturating arithmeticThomas Koenig
| ||||  `- Re: Encoding saturating arithmeticTim Rentsch
| |||`* Re: Encoding saturating arithmeticAnton Ertl
| ||| +* Re: Encoding saturating arithmeticThomas Koenig
| ||| |`- Re: Encoding saturating arithmeticDavid Brown
| ||| `- Re: Encoding saturating arithmeticBGB
| ||+- Re: Encoding saturating arithmeticEricP
| ||`- Re: Encoding saturating arithmeticDavid Brown
| |`- Re: Encoding saturating arithmeticEricP
| `* Re: Encoding saturating arithmeticEricP
|  +* Re: Encoding saturating arithmeticNiklas Holsti
|  |`* Re: Encoding saturating arithmeticEricP
|  | `* Re: Encoding saturating arithmeticMitchAlsup
|  |  `* Re: Encoding saturating arithmeticEricP
|  |   `- Re: Encoding saturating arithmeticScott Lurndal
|  `* Re: Encoding saturating arithmeticAnton Ertl
|   +* Re: Encoding saturating arithmeticEricP
|   |`* Re: Encoding saturating arithmeticAnton Ertl
|   | `* Re: Encoding saturating arithmeticEricP
|   |  +* Re: Encoding saturating arithmeticMitchAlsup
|   |  |`* Re: Encoding saturating arithmeticEricP
|   |  | `* Re: Encoding saturating arithmeticMitchAlsup
|   |  |  +- Re: Encoding saturating arithmeticBGB
|   |  |  +- Re: Encoding saturating arithmeticPaul A. Clayton
|   |  |  `* Re: Encoding saturating arithmeticluke.l...@gmail.com
|   |  |   `* Re: Encoding saturating arithmeticMitchAlsup
|   |  |    +* Re: Encoding saturating arithmeticluke.l...@gmail.com
|   |  |    |`- Re: Encoding saturating arithmeticMitchAlsup
|   |  |    `- Re: Encoding saturating arithmeticMarcus
|   |  `- Re: Encoding saturating arithmeticBernd Linsel
|   `* Re: Encoding saturating arithmeticTerje Mathisen
|    `- Re: Encoding saturating arithmeticAnton Ertl
`* Re: Encoding saturating arithmeticMarcus
 `* Re: Encoding saturating arithmeticluke.l...@gmail.com
  +* Re: Encoding saturating arithmeticBGB
  |+* Re: Encoding saturating arithmeticMitchAlsup
  |`* Re: Encoding saturating arithmeticluke.l...@gmail.com
  `* Re: Encoding saturating arithmeticMarcus

Pages:123456
Encoding saturating arithmetic

<tl4k53$3f0sn$1@newsreader4.netcologne.de>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31821&group=comp.arch#31821

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!.POSTED.2a0a-a540-201b-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de!not-for-mail
From: tkoenig@netcologne.de (Thomas Koenig)
Newsgroups: comp.arch
Subject: Encoding saturating arithmetic
Date: Thu, 17 Nov 2022 06:25:39 -0000 (UTC)
Organization: news.netcologne.de
Distribution: world
Message-ID: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
Injection-Date: Thu, 17 Nov 2022 06:25:39 -0000 (UTC)
Injection-Info: newsreader4.netcologne.de; posting-host="2a0a-a540-201b-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de:2a0a:a540:201b:0:7285:c2ff:fe6c:992d";
logging-data="3638167"; mail-complaints-to="abuse@netcologne.de"
User-Agent: slrn/1.0.3 (Linux)
 by: Thomas Koenig - Thu, 17 Nov 2022 06:25 UTC

Seems like saturating arithmetic is a thing, apparently with
integers. I can see that for applications that interface with
the real world with limited-range sensors or actuators (no sense
in trying to put more than 20 mA on a 4-20 mA current loop).

And floating point (usually?) does not need it.

What's the best way of encoding this in an ISA? To cover the
range of 8,16,32,64 bit calculations would need two bits. Or,
if somebody has, let's say, a 12-bit sensor, intermediate lengths
might also come in handy to clip results to between -2048..2047.

Or is it just not worth the bother? Maybe people who have
experience with embedded CPUs can say something about this.

Re: Encoding saturating arithmetic

<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31822&group=comp.arch#31822

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:620a:10a3:b0:6fa:156e:44c0 with SMTP id h3-20020a05620a10a300b006fa156e44c0mr2107673qkk.293.1668699381192;
Thu, 17 Nov 2022 07:36:21 -0800 (PST)
X-Received: by 2002:a05:6830:18d3:b0:66c:dd29:813d with SMTP id
v19-20020a05683018d300b0066cdd29813dmr1690202ote.312.1668699380796; Thu, 17
Nov 2022 07:36:20 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Thu, 17 Nov 2022 07:36:20 -0800 (PST)
In-Reply-To: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
Injection-Info: google-groups.googlegroups.com; posting-host=108.52.63.158; posting-account=ujX_IwoAAACu0_cef9hMHeR8g0ZYDNHh
NNTP-Posting-Host: 108.52.63.158
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: timcaffrey@aol.com (Timothy McCaffrey)
Injection-Date: Thu, 17 Nov 2022 15:36:21 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2103
 by: Timothy McCaffrey - Thu, 17 Nov 2022 15:36 UTC

On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
> Seems like saturating arithmetic is a thing, apparently with
> integers. I can see that for applications that interface with
> the real world with limited-range sensors or actuators (no sense
> in trying to put more than 20 mA on a 4-20 mA current loop).
>
> And floating point (usually?) does not need it.
>
> What's the best way of encoding this in an ISA? To cover the
> range of 8,16,32,64 bit calculations would need two bits. Or,
> if somebody has, let's say, a 12-bit sensor, intermediate lengths
> might also come in handy to clip results to between -2048..2047.
>
> Or is it just not worth the bother? Maybe people who have
> experience with embedded CPUs can say something about this.

IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.

- Tim

Re: Encoding saturating arithmetic

<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31831&group=comp.arch#31831

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a0c:ee91:0:b0:4b4:a0b0:2dd8 with SMTP id u17-20020a0cee91000000b004b4a0b02dd8mr3926607qvr.19.1668713773593;
Thu, 17 Nov 2022 11:36:13 -0800 (PST)
X-Received: by 2002:a4a:d64d:0:b0:49e:e5dd:9ac8 with SMTP id
y13-20020a4ad64d000000b0049ee5dd9ac8mr2059009oos.43.1668713773360; Thu, 17
Nov 2022 11:36:13 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Thu, 17 Nov 2022 11:36:13 -0800 (PST)
In-Reply-To: <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:b464:e01b:44d1:1597;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:b464:e01b:44d1:1597
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: MitchAlsup@aol.com (MitchAlsup)
Injection-Date: Thu, 17 Nov 2022 19:36:13 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2880
 by: MitchAlsup - Thu, 17 Nov 2022 19:36 UTC

On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com wrote:
> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
> > Seems like saturating arithmetic is a thing, apparently with
> > integers. I can see that for applications that interface with
> > the real world with limited-range sensors or actuators (no sense
> > in trying to put more than 20 mA on a 4-20 mA current loop).
> >
> > And floating point (usually?) does not need it.
> >
> > What's the best way of encoding this in an ISA? To cover the
> > range of 8,16,32,64 bit calculations would need two bits. Or,
> > if somebody has, let's say, a 12-bit sensor, intermediate lengths
> > might also come in handy to clip results to between -2048..2047.
> >
> > Or is it just not worth the bother? Maybe people who have
> > experience with embedded CPUs can say something about this.
<
> IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
> For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.
<
For ADA: one would want to saturate at particular end-of-range which
may not be power of 2.
Do most of the rest: one would saturate to the size of a container which
may not be a power of 2.
<
If one can avoid integer overflow in the actual calculation, one can
have an extract instruction (or a saturate instruction) that provides
the necessary bounds (operands) and an instruction that does::
<
if( x < lbounds ) r = lbounds;
else if( x > ubounds ) r = ubounds;
else r = x;
>
> - Tim

Re: Encoding saturating arithmetic

<tl651l$2ndr0$1@dont-email.me>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31839&group=comp.arch#31839

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: sfuld@alumni.cmu.edu.invalid (Stephen Fuld)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Thu, 17 Nov 2022 12:20:03 -0800
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <tl651l$2ndr0$1@dont-email.me>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 17 Nov 2022 20:20:05 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="6549e92a6eae6af56c50190a0d3e82c1";
logging-data="2864992"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+r5cgibMUSPR3sgqFdYYJ8U2mnWS23JT4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:HhC8ZplrhqkURbkTm4oKidBbzIE=
Content-Language: en-US
In-Reply-To: <47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
 by: Stephen Fuld - Thu, 17 Nov 2022 20:20 UTC

On 11/17/2022 11:36 AM, MitchAlsup wrote:
> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com wrote:
>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>> Seems like saturating arithmetic is a thing, apparently with
>>> integers. I can see that for applications that interface with
>>> the real world with limited-range sensors or actuators (no sense
>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>
>>> And floating point (usually?) does not need it.
>>>
>>> What's the best way of encoding this in an ISA? To cover the
>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>> might also come in handy to clip results to between -2048..2047.
>>>
>>> Or is it just not worth the bother? Maybe people who have
>>> experience with embedded CPUs can say something about this.
> <
>> IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
>> For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.
> <
> For ADA: one would want to saturate at particular end-of-range which
> may not be power of 2.
> Do most of the rest: one would saturate to the size of a container which
> may not be a power of 2.
> <
> If one can avoid integer overflow in the actual calculation, one can
> have an extract instruction (or a saturate instruction) that provides
> the necessary bounds (operands) and an instruction that does::
> <
> if( x < lbounds ) r = lbounds;
> else if( x > ubounds ) r = ubounds;
> else r = x;

A few comments.

1. I think that saturating arithmetic is a pretty big thing in DSPs. If
that is so, then perhaps someone who is familiar with the usage patterns
there can chime in.

2. I recall that the Mill will offer saturating arithmetic. Perhaps
Ivan can provide more information.

3. There is a fundamental question of whether to include the saturation
in the arithmetic instructions or as a separate instruction after the
arithmetic instruction. There are arguments on both sides.

4. Your example requires a three operand instruction. This is
frequently overkill, as for example, if the arithmetic operation was an
unsigned add, there is no need to test the lower bounds, and a simple,
two operand MAX instruction would be sufficient.

--
- Stephen Fuld
(e-mail address disguised to prevent spam)

Re: Encoding saturating arithmetic

<g4xdL.7$cVTf.3@fx16.iad>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31841&group=comp.arch#31841

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Encoding saturating arithmetic
Newsgroups: comp.arch
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com> <47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me>
Lines: 45
Message-ID: <g4xdL.7$cVTf.3@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 17 Nov 2022 20:46:04 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 17 Nov 2022 20:46:04 GMT
X-Received-Bytes: 2922
 by: Scott Lurndal - Thu, 17 Nov 2022 20:46 UTC

Stephen Fuld <sfuld@alumni.cmu.edu.invalid> writes:
>On 11/17/2022 11:36 AM, MitchAlsup wrote:
>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com wrote:
>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>>> Seems like saturating arithmetic is a thing, apparently with
>>>> integers. I can see that for applications that interface with
>>>> the real world with limited-range sensors or actuators (no sense
>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>>
>>>> And floating point (usually?) does not need it.
>>>>
>>>> What's the best way of encoding this in an ISA? To cover the
>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>>> might also come in handy to clip results to between -2048..2047.
>>>>
>>>> Or is it just not worth the bother? Maybe people who have
>>>> experience with embedded CPUs can say something about this.
>> <
>>> IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
>>> For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.
>> <
>> For ADA: one would want to saturate at particular end-of-range which
>> may not be power of 2.
>> Do most of the rest: one would saturate to the size of a container which
>> may not be a power of 2.
>> <
>> If one can avoid integer overflow in the actual calculation, one can
>> have an extract instruction (or a saturate instruction) that provides
>> the necessary bounds (operands) and an instruction that does::
>> <
>> if( x < lbounds ) r = lbounds;
>> else if( x > ubounds ) r = ubounds;
>> else r = x;
>
>A few comments.
>
>1. I think that saturating arithmetic is a pretty big thing in DSPs. If
>that is so, then perhaps someone who is familiar with the usage patterns
>there can chime in.

ARMv8 Neon (SIMD) instructions include saturating variants for most
of the arithmetic operations signed and unsigned including addition,
subtraction, multiplication, bit shift and rounding operations.

Re: Encoding saturating arithmetic

<tl699c$3g2di$1@newsreader4.netcologne.de>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31847&group=comp.arch#31847

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!.POSTED.2a0a-a540-201b-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de!not-for-mail
From: tkoenig@netcologne.de (Thomas Koenig)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Thu, 17 Nov 2022 21:32:28 -0000 (UTC)
Organization: news.netcologne.de
Distribution: world
Message-ID: <tl699c$3g2di$1@newsreader4.netcologne.de>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
Injection-Date: Thu, 17 Nov 2022 21:32:28 -0000 (UTC)
Injection-Info: newsreader4.netcologne.de; posting-host="2a0a-a540-201b-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de:2a0a:a540:201b:0:7285:c2ff:fe6c:992d";
logging-data="3672498"; mail-complaints-to="abuse@netcologne.de"
User-Agent: slrn/1.0.3 (Linux)
 by: Thomas Koenig - Thu, 17 Nov 2022 21:32 UTC

Timothy McCaffrey <timcaffrey@aol.com> schrieb:
> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>> Seems like saturating arithmetic is a thing, apparently with
>> integers. I can see that for applications that interface with
>> the real world with limited-range sensors or actuators (no sense
>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>
>> And floating point (usually?) does not need it.
>>
>> What's the best way of encoding this in an ISA? To cover the
>> range of 8,16,32,64 bit calculations would need two bits. Or,
>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>> might also come in handy to clip results to between -2048..2047.
>>
>> Or is it just not worth the bother? Maybe people who have
>> experience with embedded CPUs can say something about this.
>
> IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
> For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.

I just looked a bit at what different compilers / ISAs did for a
saturating results after a calculation, in two versions:

#define MIN(a,b) ((a)<(b)? (a) : (b))
#define MAX(a,b) ((a)>(b)? (a) : (b))

int saturate(int a)
{ int ret;
ret = MAX(a,-2048);
ret = MIN(ret,2047);
return ret;
}

and

int saturate(int a)
{ int ret;
if (a < -2048)
ret = -2048;
else if (a > 2047)
ret = 2047;
else
ret = a;

return ret;
}

ARM 64/gcc was (for both versions)

ssat r0, #12, r0
bx lr

so somebody must have done their pattern matching homework there.
Other code was a variant of (like ARM 64/clang)

mov w8, #-2048
cmn w0, #2048
csel w8, w0, w8, gt
mov w9, #2047
cmp w8, #2047
csel w0, w8, w9, lt
ret

For My 66000, the versions were actually different. The first
one gave the elegant

maxs r1,#-2048,r1
mins r1,#2047,r1
ret

the second code with a branch:

cmp r2,r1,#-2048
bge r2,.LBB0_1
; %bb.2: ; %return
mov r1,#-2048
ret
..LBB0_1: ; %if.else
mins r1,#2047,r1
ret

This would be another reason for putting in MAX and MIN instrucions
(and teaching the compiler how to use them).

Re: Encoding saturating arithmetic

<ZSxdL.9$cVTf.2@fx16.iad>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31848&group=comp.arch#31848

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Encoding saturating arithmetic
Newsgroups: comp.arch
Distribution: world
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com> <tl699c$3g2di$1@newsreader4.netcologne.de>
Lines: 41
Message-ID: <ZSxdL.9$cVTf.2@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 17 Nov 2022 21:40:09 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 17 Nov 2022 21:40:09 GMT
X-Received-Bytes: 1557
 by: Scott Lurndal - Thu, 17 Nov 2022 21:40 UTC

Thomas Koenig <tkoenig@netcologne.de> writes:
>Timothy McCaffrey <timcaffrey@aol.com> schrieb:
>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:

>I just looked a bit at what different compilers / ISAs did for a
>saturating results after a calculation, in two versions:
>
>#define MIN(a,b) ((a)<(b)? (a) : (b))
>#define MAX(a,b) ((a)>(b)? (a) : (b))
>
>int saturate(int a)
>{
> int ret;
> ret = MAX(a,-2048);
> ret = MIN(ret,2047);
> return ret;
>}
>
>and
>
>int saturate(int a)
>{
> int ret;
> if (a < -2048)
> ret = -2048;
> else if (a > 2047)
> ret = 2047;
> else
> ret = a;
>
> return ret;
>}
>
>ARM 64/gcc was (for both versions)
>
> ssat r0, #12, r0
> bx lr
>

Those are ARMv7 (A32) instructions. ARMv8 (64-bit) doesn't have an integer
saturation instruction.

Re: Encoding saturating arithmetic

<fe4f5a59-3484-46fd-aa87-93a754b5b273n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31850&group=comp.arch#31850

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:622a:1309:b0:3a5:def:19fe with SMTP id v9-20020a05622a130900b003a50def19femr4273192qtk.175.1668721741239;
Thu, 17 Nov 2022 13:49:01 -0800 (PST)
X-Received: by 2002:a05:6808:179b:b0:359:af90:2bb7 with SMTP id
bg27-20020a056808179b00b00359af902bb7mr5049575oib.23.1668721741000; Thu, 17
Nov 2022 13:49:01 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Thu, 17 Nov 2022 13:49:00 -0800 (PST)
In-Reply-To: <tl651l$2ndr0$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:b464:e01b:44d1:1597;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:b464:e01b:44d1:1597
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fe4f5a59-3484-46fd-aa87-93a754b5b273n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: MitchAlsup@aol.com (MitchAlsup)
Injection-Date: Thu, 17 Nov 2022 21:49:01 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 46
 by: MitchAlsup - Thu, 17 Nov 2022 21:49 UTC

On Thursday, November 17, 2022 at 2:20:08 PM UTC-6, Stephen Fuld wrote:
> On 11/17/2022 11:36 AM, MitchAlsup wrote:

> > If one can avoid integer overflow in the actual calculation, one can
> > have an extract instruction (or a saturate instruction) that provides
> > the necessary bounds (operands) and an instruction that does::
> > <
> > if( x < lbounds ) r = lbounds;
> > else if( x > ubounds ) r = ubounds;
> > else r = x;
> A few comments.
>
> 1. I think that saturating arithmetic is a pretty big thing in DSPs. If
> that is so, then perhaps someone who is familiar with the usage patterns
> there can chime in.
<
Many DSPs only have arithmetic containers barely able to hold the
calculation itself, and in this realm, saturation is important. Imagine
8-bit DSP without saturation ?!?
>
> 2. I recall that the Mill will offer saturating arithmetic. Perhaps
> Ivan can provide more information.
<
OpenGL has saturation in integer and in FP.
FP saturates at [+0.0...+1.0]
>
> 3. There is a fundamental question of whether to include the saturation
> in the arithmetic instructions or as a separate instruction after the
> arithmetic instruction. There are arguments on both sides.
>
In the general purpose world, we can't even get people to check for
overflow ?!? what chance does saturation have ???
>
> 4. Your example requires a three operand instruction. This is
> frequently overkill, as for example, if the arithmetic operation was an
> unsigned add, there is no need to test the lower bounds, and a simple,
> two operand MAX instruction would be sufficient.
<
MIN(ubound,MAX(lbound,result))
<
If you prefer.
>
>
>
> --
> - Stephen Fuld
> (e-mail address disguised to prevent spam)

Re: Encoding saturating arithmetic

<tl6rgq$2j5ca$1@dont-email.me>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31857&group=comp.arch#31857

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ivan@millcomputing.com (Ivan Godard)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Thu, 17 Nov 2022 18:43:38 -0800
Organization: A noiseless patient Spider
Lines: 80
Message-ID: <tl6rgq$2j5ca$1@dont-email.me>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
<tl651l$2ndr0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 18 Nov 2022 02:43:38 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3997abaaf651c728305dfd24b462889e";
logging-data="2725258"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oWT7sSv74kWYwUyL9if5N"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:gsnViyrAjm8fSSQccSVqBWGjQlw=
In-Reply-To: <tl651l$2ndr0$1@dont-email.me>
Content-Language: en-US
 by: Ivan Godard - Fri, 18 Nov 2022 02:43 UTC

On 11/17/2022 12:20 PM, Stephen Fuld wrote:
> On 11/17/2022 11:36 AM, MitchAlsup wrote:
>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
>> wrote:
>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>>> Seems like saturating arithmetic is a thing, apparently with
>>>> integers. I can see that for applications that interface with
>>>> the real world with limited-range sensors or actuators (no sense
>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>>
>>>> And floating point (usually?) does not need it.
>>>>
>>>> What's the best way of encoding this in an ISA? To cover the
>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>>> might also come in handy to clip results to between -2048..2047.
>>>>
>>>> Or is it just not worth the bother? Maybe people who have
>>>> experience with embedded CPUs can say something about this.
>> <
>>> IIRC, saturating adds & subtracts are used for things like JPEG &
>>> MPEG encoding/decoding.
>>> For 12 bit, you could do 16 bit and then have ceiling/floor
>>> operations to "clip" the result.
>> <
>> For ADA: one would want to saturate at particular end-of-range which
>> may not be power of 2.
>> Do most of the rest: one would saturate to the size of a container which
>> may not be a power of 2.
>> <
>> If one can avoid integer overflow in the actual calculation, one can
>> have an extract instruction (or a saturate instruction) that provides
>> the necessary bounds (operands) and an instruction that does::
>> <
>>            if(         x < lbounds   ) r = lbounds;
>>            else if( x > ubounds ) r = ubounds;
>>            else                              r = x;
>
> A few comments.
>
> 1.    I think that saturating arithmetic is a pretty big thing in DSPs.
> If that is so, then perhaps someone who is familiar with the usage
> patterns there can chime in.
>
> 2.    I recall that the Mill will offer saturating arithmetic.  Perhaps
> Ivan can provide more information.

Every operation that can overflow has saturation as one of the overflow
behaviors (the others are wrap, fault, and double-wide result).

The reason for hardware saturation is not bound checking, it's
intra-expression overflow. If the expression is "a = b +b" then you can
check the condition codes (the groan you hear is Mitch turning over in
his grave) with a conditional branch (plugging up the predictor) or a
special op. If the expression is a = b+c*d-e" then checking gets out of
hand.

> 3.    There is a fundamental question of whether to include the
> saturation in the arithmetic instructions or as a separate instruction
> after the arithmetic instruction.  There are arguments on both sides.

Per-instruction saturation is needed for intra-expression overflow. A
separate op, in addition to per-instruction saturation, is needed to
bound to non-datasize bounds, as needed for Ada and in practice needed
for many DSP algorithms regardless of the language.

> 4.    Your example requires a three operand instruction.  This is
> frequently overkill, as for example, if the arithmetic operation was an
> unsigned add, there is no need to test the lower bounds, and a simple,
> two operand MAX instruction would be sufficient.

Consider adding years to a calendar date constrained to be AD. There is
no year zero.

Fortunately, the great majority of constraints are statically known to
the compiler, so if your design has a convenient way to put constants in
the code stream (ahem - Mitch, Mill) you don't need more reg ports, and
for the rare dynamic cases you can use two ops, one for lower and one
for upper bound.

Re: Encoding saturating arithmetic

<a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31858&group=comp.arch#31858

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:620a:1116:b0:6f9:fea2:833a with SMTP id o22-20020a05620a111600b006f9fea2833amr4459876qkk.659.1668740033337;
Thu, 17 Nov 2022 18:53:53 -0800 (PST)
X-Received: by 2002:a9d:19e5:0:b0:66c:49c4:39a with SMTP id
k92-20020a9d19e5000000b0066c49c4039amr2910363otk.81.1668740033089; Thu, 17
Nov 2022 18:53:53 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Thu, 17 Nov 2022 18:53:52 -0800 (PST)
In-Reply-To: <tl6rgq$2j5ca$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:b464:e01b:44d1:1597;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:b464:e01b:44d1:1597
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me>
<tl6rgq$2j5ca$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: MitchAlsup@aol.com (MitchAlsup)
Injection-Date: Fri, 18 Nov 2022 02:53:53 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 6057
 by: MitchAlsup - Fri, 18 Nov 2022 02:53 UTC

On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
> > On 11/17/2022 11:36 AM, MitchAlsup wrote:
> >> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
> >> wrote:
> >>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
> >>>> Seems like saturating arithmetic is a thing, apparently with
> >>>> integers. I can see that for applications that interface with
> >>>> the real world with limited-range sensors or actuators (no sense
> >>>> in trying to put more than 20 mA on a 4-20 mA current loop).
> >>>>
> >>>> And floating point (usually?) does not need it.
> >>>>
> >>>> What's the best way of encoding this in an ISA? To cover the
> >>>> range of 8,16,32,64 bit calculations would need two bits. Or,
> >>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
> >>>> might also come in handy to clip results to between -2048..2047.
> >>>>
> >>>> Or is it just not worth the bother? Maybe people who have
> >>>> experience with embedded CPUs can say something about this.
> >> <
> >>> IIRC, saturating adds & subtracts are used for things like JPEG &
> >>> MPEG encoding/decoding.
> >>> For 12 bit, you could do 16 bit and then have ceiling/floor
> >>> operations to "clip" the result.
> >> <
> >> For ADA: one would want to saturate at particular end-of-range which
> >> may not be power of 2.
> >> Do most of the rest: one would saturate to the size of a container which
> >> may not be a power of 2.
> >> <
> >> If one can avoid integer overflow in the actual calculation, one can
> >> have an extract instruction (or a saturate instruction) that provides
> >> the necessary bounds (operands) and an instruction that does::
> >> <
> >> if( x < lbounds ) r = lbounds;
> >> else if( x > ubounds ) r = ubounds;
> >> else r = x;
> >
> > A few comments.
> >
> > 1. I think that saturating arithmetic is a pretty big thing in DSPs.
> > If that is so, then perhaps someone who is familiar with the usage
> > patterns there can chime in.
> >
> > 2. I recall that the Mill will offer saturating arithmetic. Perhaps
> > Ivan can provide more information.
> Every operation that can overflow has saturation as one of the overflow
> behaviors (the others are wrap, fault, and double-wide result).
>
> The reason for hardware saturation is not bound checking, it's
> intra-expression overflow. If the expression is "a = b +b" then you can
> check the condition codes (the groan you hear is Mitch turning over in
> his grave)
<
The contrapositive is much more likely. I an neither in my grave, nor
turning over.
<
> with a conditional branch (plugging up the predictor) or a
> special op. If the expression is a = b+c*d-e" then checking gets out of
> hand.
<
If you do it with predication you don't <necessarily>plug up[ the branch
predictor.
<
Which leads to the question: does one use the same predictor for
branches and for predication ??? Hmmmmm.........
<
> > 3. There is a fundamental question of whether to include the
> > saturation in the arithmetic instructions or as a separate instruction
> > after the arithmetic instruction. There are arguments on both sides.
> Per-instruction saturation is needed for intra-expression overflow. A
> separate op, in addition to per-instruction saturation, is needed to
> bound to non-datasize bounds, as needed for Ada and in practice needed
> for many DSP algorithms regardless of the language.
> > 4. Your example requires a three operand instruction. This is
> > frequently overkill, as for example, if the arithmetic operation was an
> > unsigned add, there is no need to test the lower bounds, and a simple,
> > two operand MAX instruction would be sufficient.
> Consider adding years to a calendar date constrained to be AD. There is
> no year zero.
<
There are all sorts of maladies in the calander no zero is but a sampling;
there are no leap year in years divisible by 100 except those divisible by 400;
Thursday 4 October 1582 was followed by Friday 15 October 1582, and so
on.
>
> Fortunately, the great majority of constraints are statically known to
> the compiler, so if your design has a convenient way to put constants in
> the code stream (ahem - Mitch, Mill) you don't need more reg ports, and
> for the rare dynamic cases you can use two ops, one for lower and one
> for upper bound.
<
As I am fond of stating: There is no cheaper way to deliver an operand to
a calculation unit that as a constant.

Re: Encoding saturating arithmetic

<tl6si9$2j5ca$2@dont-email.me>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31859&group=comp.arch#31859

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ivan@millcomputing.com (Ivan Godard)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Thu, 17 Nov 2022 19:01:29 -0800
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <tl6si9$2j5ca$2@dont-email.me>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
<tl651l$2ndr0$1@dont-email.me> <tl6rgq$2j5ca$1@dont-email.me>
<a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Nov 2022 03:01:29 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3997abaaf651c728305dfd24b462889e";
logging-data="2725258"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+erk0SMziMrxvi3PBxMMbE"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:bY0GQWtJnY0WsM+1R3bu1DoTiBI=
Content-Language: en-US
In-Reply-To: <a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
 by: Ivan Godard - Fri, 18 Nov 2022 03:01 UTC

On 11/17/2022 6:53 PM, MitchAlsup wrote:
> On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
>> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
>>> On 11/17/2022 11:36 AM, MitchAlsup wrote:
>>>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
>>>> wrote:
>>>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>>>>> Seems like saturating arithmetic is a thing, apparently with
>>>>>> integers. I can see that for applications that interface with
>>>>>> the real world with limited-range sensors or actuators (no sense
>>>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>>>>
>>>>>> And floating point (usually?) does not need it.
>>>>>>
>>>>>> What's the best way of encoding this in an ISA? To cover the
>>>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>>>>> might also come in handy to clip results to between -2048..2047.
>>>>>>
>>>>>> Or is it just not worth the bother? Maybe people who have
>>>>>> experience with embedded CPUs can say something about this.
>>>> <
>>>>> IIRC, saturating adds & subtracts are used for things like JPEG &
>>>>> MPEG encoding/decoding.
>>>>> For 12 bit, you could do 16 bit and then have ceiling/floor
>>>>> operations to "clip" the result.
>>>> <
>>>> For ADA: one would want to saturate at particular end-of-range which
>>>> may not be power of 2.
>>>> Do most of the rest: one would saturate to the size of a container which
>>>> may not be a power of 2.
>>>> <
>>>> If one can avoid integer overflow in the actual calculation, one can
>>>> have an extract instruction (or a saturate instruction) that provides
>>>> the necessary bounds (operands) and an instruction that does::
>>>> <
>>>> if( x < lbounds ) r = lbounds;
>>>> else if( x > ubounds ) r = ubounds;
>>>> else r = x;
>>>
>>> A few comments.
>>>
>>> 1. I think that saturating arithmetic is a pretty big thing in DSPs.
>>> If that is so, then perhaps someone who is familiar with the usage
>>> patterns there can chime in.
>>>
>>> 2. I recall that the Mill will offer saturating arithmetic. Perhaps
>>> Ivan can provide more information.
>> Every operation that can overflow has saturation as one of the overflow
>> behaviors (the others are wrap, fault, and double-wide result).
>>
>> The reason for hardware saturation is not bound checking, it's
>> intra-expression overflow. If the expression is "a = b +b" then you can
>> check the condition codes (the groan you hear is Mitch turning over in
>> his grave)
> <
> The contrapositive is much more likely. I an neither in my grave, nor
> turning over.
> <
>> with a conditional branch (plugging up the predictor) or a
>> special op. If the expression is a = b+c*d-e" then checking gets out of
>> hand.
> <
> If you do it with predication you don't <necessarily>plug up[ the branch
> predictor.
> <
> Which leads to the question: does one use the same predictor for
> branches and for predication ??? Hmmmmm.........

Why predict predication? Just be wide enough that you don't care that
some are nooped.

Re: Encoding saturating arithmetic

<tl7b2k$3go18$1@newsreader4.netcologne.de>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31861&group=comp.arch#31861

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!.POSTED.2a0a-a540-201b-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de!not-for-mail
From: tkoenig@netcologne.de (Thomas Koenig)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 07:09:08 -0000 (UTC)
Organization: news.netcologne.de
Distribution: world
Message-ID: <tl7b2k$3go18$1@newsreader4.netcologne.de>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<tl699c$3g2di$1@newsreader4.netcologne.de> <ZSxdL.9$cVTf.2@fx16.iad>
Injection-Date: Fri, 18 Nov 2022 07:09:08 -0000 (UTC)
Injection-Info: newsreader4.netcologne.de; posting-host="2a0a-a540-201b-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de:2a0a:a540:201b:0:7285:c2ff:fe6c:992d";
logging-data="3694632"; mail-complaints-to="abuse@netcologne.de"
User-Agent: slrn/1.0.3 (Linux)
 by: Thomas Koenig - Fri, 18 Nov 2022 07:09 UTC

Scott Lurndal <scott@slp53.sl.home> schrieb:
> Thomas Koenig <tkoenig@netcologne.de> writes:
[...]
>>ARM 64/gcc was (for both versions)
>>
>> ssat r0, #12, r0
>> bx lr
>>
>
> Those are ARMv7 (A32) instructions. ARMv8 (64-bit) doesn't have an integer
> saturation instruction.

Correct, I selected the wrong compiler on godbolt.

Re: Encoding saturating arithmetic

<tl7koa$2tnu8$1@dont-email.me>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31865&group=comp.arch#31865

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 10:54:18 +0100
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <tl7koa$2tnu8$1@dont-email.me>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Nov 2022 09:54:18 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="af11441be165e54e49ca43a3f5870de0";
logging-data="3071944"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18op+QWkf8E5Y5jUuDcrKGmMkdE33vfci4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:VvndGv5f0+YKtg6Itce1OfJ4q10=
In-Reply-To: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
Content-Language: en-GB
 by: David Brown - Fri, 18 Nov 2022 09:54 UTC

On 17/11/2022 07:25, Thomas Koenig wrote:
> Seems like saturating arithmetic is a thing, apparently with
> integers. I can see that for applications that interface with
> the real world with limited-range sensors or actuators (no sense
> in trying to put more than 20 mA on a 4-20 mA current loop).
>
> And floating point (usually?) does not need it.
>
> What's the best way of encoding this in an ISA? To cover the
> range of 8,16,32,64 bit calculations would need two bits. Or,
> if somebody has, let's say, a 12-bit sensor, intermediate lengths
> might also come in handy to clip results to between -2048..2047.
>
> Or is it just not worth the bother? Maybe people who have
> experience with embedded CPUs can say something about this.

As you say, it is found in some embedded systems (and that includes
floating point saturation as well as integer saturation, though integer
saturation is more important). It is particularly popular in DSPs and
DSP algorithms.

You can google for the saturating arithmetic functions in ARM Cortex-M
processors. They are considered part of the "DSP functions", along with
multiply-accumulate instructions and the like. They are used in some
code, usually using intrinsics or even inline assembly to get the peak
efficiency.

There is also a somewhat outdated proposed C extension, "Extensions to
support embedded processors"
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf>. The whole
TR is, IMHO, a flop, and has not really done much. It included support
for saturated types, and there is limited support for them in gcc
<https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html>. However, they
are pretty useless in practice - if you need to use saturated types, you
need to know the exact size - types like "_Sat unsigned long _Fract" are
pointless.

I believe Ada has saturating types as part of the language, making them
a lot more convenient than in C - maybe an expert in that language can
say how useful they are.

Re: Encoding saturating arithmetic

<bd554a34-8153-40df-a748-594aa4cb35a0n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31867&group=comp.arch#31867

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:620a:6d7:b0:6fa:a605:ea6c with SMTP id 23-20020a05620a06d700b006faa605ea6cmr5633112qky.465.1668776887620;
Fri, 18 Nov 2022 05:08:07 -0800 (PST)
X-Received: by 2002:aca:bb08:0:b0:355:51ec:36c9 with SMTP id
l8-20020acabb08000000b0035551ec36c9mr6380746oif.245.1668776887056; Fri, 18
Nov 2022 05:08:07 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Fri, 18 Nov 2022 05:08:06 -0800 (PST)
In-Reply-To: <tl6rgq$2j5ca$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:b4b9:239c:76dc:8d09;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:b4b9:239c:76dc:8d09
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me>
<tl6rgq$2j5ca$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bd554a34-8153-40df-a748-594aa4cb35a0n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: already5chosen@yahoo.com (Michael S)
Injection-Date: Fri, 18 Nov 2022 13:08:07 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 6480
 by: Michael S - Fri, 18 Nov 2022 13:08 UTC

On Friday, November 18, 2022 at 4:43:45 AM UTC+2, Ivan Godard wrote:
> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
> > On 11/17/2022 11:36 AM, MitchAlsup wrote:
> >> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
> >> wrote:
> >>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
> >>>> Seems like saturating arithmetic is a thing, apparently with
> >>>> integers. I can see that for applications that interface with
> >>>> the real world with limited-range sensors or actuators (no sense
> >>>> in trying to put more than 20 mA on a 4-20 mA current loop).
> >>>>
> >>>> And floating point (usually?) does not need it.
> >>>>
> >>>> What's the best way of encoding this in an ISA? To cover the
> >>>> range of 8,16,32,64 bit calculations would need two bits. Or,
> >>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
> >>>> might also come in handy to clip results to between -2048..2047.
> >>>>
> >>>> Or is it just not worth the bother? Maybe people who have
> >>>> experience with embedded CPUs can say something about this.
> >> <
> >>> IIRC, saturating adds & subtracts are used for things like JPEG &
> >>> MPEG encoding/decoding.
> >>> For 12 bit, you could do 16 bit and then have ceiling/floor
> >>> operations to "clip" the result.
> >> <
> >> For ADA: one would want to saturate at particular end-of-range which
> >> may not be power of 2.
> >> Do most of the rest: one would saturate to the size of a container which
> >> may not be a power of 2.
> >> <
> >> If one can avoid integer overflow in the actual calculation, one can
> >> have an extract instruction (or a saturate instruction) that provides
> >> the necessary bounds (operands) and an instruction that does::
> >> <
> >> if( x < lbounds ) r = lbounds;
> >> else if( x > ubounds ) r = ubounds;
> >> else r = x;
> >
> > A few comments.
> >
> > 1. I think that saturating arithmetic is a pretty big thing in DSPs.
> > If that is so, then perhaps someone who is familiar with the usage
> > patterns there can chime in.
> >
> > 2. I recall that the Mill will offer saturating arithmetic. Perhaps
> > Ivan can provide more information.
> Every operation that can overflow has saturation as one of the overflow
> behaviors (the others are wrap, fault, and double-wide result).
>
> The reason for hardware saturation is not bound checking, it's
> intra-expression overflow. If the expression is "a = b +b" then you can
> check the condition codes (the groan you hear is Mitch turning over in
> his grave) with a conditional branch (plugging up the predictor) or a
> special op. If the expression is a = b+c*d-e" then checking gets out of
> hand.

Sound like an argument for sticky overflow flag rather than saturation.

> > 3. There is a fundamental question of whether to include the
> > saturation in the arithmetic instructions or as a separate instruction
> > after the arithmetic instruction. There are arguments on both sides.
> Per-instruction saturation is needed for intra-expression overflow. A
> separate op, in addition to per-instruction saturation, is needed to
> bound to non-datasize bounds, as needed for Ada

Where is it needed in Ada?
According to my (incomplete, but not too incomplete) knowledge,
there are know situation where saturation arithmetic could help
in implementing compilers for Ada-83 or 95.
Is different in newer standards or am I missing something about
old ones?

> and in practice needed
> for many DSP algorithms regardless of the language.

'many' is a too strong word. 'occasionally' is closer to reality.
On traditional DSPs most often you want to saturate accumulator,
which is most typically 40-bit wide (sometimes 36 bit) into signed
32-bit lower part of the same accumulator.
40->16 and 32->16 also happens, but relatively rarer and not always
justifies separate instruction.
On the other hand, 32->16 saturation combined with packing is very
common on SIMD processors, both DSPs and general-purpose.
Also, 16->8 saturation with packing is commonly provided, but
I am not sure that it is still commonly used. The sorts of processing
that utilized 16->8 either became trivial in multi-GHz era (still image
processing) or migrated to specialized engines.

> > 4. Your example requires a three operand instruction. This is
> > frequently overkill, as for example, if the arithmetic operation was an
> > unsigned add, there is no need to test the lower bounds, and a simple,
> > two operand MAX instruction would be sufficient.
> Consider adding years to a calendar date constrained to be AD. There is
> no year zero.
>
> Fortunately, the great majority of constraints are statically known to
> the compiler, so if your design has a convenient way to put constants in
> the code stream (ahem - Mitch, Mill) you don't need more reg ports, and
> for the rare dynamic cases you can use two ops, one for lower and one
> for upper bound.

Re: Encoding saturating arithmetic

<tl847o$1acg$1@gioia.aioe.org>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31868&group=comp.arch#31868

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!aioe.org!pcXxviI/vaijo7Aoz0JW3Q.user.46.165.242.91.POSTED!not-for-mail
From: terje.mathisen@tmsw.no (Terje Mathisen)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 15:18:40 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tl847o$1acg$1@gioia.aioe.org>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="43408"; posting-host="pcXxviI/vaijo7Aoz0JW3Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101
Firefox/68.0 SeaMonkey/2.53.14
X-Notice: Filtered by postfilter v. 0.9.2
 by: Terje Mathisen - Fri, 18 Nov 2022 14:18 UTC

MitchAlsup wrote:
> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com wrote:
>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>> Seems like saturating arithmetic is a thing, apparently with
>>> integers. I can see that for applications that interface with
>>> the real world with limited-range sensors or actuators (no sense
>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>
>>> And floating point (usually?) does not need it.
>>>
>>> What's the best way of encoding this in an ISA? To cover the
>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>> might also come in handy to clip results to between -2048..2047.
>>>
>>> Or is it just not worth the bother? Maybe people who have
>>> experience with embedded CPUs can say something about this.
> <
>> IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
>> For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.
> <
> For ADA: one would want to saturate at particular end-of-range which
> may not be power of 2.
> Do most of the rest: one would saturate to the size of a container which
> may not be a power of 2.
> <
> If one can avoid integer overflow in the actual calculation, one can
> have an extract instruction (or a saturate instruction) that provides
> the necessary bounds (operands) and an instruction that does::
> <
> if( x < lbounds ) r = lbounds;
> else if( x > ubounds ) r = ubounds;
> else r = x;

The best I've been able to come up with, in the time before CMOV, was to
subtract the lower bound, propagate any carry to save it, then compare
the result with the range: Any carry from that comparison would indicate
the need for saturation, with the type determined by the initial
carry/non-carry.

It only works when you have least one spare bit though, i.e. the input
range needs to allow the subtraction to never cause an overflow.

Terje

--
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

Re: Encoding saturating arithmetic

<tl84oc$1kag$1@gioia.aioe.org>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31869&group=comp.arch#31869

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!news.niel.me!aioe.org!pcXxviI/vaijo7Aoz0JW3Q.user.46.165.242.91.POSTED!not-for-mail
From: terje.mathisen@tmsw.no (Terje Mathisen)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 15:27:32 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tl84oc$1kag$1@gioia.aioe.org>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<tl699c$3g2di$1@newsreader4.netcologne.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="53584"; posting-host="pcXxviI/vaijo7Aoz0JW3Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101
Firefox/68.0 SeaMonkey/2.53.14
X-Notice: Filtered by postfilter v. 0.9.2
 by: Terje Mathisen - Fri, 18 Nov 2022 14:27 UTC

Thomas Koenig wrote:
> Timothy McCaffrey <timcaffrey@aol.com> schrieb:
>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>> Seems like saturating arithmetic is a thing, apparently with
>>> integers. I can see that for applications that interface with
>>> the real world with limited-range sensors or actuators (no sense
>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>
>>> And floating point (usually?) does not need it.
>>>
>>> What's the best way of encoding this in an ISA? To cover the
>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>> might also come in handy to clip results to between -2048..2047.
>>>
>>> Or is it just not worth the bother? Maybe people who have
>>> experience with embedded CPUs can say something about this.
>>
>> IIRC, saturating adds & subtracts are used for things like JPEG & MPEG encoding/decoding.
>> For 12 bit, you could do 16 bit and then have ceiling/floor operations to "clip" the result.
>
> I just looked a bit at what different compilers / ISAs did for a
> saturating results after a calculation, in two versions:
>
> #define MIN(a,b) ((a)<(b)? (a) : (b))
> #define MAX(a,b) ((a)>(b)? (a) : (b))
>
> int saturate(int a)
> {
> int ret;
> ret = MAX(a,-2048);
> ret = MIN(ret,2047);
> return ret;
> }
>
> and
>
> int saturate(int a)
> {
> int ret;
> if (a < -2048)
> ret = -2048;
> else if (a > 2047)
> ret = 2047;
> else
> ret = a;
>
> return ret;
> }
>
> ARM 64/gcc was (for both versions)
>
> ssat r0, #12, r0
> bx lr
>
> so somebody must have done their pattern matching homework there.
> Other code was a variant of (like ARM 64/clang)
>
> mov w8, #-2048
> cmn w0, #2048
> csel w8, w0, w8, gt
> mov w9, #2047
> cmp w8, #2047
> csel w0, w8, w9, lt
> ret
>
> For My 66000, the versions were actually different. The first
> one gave the elegant
>
> maxs r1,#-2048,r1
> mins r1,#2047,r1
> ret
>
> the second code with a branch:
>
> cmp r2,r1,#-2048
> bge r2,.LBB0_1
> ; %bb.2: ; %return
> mov r1,#-2048
> ret
> .LBB0_1: ; %if.else
> mins r1,#2047,r1
> ret
>
> This would be another reason for putting in MAX and MIN instrucions
> (and teaching the compiler how to use them).

Yeah!

Doing saturation at a binary power of two is of course easier than to
handle arbitrary limits, where upper, lower and range (upper-lower+1)
are all non-power of two.

Terje

--
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

Re: Encoding saturating arithmetic

<tl85co$1tg4$1@gioia.aioe.org>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31870&group=comp.arch#31870

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!aioe.org!pcXxviI/vaijo7Aoz0JW3Q.user.46.165.242.91.POSTED!not-for-mail
From: terje.mathisen@tmsw.no (Terje Mathisen)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 15:38:24 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tl85co$1tg4$1@gioia.aioe.org>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
<tl651l$2ndr0$1@dont-email.me> <tl6rgq$2j5ca$1@dont-email.me>
<a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="62980"; posting-host="pcXxviI/vaijo7Aoz0JW3Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101
Firefox/68.0 SeaMonkey/2.53.14
X-Notice: Filtered by postfilter v. 0.9.2
 by: Terje Mathisen - Fri, 18 Nov 2022 14:38 UTC

MitchAlsup wrote:
> On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
>> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
>>> On 11/17/2022 11:36 AM, MitchAlsup wrote:
>>>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
>>>> wrote:
>>>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>>>>> Seems like saturating arithmetic is a thing, apparently with
>>>>>> integers. I can see that for applications that interface with
>>>>>> the real world with limited-range sensors or actuators (no sense
>>>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>>>>
>>>>>> And floating point (usually?) does not need it.
>>>>>>
>>>>>> What's the best way of encoding this in an ISA? To cover the
>>>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>>>>> might also come in handy to clip results to between -2048..2047.
>>>>>>
>>>>>> Or is it just not worth the bother? Maybe people who have
>>>>>> experience with embedded CPUs can say something about this.
>>>> <
>>>>> IIRC, saturating adds & subtracts are used for things like JPEG &
>>>>> MPEG encoding/decoding.
>>>>> For 12 bit, you could do 16 bit and then have ceiling/floor
>>>>> operations to "clip" the result.
>>>> <
>>>> For ADA: one would want to saturate at particular end-of-range which
>>>> may not be power of 2.
>>>> Do most of the rest: one would saturate to the size of a container which
>>>> may not be a power of 2.
>>>> <
>>>> If one can avoid integer overflow in the actual calculation, one can
>>>> have an extract instruction (or a saturate instruction) that provides
>>>> the necessary bounds (operands) and an instruction that does::
>>>> <
>>>> if( x < lbounds ) r = lbounds;
>>>> else if( x > ubounds ) r = ubounds;
>>>> else r = x;
>>>
>>> A few comments.
>>>
>>> 1. I think that saturating arithmetic is a pretty big thing in DSPs.
>>> If that is so, then perhaps someone who is familiar with the usage
>>> patterns there can chime in.
>>>
>>> 2. I recall that the Mill will offer saturating arithmetic. Perhaps
>>> Ivan can provide more information.
>> Every operation that can overflow has saturation as one of the overflow
>> behaviors (the others are wrap, fault, and double-wide result).
>>
>> The reason for hardware saturation is not bound checking, it's
>> intra-expression overflow. If the expression is "a = b +b" then you can
>> check the condition codes (the groan you hear is Mitch turning over in
>> his grave)
> <
> The contrapositive is much more likely. I an neither in my grave, nor
> turning over.
> <
>> with a conditional branch (plugging up the predictor) or a
>> special op. If the expression is a = b+c*d-e" then checking gets out of
>> hand.
> <
> If you do it with predication you don't <necessarily>plug up[ the branch
> predictor.
> <
> Which leads to the question: does one use the same predictor for
> branches and for predication ??? Hmmmmm.........
> <
>>> 3. There is a fundamental question of whether to include the
>>> saturation in the arithmetic instructions or as a separate instruction
>>> after the arithmetic instruction. There are arguments on both sides.
>> Per-instruction saturation is needed for intra-expression overflow. A
>> separate op, in addition to per-instruction saturation, is needed to
>> bound to non-datasize bounds, as needed for Ada and in practice needed
>> for many DSP algorithms regardless of the language.
>>> 4. Your example requires a three operand instruction. This is
>>> frequently overkill, as for example, if the arithmetic operation was an
>>> unsigned add, there is no need to test the lower bounds, and a simple,
>>> two operand MAX instruction would be sufficient.
>> Consider adding years to a calendar date constrained to be AD. There is
>> no year zero.
> <
> There are all sorts of maladies in the calander no zero is but a sampling;
> there are no leap year in years divisible by 100 except those divisible by 400;
> Thursday 4 October 1582 was followed by Friday 15 October 1582, and so
> on.

Much worse: The jumps from Julian to Gregorian leap year handling
happened over a long time range, so various countries did it much later
than others.

In my own code I internally use 1600-03-01 as day zero and then pretend
negative day numbers are valid.

I'm still proud of the algorithm I came up with ~30 years ago which
converts a day number into year-month-day in less time than a single DIV
opcode: The textbook algorithms had a bunch of those div/mod operations.

Easter is similar, but I haven't bothered trying to make that one really
fast.
>>
>> Fortunately, the great majority of constraints are statically known to
>> the compiler, so if your design has a convenient way to put constants in
>> the code stream (ahem - Mitch, Mill) you don't need more reg ports, and
>> for the rare dynamic cases you can use two ops, one for lower and one
>> for upper bound.
> <
> As I am fond of stating: There is no cheaper way to deliver an operand to
> a calculation unit that as a constant.

Amen!

Terje

--
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

Re: Encoding saturating arithmetic

<db119b7b-5b39-46b0-bc17-eb04000b95b7n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31872&group=comp.arch#31872

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a37:8947:0:b0:6fa:11f6:518f with SMTP id l68-20020a378947000000b006fa11f6518fmr6014597qkd.774.1668783117828;
Fri, 18 Nov 2022 06:51:57 -0800 (PST)
X-Received: by 2002:aca:d0f:0:b0:35a:2c2c:9a37 with SMTP id
15-20020aca0d0f000000b0035a2c2c9a37mr6733746oin.79.1668783117567; Fri, 18 Nov
2022 06:51:57 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Fri, 18 Nov 2022 06:51:57 -0800 (PST)
In-Reply-To: <tl6si9$2j5ca$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:d040:5a95:8127:a64c;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:d040:5a95:8127:a64c
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me>
<tl6rgq$2j5ca$1@dont-email.me> <a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
<tl6si9$2j5ca$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <db119b7b-5b39-46b0-bc17-eb04000b95b7n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: MitchAlsup@aol.com (MitchAlsup)
Injection-Date: Fri, 18 Nov 2022 14:51:57 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 5026
 by: MitchAlsup - Fri, 18 Nov 2022 14:51 UTC

On Thursday, November 17, 2022 at 9:01:32 PM UTC-6, Ivan Godard wrote:
> On 11/17/2022 6:53 PM, MitchAlsup wrote:
> > On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
> >> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
> >>> On 11/17/2022 11:36 AM, MitchAlsup wrote:
> >>>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
> >>>> wrote:
> >>>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
> >>>>>> Seems like saturating arithmetic is a thing, apparently with
> >>>>>> integers. I can see that for applications that interface with
> >>>>>> the real world with limited-range sensors or actuators (no sense
> >>>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
> >>>>>>
> >>>>>> And floating point (usually?) does not need it.
> >>>>>>
> >>>>>> What's the best way of encoding this in an ISA? To cover the
> >>>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
> >>>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
> >>>>>> might also come in handy to clip results to between -2048..2047.
> >>>>>>
> >>>>>> Or is it just not worth the bother? Maybe people who have
> >>>>>> experience with embedded CPUs can say something about this.
> >>>> <
> >>>>> IIRC, saturating adds & subtracts are used for things like JPEG &
> >>>>> MPEG encoding/decoding.
> >>>>> For 12 bit, you could do 16 bit and then have ceiling/floor
> >>>>> operations to "clip" the result.
> >>>> <
> >>>> For ADA: one would want to saturate at particular end-of-range which
> >>>> may not be power of 2.
> >>>> Do most of the rest: one would saturate to the size of a container which
> >>>> may not be a power of 2.
> >>>> <
> >>>> If one can avoid integer overflow in the actual calculation, one can
> >>>> have an extract instruction (or a saturate instruction) that provides
> >>>> the necessary bounds (operands) and an instruction that does::
> >>>> <
> >>>> if( x < lbounds ) r = lbounds;
> >>>> else if( x > ubounds ) r = ubounds;
> >>>> else r = x;
> >>>
> >>> A few comments.
> >>>
> >>> 1. I think that saturating arithmetic is a pretty big thing in DSPs.
> >>> If that is so, then perhaps someone who is familiar with the usage
> >>> patterns there can chime in.
> >>>
> >>> 2. I recall that the Mill will offer saturating arithmetic. Perhaps
> >>> Ivan can provide more information.
> >> Every operation that can overflow has saturation as one of the overflow
> >> behaviors (the others are wrap, fault, and double-wide result).
> >>
> >> The reason for hardware saturation is not bound checking, it's
> >> intra-expression overflow. If the expression is "a = b +b" then you can
> >> check the condition codes (the groan you hear is Mitch turning over in
> >> his grave)
> > <
> > The contrapositive is much more likely. I an neither in my grave, nor
> > turning over.
> > <
> >> with a conditional branch (plugging up the predictor) or a
> >> special op. If the expression is a = b+c*d-e" then checking gets out of
> >> hand.
> > <
> > If you do it with predication you don't <necessarily>plug up[ the branch
> > predictor.
> > <
> > Which leads to the question: does one use the same predictor for
> > branches and for predication ??? Hmmmmm.........
<
> Why predict predication? Just be wide enough that you don't care that
> some are nooped.
<
I just wanted to stir the pot and illustrate why using branches as your
only means to control which instructions execute is very sub-optimal.

Re: Encoding saturating arithmetic

<0a645484-fa98-47af-b3c9-6bd01b0e3082n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31873&group=comp.arch#31873

  copy link   Newsgroups: comp.arch
X-Received: by 2002:ac8:7154:0:b0:399:2e73:5498 with SMTP id h20-20020ac87154000000b003992e735498mr6778167qtp.36.1668783232295;
Fri, 18 Nov 2022 06:53:52 -0800 (PST)
X-Received: by 2002:a05:6830:d87:b0:66d:8b98:683f with SMTP id
bv7-20020a0568300d8700b0066d8b98683fmr4232812otb.40.1668783230508; Fri, 18
Nov 2022 06:53:50 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Fri, 18 Nov 2022 06:53:50 -0800 (PST)
In-Reply-To: <tl7koa$2tnu8$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:d040:5a95:8127:a64c;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:d040:5a95:8127:a64c
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <tl7koa$2tnu8$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0a645484-fa98-47af-b3c9-6bd01b0e3082n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: MitchAlsup@aol.com (MitchAlsup)
Injection-Date: Fri, 18 Nov 2022 14:53:52 +0000
Content-Type: text/plain; charset="UTF-8"
 by: MitchAlsup - Fri, 18 Nov 2022 14:53 UTC

On Friday, November 18, 2022 at 3:54:22 AM UTC-6, David Brown wrote:
> On 17/11/2022 07:25, Thomas Koenig wrote:
> > Seems like saturating arithmetic is a thing, apparently with
> > integers. I can see that for applications that interface with
> > the real world with limited-range sensors or actuators (no sense
> > in trying to put more than 20 mA on a 4-20 mA current loop).
> >
> > And floating point (usually?) does not need it.
> >
> > What's the best way of encoding this in an ISA? To cover the
> > range of 8,16,32,64 bit calculations would need two bits. Or,
> > if somebody has, let's say, a 12-bit sensor, intermediate lengths
> > might also come in handy to clip results to between -2048..2047.
> >
> > Or is it just not worth the bother? Maybe people who have
> > experience with embedded CPUs can say something about this.
> As you say, it is found in some embedded systems (and that includes
> floating point saturation as well as integer saturation, though integer
> saturation is more important). It is particularly popular in DSPs and
> DSP algorithms.
>
> You can google for the saturating arithmetic functions in ARM Cortex-M
> processors. They are considered part of the "DSP functions", along with
> multiply-accumulate instructions and the like. They are used in some
> code, usually using intrinsics or even inline assembly to get the peak
> efficiency.
>
> There is also a somewhat outdated proposed C extension, "Extensions to
> support embedded processors"
> <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf>. The whole
> TR is, IMHO, a flop, and has not really done much. It included support
> for saturated types, and there is limited support for them in gcc
> <https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html>. However, they
> are pretty useless in practice - if you need to use saturated types, you
> need to know the exact size - types like "_Sat unsigned long _Fract" are
> pointless.
>
>
> I believe Ada has saturating types as part of the language, making them
> a lot more convenient than in C - maybe an expert in that language can
> say how useful they are.
<
Yes, but ADA saturates to the range of the variable, not its container size.
{Which was my point above.}

Re: Encoding saturating arithmetic

<36fc43ae-ab02-4fba-80ac-d951c9dcb261n@googlegroups.com>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31875&group=comp.arch#31875

  copy link   Newsgroups: comp.arch
X-Received: by 2002:ac8:1093:0:b0:39c:df33:c189 with SMTP id a19-20020ac81093000000b0039cdf33c189mr7010169qtj.498.1668783917232;
Fri, 18 Nov 2022 07:05:17 -0800 (PST)
X-Received: by 2002:a05:6870:cc8b:b0:142:150e:88ce with SMTP id
ot11-20020a056870cc8b00b00142150e88cemr7451803oab.245.1668783916401; Fri, 18
Nov 2022 07:05:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Fri, 18 Nov 2022 07:05:16 -0800 (PST)
In-Reply-To: <tl6si9$2j5ca$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:d040:5a95:8127:a64c;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:d040:5a95:8127:a64c
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me>
<tl6rgq$2j5ca$1@dont-email.me> <a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
<tl6si9$2j5ca$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <36fc43ae-ab02-4fba-80ac-d951c9dcb261n@googlegroups.com>
Subject: Re: Encoding saturating arithmetic
From: MitchAlsup@aol.com (MitchAlsup)
Injection-Date: Fri, 18 Nov 2022 15:05:17 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 5573
 by: MitchAlsup - Fri, 18 Nov 2022 15:05 UTC

On Thursday, November 17, 2022 at 9:01:32 PM UTC-6, Ivan Godard wrote:
> On 11/17/2022 6:53 PM, MitchAlsup wrote:
> > On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
> >> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
> >>> On 11/17/2022 11:36 AM, MitchAlsup wrote:
> >>>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
> >>>> wrote:
> >>>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
> >>>>>> Seems like saturating arithmetic is a thing, apparently with
> >>>>>> integers. I can see that for applications that interface with
> >>>>>> the real world with limited-range sensors or actuators (no sense
> >>>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
> >>>>>>
> >>>>>> And floating point (usually?) does not need it.
> >>>>>>
> >>>>>> What's the best way of encoding this in an ISA? To cover the
> >>>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
> >>>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
> >>>>>> might also come in handy to clip results to between -2048..2047.
> >>>>>>
> >>>>>> Or is it just not worth the bother? Maybe people who have
> >>>>>> experience with embedded CPUs can say something about this.
> >>>> <
> >>>>> IIRC, saturating adds & subtracts are used for things like JPEG &
> >>>>> MPEG encoding/decoding.
> >>>>> For 12 bit, you could do 16 bit and then have ceiling/floor
> >>>>> operations to "clip" the result.
> >>>> <
> >>>> For ADA: one would want to saturate at particular end-of-range which
> >>>> may not be power of 2.
> >>>> Do most of the rest: one would saturate to the size of a container which
> >>>> may not be a power of 2.
> >>>> <
> >>>> If one can avoid integer overflow in the actual calculation, one can
> >>>> have an extract instruction (or a saturate instruction) that provides
> >>>> the necessary bounds (operands) and an instruction that does::
> >>>> <
> >>>> if( x < lbounds ) r = lbounds;
> >>>> else if( x > ubounds ) r = ubounds;
> >>>> else r = x;
> >>>
> >>> A few comments.
> >>>
> >>> 1. I think that saturating arithmetic is a pretty big thing in DSPs.
> >>> If that is so, then perhaps someone who is familiar with the usage
> >>> patterns there can chime in.
> >>>
> >>> 2. I recall that the Mill will offer saturating arithmetic. Perhaps
> >>> Ivan can provide more information.
> >> Every operation that can overflow has saturation as one of the overflow
> >> behaviors (the others are wrap, fault, and double-wide result).
> >>
> >> The reason for hardware saturation is not bound checking, it's
> >> intra-expression overflow. If the expression is "a = b +b" then you can
> >> check the condition codes (the groan you hear is Mitch turning over in
> >> his grave)
> > <
> > The contrapositive is much more likely. I an neither in my grave, nor
> > turning over.
> > <
> >> with a conditional branch (plugging up the predictor) or a
> >> special op. If the expression is a = b+c*d-e" then checking gets out of
> >> hand.
> > <
> > If you do it with predication you don't <necessarily>plug up[ the branch
> > predictor.
> > <
> > Which leads to the question: does one use the same predictor for
> > branches and for predication ??? Hmmmmm.........
<
> Why predict predication? Just be wide enough that you don't care that
> some are nooped.
<
My compiler uses predication indiscriminately--it predicates anything
and everything that fits under an 8 instruction shadow--without thinking
about whether the control flow is predictable or not.
<
But the execution model does not use the predictor associated with
branches either.
<
The thought train is that one sues predication when the join point of
flow control will already have been fetched by the time the flow control
becomes manifest. And for the vast majority of predications, the join
point has already arrived by the time the PRED instruction is ready to
execute. Thus, there is no reason to alter the fetch point, and it is
easier to skip instructions.

Re: Encoding saturating arithmetic

<jtpulcFdnn5U1@mid.individual.net>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31881&group=comp.arch#31881

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.holsti@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 19:45:16 +0200
Organization: Tidorum Ltd
Lines: 58
Message-ID: <jtpulcFdnn5U1@mid.individual.net>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<tl7koa$2tnu8$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net 8ZqmB/PAkOl8xq42gewnRQrxzyV+IasDAHnLQCXpueE6J4Tgw7
Cancel-Lock: sha1:Vl+xhkW/tkxEcioR48+xXyv8D+k=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.4.1
Content-Language: en-US
In-Reply-To: <tl7koa$2tnu8$1@dont-email.me>
 by: Niklas Holsti - Fri, 18 Nov 2022 17:45 UTC

On 2022-11-18 11:54, David Brown wrote:

> I believe Ada has saturating types as part of the language,

Nope (or not yet). Ada provides range-constrained types (integer and
floating), and wrap-around types (unsigned integer only), but not
saturating types. Exceeding a range constraint raises an exception.

Of course one can program saturating types, and overload the ordinary
operators, as in many other languages.

> making them a lot more convenient than in C - maybe an expert in that
> language can say how useful they are.

I've never missed saturating types in my Ada programs. Saturation is
typically needed for commands to actuators, or at a few other points in
servo-control loops, but the upper and lower bounds are seldom powers of
two, and I don't see why one would want saturation on all operators --
more often, only the final result of a computation needs saturation.

On 2022-11-18 15:08, Michael S wrote:
> On Friday, November 18, 2022 at 4:43:45 AM UTC+2, Ivan Godard wrote
[snip]

>> Per-instruction saturation is needed for intra-expression overflow.
>> A separate op, in addition to per-instruction saturation, is needed
>> to bound to non-datasize bounds, as needed for Ada
>
> Where is it needed in Ada? According to my (incomplete, but not too
> incomplete) knowledge, there are know situation where saturation
> arithmetic could help in implementing compilers for Ada-83 or 95. Is
> different in newer standards or am I missing something about old
> ones?

I agree that saturating arithmetic is mostly not useful for implementing
Ada semantics. The only case I can think of, where it might be used, is
to implement a case statement with an "others" clause, for example:

type Count is range 1 .. 9;
C1, C2 : Count;
...
case C1 + C2 is
when 1 => ...;
when 2 => ...;
when 3 => ...;
when others => ...;
end case;

If the compiler decides to implement this 4-branch statement with a jump
table indexed from 0 to 3, it could perform the addition C1+C2 with
saturation to that range, thus routing all values except 1, 2, 3 to the
"others" branch. Of course any language with a similar "others" or
"default" branch could do that too.

Re: Encoding saturating arithmetic

<jtpuqiFdnn5U2@mid.individual.net>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31882&group=comp.arch#31882

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.holsti@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 19:48:02 +0200
Organization: Tidorum Ltd
Lines: 22
Message-ID: <jtpuqiFdnn5U2@mid.individual.net>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<tl7koa$2tnu8$1@dont-email.me>
<0a645484-fa98-47af-b3c9-6bd01b0e3082n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net 7s48mYe8FoC+2pQW2wW4hAqU5AuHxVlejctAOOACBLg10QUdRF
Cancel-Lock: sha1:pbZane7SycNHBIugEdxHTRcGBrY=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.4.1
Content-Language: en-US
In-Reply-To: <0a645484-fa98-47af-b3c9-6bd01b0e3082n@googlegroups.com>
 by: Niklas Holsti - Fri, 18 Nov 2022 17:48 UTC

On 2022-11-18 16:53, MitchAlsup wrote:
> On Friday, November 18, 2022 at 3:54:22 AM UTC-6, David Brown wrote:

[...]

>> I believe Ada has saturating types as part of the language, making them
>> a lot more convenient than in C - maybe an expert in that language can
>> say how useful they are.
> <
> Yes, but ADA saturates to the range of the variable, not its container size.
> {Which was my point above.}

As I wrote in an earlier reply to David Brown, Ada does not provide
saturating arithmetic, neither to natural power-of-two sizes nor to
programmer-defined ranges.

However, instructions that check a value against a range would be useful
in Ada code. The range would sometimes be static (compile-time constant)
but also sometimes dynamic, for example when indexing an array that is a
parameter to a routine and of a type that has an unconstrained index range.

Re: Encoding saturating arithmetic

<Y8QdL.11255$cVTf.2442@fx16.iad>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31884&group=comp.arch#31884

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
From: ThatWouldBeTelling@thevillage.com (EricP)
User-Agent: Thunderbird 2.0.0.24 (Windows/20100228)
MIME-Version: 1.0
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de> <85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com> <47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com> <tl651l$2ndr0$1@dont-email.me> <tl6rgq$2j5ca$1@dont-email.me> <a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
In-Reply-To: <a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 23
Message-ID: <Y8QdL.11255$cVTf.2442@fx16.iad>
X-Complaints-To: abuse@UsenetServer.com
NNTP-Posting-Date: Fri, 18 Nov 2022 18:28:08 UTC
Date: Fri, 18 Nov 2022 13:27:55 -0500
X-Received-Bytes: 1856
 by: EricP - Fri, 18 Nov 2022 18:27 UTC

MitchAlsup wrote:
> On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
> <
>> with a conditional branch (plugging up the predictor) or a
>> special op. If the expression is a = b+c*d-e" then checking gets out of
>> hand.
> <
> If you do it with predication you don't <necessarily>plug up[ the branch
> predictor.
> <
> Which leads to the question: does one use the same predictor for
> branches and for predication ??? Hmmmmm.........

There are a bunch of papers from back around Itanium days on
predicate prediction. (I suspect some changed the term "predication"
to "guarded execution" because "predicate prediction" winds up with too
many pred's in sentences, plus one might spit in peoples faces a lot.)

It seems that guarded execution (predication) can both positively and
negatively impact branch prediction by skewing the stats that BP sees.
This led some to propose GE and combined GE+BP predictors.

Re: Encoding saturating arithmetic

<tl953c$31268$1@dont-email.me>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31898&group=comp.arch#31898

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ivan@millcomputing.com (Ivan Godard)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Fri, 18 Nov 2022 15:39:25 -0800
Organization: A noiseless patient Spider
Lines: 101
Message-ID: <tl953c$31268$1@dont-email.me>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
<tl651l$2ndr0$1@dont-email.me> <tl6rgq$2j5ca$1@dont-email.me>
<a2d0f002-ff48-4b20-ade5-b5325eea8709n@googlegroups.com>
<tl6si9$2j5ca$2@dont-email.me>
<36fc43ae-ab02-4fba-80ac-d951c9dcb261n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Nov 2022 23:39:24 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3f06ddc9347caf3ccf5714bbefa5ef24";
logging-data="3180744"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PoQhwpetwXeko8CgZh5sX"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:aalYtyJVEiT03x4DHdVQb0JrSl0=
Content-Language: en-US
In-Reply-To: <36fc43ae-ab02-4fba-80ac-d951c9dcb261n@googlegroups.com>
 by: Ivan Godard - Fri, 18 Nov 2022 23:39 UTC

On 11/18/2022 7:05 AM, MitchAlsup wrote:
> On Thursday, November 17, 2022 at 9:01:32 PM UTC-6, Ivan Godard wrote:
>> On 11/17/2022 6:53 PM, MitchAlsup wrote:
>>> On Thursday, November 17, 2022 at 8:43:45 PM UTC-6, Ivan Godard wrote:
>>>> On 11/17/2022 12:20 PM, Stephen Fuld wrote:
>>>>> On 11/17/2022 11:36 AM, MitchAlsup wrote:
>>>>>> On Thursday, November 17, 2022 at 9:36:22 AM UTC-6, timca...@aol.com
>>>>>> wrote:
>>>>>>> On Thursday, November 17, 2022 at 1:25:42 AM UTC-5, Thomas Koenig wrote:
>>>>>>>> Seems like saturating arithmetic is a thing, apparently with
>>>>>>>> integers. I can see that for applications that interface with
>>>>>>>> the real world with limited-range sensors or actuators (no sense
>>>>>>>> in trying to put more than 20 mA on a 4-20 mA current loop).
>>>>>>>>
>>>>>>>> And floating point (usually?) does not need it.
>>>>>>>>
>>>>>>>> What's the best way of encoding this in an ISA? To cover the
>>>>>>>> range of 8,16,32,64 bit calculations would need two bits. Or,
>>>>>>>> if somebody has, let's say, a 12-bit sensor, intermediate lengths
>>>>>>>> might also come in handy to clip results to between -2048..2047.
>>>>>>>>
>>>>>>>> Or is it just not worth the bother? Maybe people who have
>>>>>>>> experience with embedded CPUs can say something about this.
>>>>>> <
>>>>>>> IIRC, saturating adds & subtracts are used for things like JPEG &
>>>>>>> MPEG encoding/decoding.
>>>>>>> For 12 bit, you could do 16 bit and then have ceiling/floor
>>>>>>> operations to "clip" the result.
>>>>>> <
>>>>>> For ADA: one would want to saturate at particular end-of-range which
>>>>>> may not be power of 2.
>>>>>> Do most of the rest: one would saturate to the size of a container which
>>>>>> may not be a power of 2.
>>>>>> <
>>>>>> If one can avoid integer overflow in the actual calculation, one can
>>>>>> have an extract instruction (or a saturate instruction) that provides
>>>>>> the necessary bounds (operands) and an instruction that does::
>>>>>> <
>>>>>> if( x < lbounds ) r = lbounds;
>>>>>> else if( x > ubounds ) r = ubounds;
>>>>>> else r = x;
>>>>>
>>>>> A few comments.
>>>>>
>>>>> 1. I think that saturating arithmetic is a pretty big thing in DSPs.
>>>>> If that is so, then perhaps someone who is familiar with the usage
>>>>> patterns there can chime in.
>>>>>
>>>>> 2. I recall that the Mill will offer saturating arithmetic. Perhaps
>>>>> Ivan can provide more information.
>>>> Every operation that can overflow has saturation as one of the overflow
>>>> behaviors (the others are wrap, fault, and double-wide result).
>>>>
>>>> The reason for hardware saturation is not bound checking, it's
>>>> intra-expression overflow. If the expression is "a = b +b" then you can
>>>> check the condition codes (the groan you hear is Mitch turning over in
>>>> his grave)
>>> <
>>> The contrapositive is much more likely. I an neither in my grave, nor
>>> turning over.
>>> <
>>>> with a conditional branch (plugging up the predictor) or a
>>>> special op. If the expression is a = b+c*d-e" then checking gets out of
>>>> hand.
>>> <
>>> If you do it with predication you don't <necessarily>plug up[ the branch
>>> predictor.
>>> <
>>> Which leads to the question: does one use the same predictor for
>>> branches and for predication ??? Hmmmmm.........
> <
>> Why predict predication? Just be wide enough that you don't care that
>> some are nooped.
> <
> My compiler uses predication indiscriminately--it predicates anything
> and everything that fits under an 8 instruction shadow--without thinking
> about whether the control flow is predictable or not.
> <
> But the execution model does not use the predictor associated with
> branches either.
> <
> The thought train is that one sues predication when the join point of
> flow control will already have been fetched by the time the flow control
> becomes manifest. And for the vast majority of predications, the join
> point has already arrived by the time the PRED instruction is ready to
> execute. Thus, there is no reason to alter the fetch point, and it is
> easier to skip instructions.

You need a good lawyer if you sue predication.

Seems that you condition predication on the encoding artifact that is
used to express it. A better choice is: use predication if the resources
used by all-path predication are less than the resources used by
branching as weighted by the path probability, including the cost of
misprediction. This leads to balancing resource criticality, width, and
miss cost and probability.

For a throughput design like Mill the resource is execution speed, the
width is family-member dependent, and miss is usually frequent enough to
dominate costs. Internally the code generator uses a tunable cost
function to decide on predication.

Re: Encoding saturating arithmetic

<tlaf1k$37df8$1@dont-email.me>

  copy mid

http://retrobbs.i2p/devel/article-flat.php?id=31900&group=comp.arch#31900

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.arch
Subject: Re: Encoding saturating arithmetic
Date: Sat, 19 Nov 2022 12:35:16 +0100
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <tlaf1k$37df8$1@dont-email.me>
References: <tl4k53$3f0sn$1@newsreader4.netcologne.de>
<85f0cd46-5c5c-424c-a50b-d2583cc5eb15n@googlegroups.com>
<47eb5ce6-c1f9-42fa-a9f9-42713e2c2d5en@googlegroups.com>
<tl651l$2ndr0$1@dont-email.me>
<fe4f5a59-3484-46fd-aa87-93a754b5b273n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 19 Nov 2022 11:35:16 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="adf326431c80a6eb06a4488ac11fb03c";
logging-data="3388904"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199t4SmxzSexjLWKfVIabaaZNttGnv+8Cs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
Cancel-Lock: sha1:Bji4Z5sIfXlAJgFDCwHAS+LfoYU=
In-Reply-To: <fe4f5a59-3484-46fd-aa87-93a754b5b273n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Sat, 19 Nov 2022 11:35 UTC

On 17/11/2022 22:49, MitchAlsup wrote:
> On Thursday, November 17, 2022 at 2:20:08 PM UTC-6, Stephen Fuld wrote:
>> On 11/17/2022 11:36 AM, MitchAlsup wrote:
>

>>
>> 4. Your example requires a three operand instruction. This is
>> frequently overkill, as for example, if the arithmetic operation was an
>> unsigned add, there is no need to test the lower bounds, and a simple,
>> two operand MAX instruction would be sufficient.
> <
> MIN(ubound,MAX(lbound,result))
> <
> If you prefer.

Usually, lbound is either 0, or -ubound. It's rare that you need two
independent bounds.


devel / comp.arch / Encoding saturating arithmetic

Pages:123456
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor