Recent

Author Topic: BitHelpers - bit manipulation for standard pascal types  (Read 12740 times)

avra

  • Hero Member
  • *****
  • Posts: 2187
    • Additional info
Re: BitHelpers - bit manipulation for standard pascal types
« Reply #15 on: June 25, 2021, 01:14:48 am »
Looks very good.
Thanks. I'm glad you like it.  :D

Wondering if this should become a FPC standard...
Original idea was that once BitHelpers is feature complete and well tested, it becomes a FPC package. Maybe that time has come. We'll see how that goes...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 10795
Re: BitHelpers - bit manipulation for standard pascal types
« Reply #16 on: June 25, 2021, 09:46:23 am »
Note there is overlap with the standard Bithelpers that come with FPC3.2.0 and up, as you know. Maybe we should look at integration.

avra

  • Hero Member
  • *****
  • Posts: 2187
    • Additional info
Re: BitHelpers - bit manipulation for standard pascal types
« Reply #17 on: June 25, 2021, 12:15:57 pm »
Note there is overlap with the standard Bithelpers that come with FPC3.2.0 and up, as you know. Maybe we should look at integration.
Nothing was mentioned about it at https://wiki.freepascal.org/FPC_New_Features_3.2.0 so I thought it was still in trunk only. Therefore I have compared 3.0 and 3.2 sources, and I see that standard type helpers have been extended for bit access with SetBit(), ClearBit(), ToggleBit() and TestBit(). I have a feeling that if we add all methods from my bithelpers to standard types then it would be overcrowded. I don't mind doing it, but although bit manipulation is very important - out of embedded world it is actually not used that often to justify it's place in standard type default helpers, so I would prefer to see it in a separate FPC package and just add bithelpers unit to uses code section when needed. What do you think?

In latest update I have also moved away from standard SHL based bit getting/setting to precalculated BitValueLookupTable[] for speed optimization, but did not make any benchmarks yet to see if there was actual benefit (SHL should be very fast in modern processors). Patches should be made only after benchmarking decision.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 10795
Re: BitHelpers - bit manipulation for standard pascal types
« Reply #18 on: June 25, 2021, 02:35:20 pm »
I have a feeling that if we add all methods from my bithelpers to standard types then it would be overcrowded. I don't mind doing it, but although bit manipulation is very important - out of embedded world it is actually not used that often to justify it's place in standard type default helpers, so I would prefer to see it in a separate FPC package and just add bithelpers unit to uses code section when needed. What do you think?
Well, several points:
- I wrote that code with some great feedback and tests by Bart, but I should probably also write a request for documentation (and a proposal documentation to go with it)
- Apart from embedded, it is is useful in several ways, especially Database application, e.g. SQL is based on bit tables and it is highly useful for AI as well. (schuler likes your code not for nothing)
- I wrote it initially because I needed it for electronics on Raspberry Pi and the likes.
- I still think we should avoid overlaps, but lets discuss just the candidates that really warrant inclusion in my four.
Quote
In latest update I have also moved away from standard SHL based bit getting/setting to precalculated BitValueLookupTable[] for speed optimization, but did not make any benchmarks yet to see if there was actual benefit (SHL should be very fast in modern processors). Patches should be made only after benchmarking decision.
I agree. Note I have some updates for implemention of ROL/ROR SAR etc, which is also useful.

About speed: that is indeed highly CPU dependent. Lets come up with some tests.

I really appreciate your contribution! It is a nice library.
« Last Edit: June 25, 2021, 02:37:04 pm by Thaddy »

avra

  • Hero Member
  • *****
  • Posts: 2187
    • Additional info
Re: BitHelpers - bit manipulation for standard pascal types
« Reply #19 on: June 25, 2021, 03:41:33 pm »
Apart from embedded, it is useful in several ways, especially Database application, e.g. SQL is based on bit tables and it is highly useful for AI as well.
So you prefer adding to syshelp*.inc instead of adding a separate package? If other devs also support that idea then I could do that (if there is a devs resistance to that idea then I would more prefer rejection before then after doing all the work).

- I still think we should avoid overlaps, but lets discuss just the candidates that really warrant inclusion in my four.
What overlaps? I do not have your ToggleBit() and ClearBit() at all, and instead of your TestBit() and SetBit() I use array like access property Bit[] which I like very much and want to keep. Maybe I can change names of that property's getter and setter, since that is the only place where I see an overlap (or to be more precise a method name conflict). If preferred, besides renaming I might also hide those methods...

I really appreciate your contribution! It is a nice library.
You're very kind. Thanks!
« Last Edit: June 25, 2021, 03:53:40 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

avra

  • Hero Member
  • *****
  • Posts: 2187
    • Additional info
Re: BitHelpers - bit manipulation for standard pascal types
« Reply #20 on: July 12, 2021, 01:04:55 pm »
I have run some simple benchmarks (maybe too simple, but anyway) on x86 Win64, and shift based bit setting was faster 3% if variable was qword, and 10% if variable was word. So I will give up table based bit access and avoid spending 64 qwords for a lookup table. However inlining brings significant speed improvement in loops so I will leave it on.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018