www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Saturating integer arithmetic

reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
Hi people. I've been working on this colour library when I have free
time (almost never!), and I want to work on blending/filtering
functions, but that work is kinda blocked by saturating arithmetic
logic.
I started working on a saturating integer library a few times, but
it's much a much bigger job than it appears, and I haven't had enough
time for it. (Efficient) implementation tends to be significantly
different for every int width and signed/unsigned. I see no use for an
inefficient implementation used by a colour library; images tend to be
millions of pixels, and inefficiency very quickly adds up.

I wonder if anyone has an interest in the area and wants to have a go
at it? It's a pretty big job.
It should support scalars, packed vectors (ie, 4 bytes in an int), and
SIMD vectors (wider vectors using hardware simd ops). Each step can
gain considerable efficiency for the vector width. It's hard to write
a useful colour blending library without the full set of these
available.
Jun 26 2016
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 6/26/16 9:47 AM, Manu via Digitalmars-d wrote:
 Hi people. I've been working on this colour library when I have free
 time (almost never!), and I want to work on blending/filtering
 functions, but that work is kinda blocked by saturating arithmetic
 logic.
 I started working on a saturating integer library a few times, but
 it's much a much bigger job than it appears, and I haven't had enough
 time for it. (Efficient) implementation tends to be significantly
 different for every int width and signed/unsigned. I see no use for an
 inefficient implementation used by a colour library; images tend to be
 millions of pixels, and inefficiency very quickly adds up.
See the recent thread "DbI checked integral". Saturation is a direct goal, and you should be able to optimize operations for each type by defining relatively small hooks.
 I wonder if anyone has an interest in the area and wants to have a go
 at it? It's a pretty big job.
 It should support scalars, packed vectors (ie, 4 bytes in an int), and
 SIMD vectors (wider vectors using hardware simd ops). Each step can
 gain considerable efficiency for the vector width. It's hard to write
 a useful colour blending library without the full set of these
 available.
What primitives would the vectors implement? Andrei
Jun 26 2016
next sibling parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Sunday, 26 June 2016 at 13:59:03 UTC, Andrei Alexandrescu 
wrote:
 On 6/26/16 9:47 AM, Manu via Digitalmars-d wrote:
 Hi people. I've been working on this colour library when I 
 have free
 time (almost never!), and I want to work on blending/filtering
 functions, but that work is kinda blocked by saturating 
 arithmetic
 logic.
 I started working on a saturating integer library a few times, 
 but
 it's much a much bigger job than it appears, and I haven't had 
 enough
 time for it. (Efficient) implementation tends to be 
 significantly
 different for every int width and signed/unsigned. I see no 
 use for an
 inefficient implementation used by a colour library; images 
 tend to be
 millions of pixels, and inefficiency very quickly adds up.
See the recent thread "DbI checked integral". Saturation is a direct goal, and you should be able to optimize operations for each type by defining relatively small hooks.
No, saturated operations are typically SIMD instructions.
Jun 26 2016
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sunday, 26 June 2016 at 13:59:03 UTC, Andrei Alexandrescu 
wrote:
 See the recent thread "DbI checked integral". Saturation is a 
 direct goal, and you should be able to optimize operations for 
 each type by defining relatively small hooks.
alas, it is completely inappropriate for any computation-heavy task. even if dmd inliner will do some miracle and will inline some calls.
Jun 26 2016
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 06/26/2016 02:07 PM, ketmar wrote:
 On Sunday, 26 June 2016 at 13:59:03 UTC, Andrei Alexandrescu wrote:
 See the recent thread "DbI checked integral". Saturation is a direct
 goal, and you should be able to optimize operations for each type by
 defining relatively small hooks.
alas, it is completely inappropriate for any computation-heavy task. even if dmd inliner will do some miracle and will inline some calls.
What is some good literature to look at for SIMD saturation arithmetic? -- Andrei
Jun 26 2016
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sunday, 26 June 2016 at 22:34:29 UTC, Andrei Alexandrescu 
wrote:
 What is some good literature to look at for SIMD saturation 
 arithmetic? -- Andrei
sorry, i don't know. for me it was intel cpu manuals. certainly not even a good way to learn the things.
Jun 26 2016
prev sibling next sibling parent John Colvin <john.loughran.colvin gmail.com> writes:
On Sunday, 26 June 2016 at 22:34:29 UTC, Andrei Alexandrescu 
wrote:
 On 06/26/2016 02:07 PM, ketmar wrote:
 On Sunday, 26 June 2016 at 13:59:03 UTC, Andrei Alexandrescu 
 wrote:
 See the recent thread "DbI checked integral". Saturation is a 
 direct
 goal, and you should be able to optimize operations for each 
 type by
 defining relatively small hooks.
alas, it is completely inappropriate for any computation-heavy task. even if dmd inliner will do some miracle and will inline some calls.
What is some good literature to look at for SIMD saturation arithmetic? -- Andrei
http://www.intel.co.uk/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-opt mization-manual.pdf has some stuff about saturation in it. I learned from that and the instruction reference.
Jun 26 2016
prev sibling parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Sunday, 26 June 2016 at 22:34:29 UTC, Andrei Alexandrescu 
wrote:
 On 06/26/2016 02:07 PM, ketmar wrote:
 On Sunday, 26 June 2016 at 13:59:03 UTC, Andrei Alexandrescu 
 wrote:
 See the recent thread "DbI checked integral". Saturation is a 
 direct
 goal, and you should be able to optimize operations for each 
 type by
 defining relatively small hooks.
alas, it is completely inappropriate for any computation-heavy task. even if dmd inliner will do some miracle and will inline some calls.
What is some good literature to look at for SIMD saturation arithmetic? -- Andrei
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=adds_epi
Jun 26 2016
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Monday, 27 June 2016 at 04:23:10 UTC, Ola Fosheim Grøstad 
wrote:
 https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=adds_epi
Or in the context of 8 bit colour vectors: https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=adds_epu8
Jun 26 2016
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Monday, 27 June 2016 at 04:25:56 UTC, Ola Fosheim Grøstad 
wrote:
 On Monday, 27 June 2016 at 04:23:10 UTC, Ola Fosheim Grøstad 
 wrote:
 https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=adds_epi
Or in the context of 8 bit colour vectors: https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=adds_epu8
Turns out a careful selected search for "saturat" gives you a near complete overview over the standard intrinsics with saturation: https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=saturat
Jun 26 2016