Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys: add common imath module mv isin() form test/driver_dac_dds #19378

Merged
merged 4 commits into from
Jun 6, 2023

Conversation

benpicco
Copy link
Contributor

Contribution description

An integer-only sin() function is generally useful, move it to a common place imath where more integer-only math functions can be added when needed.

Testing procedure

This only moves code around.

Issues/PRs references

@github-actions github-actions bot added Area: sys Area: System Area: tests Area: tests and testing framework labels Mar 10, 2023
@benpicco benpicco force-pushed the imath branch 2 times, most recently from bcd87e1 to 12488c2 Compare March 10, 2023 13:04
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Mar 10, 2023
@benpicco benpicco requested review from maribu and kfessel March 10, 2023 13:05
sys/imath/imath.c Outdated Show resolved Hide resolved
@riot-ci
Copy link

riot-ci commented Mar 10, 2023

Murdock results

✔️ PASSED

34d0027 imath: add powi() function

Success Failures Total Runtime
6933 0 6933 13m:11s

Artifacts

@benpicco benpicco force-pushed the imath branch 2 times, most recently from 0f6d400 to a51e84c Compare March 13, 2023 20:29
Copy link
Contributor

@kfessel kfessel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stared at this for too long:
bike-shed:

  • some pre- or postfix for the function names that indicate that it is not only special for its type but also less accurate i think fast_isin() would be nice (like fast_sqrt (just realised we don't have that quake algorithm :) ).

  • Q Notation needs to be explained https://en.wikipedia.org/wiki/Q_%28number_format%29

  • I think there should no be two ways for writing fractional binary number with N-Bit fraction in one piece of documentation (Q14 vs 2^^15)

  • define PI

  • ISIN_PERIOD is wrong

https://godbolt.org/z/edqv4T6YM

this get us both fast_isin and fast_icos (icos being smaller)
iPI = 0x8000>>1

static inline  __attribute__((always_inline))
int32_t ihelp(int32_t x){
    int32_t y;
    static const int32_t qN = 13,
                         qA = 12,
                          B = 19900,
                          C = 3516;

    x = x << (31 - qN);         /* Mask with PI                 */
    x = x >> (31 - qN);         /* Note: SIGNED shift! (to qN)  */
    x = x * x >> (2 * qN - 14); /* x=x^2 To Q14                 */

    y = B - (x * C >> 14);      /* B - x^2*C                    */
    y = (1 << qA)               /* A - x^2*(B-x^2*C)            */
      - (x * y >> 16);

    return y;
}

//static inline
int32_t isin(int32_t x){
    static const int32_t qN = 13;
    int32_t c = (x << (30 - qN));
    int32_t y = ihelp(x - (1 << qN));
    return c >= 0 ? y :-y ;
}

//static inline
int32_t icos(int32_t x){
    static const int32_t qN = 13;
    // vvv this is most probalbly not the optimal way to write that
    //     but the compiler gets it
    int32_t c = ((x + iPI/2) << (30 - qN));  
    int32_t y = ihelp(x);
    return c >= 0 ? y :-y ;
}

sys/include/imath.h Outdated Show resolved Hide resolved
@benpicco
Copy link
Contributor Author

iPI = 0x8000>>1

so iPI = ISIN_PERIOD >> 1?

@benpicco
Copy link
Contributor Author

it is not only special for its type but also less accurate

but is it really less accurate if we only have fixed point? The accuracy is pretty much given by the number format.

Plot of the values looks good.

isin

I did drop the ambiguous Q notation entirely.

@kfessel
Copy link
Contributor

kfessel commented Mar 22, 2023

i checked with a scaled libm sin() and got a difference of upto 13 (12 with some prescaling) (so ~3-4 Bits may be not accurate)

https://godbolt.org/z/8hdW7ra6M

@kfessel kfessel changed the title tests/driver_dac_dds: move isin() function to common imath module sys: add common imath module mv isin() form test/driver_dac_dds Mar 22, 2023
@maribu
Copy link
Member

maribu commented Mar 23, 2023

@kfessel asked me to join the bike shedding to untie the knot. My two cents are:

  • there are already double sin(double x), float sinf(float x), long double sinl(long double x) in the C standard, so an int32_t sini(int32_t x) (or maybe even an int32_t sini32(int32_t x)) would be more consistent
  • if there is an accuracy vs speed trade of an int32_t fastsini32(int32_t x) would make this obvious. But so would also be a note in Doxygen. Maybe a compromise would be an static inline wrapper without the fast that calls the fast one. And if one explicitly wants a fast or a highly accurate implementation, one could use the long name.

But to be honest, I'm not much a fan of bike shedding. I'm usually happy with whatever name the person investing the time and effort in PR picked for naming. IMO time is better spent on the implementation than on the naming of functions or data types.

@kfessel
Copy link
Contributor

kfessel commented Mar 23, 2023

I'd prefer fast_sini() keeps the sin visible.

@benpicco
Copy link
Contributor Author

benpicco commented Jun 5, 2023

Ok, I think it looks more ugly now, but there is now consistency with math.h

@kfessel
Copy link
Contributor

kfessel commented Jun 5, 2023

bors merge

@bors
Copy link
Contributor

bors bot commented Jun 5, 2023

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

bors bot added a commit that referenced this pull request Jun 5, 2023
19378: sys: add common imath module mv isin() form test/driver_dac_dds r=kfessel a=benpicco



19693: sys/color: extend unittest and fix module r=kfessel a=kfessel

### Contribution description

this extends the unittest for sys_color testing more colors 

### Testing procedure

```
RIOT_tree/tests/unittests$ make tests-color test
```
will fail since our `rgb2hsv` implementation is wrong (or is using an other colorspace than hsv2rgb (without documenting))

the new `hsv2rgb` test will succeed

### Issues/PRs references

#19614  was the reason i had a look at this 

#1315 added the rgb2hsv and hsv2rgb function

#9940 added the test for black special case

https://www.vagrearg.org/content/hsvrgb << some optimization for that function (avoiding float)


Co-authored-by: Benjamin Valentin <[email protected]>
Co-authored-by: Karl Fessel <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jun 5, 2023

Build failed (retrying...):

bors bot added a commit that referenced this pull request Jun 5, 2023
19378: sys: add common imath module mv isin() form test/driver_dac_dds r=kfessel a=benpicco



Co-authored-by: Benjamin Valentin <[email protected]>
@maribu
Copy link
Member

maribu commented Jun 5, 2023

bors cancel

@bors
Copy link
Contributor

bors bot commented Jun 5, 2023

Canceled.

@maribu
Copy link
Member

maribu commented Jun 5, 2023

bors cancel

@maribu
Copy link
Member

maribu commented Jun 5, 2023

bors merge

bors bot added a commit that referenced this pull request Jun 5, 2023
19378: sys: add common imath module mv isin() form test/driver_dac_dds r=maribu a=benpicco



19693: sys/color: extend unittest and fix module r=kfessel a=kfessel

### Contribution description

this extends the unittest for sys_color testing more colors 

### Testing procedure

```
RIOT_tree/tests/unittests$ make tests-color test
```
will fail since our `rgb2hsv` implementation is wrong (or is using an other colorspace than hsv2rgb (without documenting))

the new `hsv2rgb` test will succeed

### Issues/PRs references

#19614  was the reason i had a look at this 

#1315 added the rgb2hsv and hsv2rgb function

#9940 added the test for black special case

https://www.vagrearg.org/content/hsvrgb << some optimization for that function (avoiding float)


Co-authored-by: Benjamin Valentin <[email protected]>
Co-authored-by: Karl Fessel <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jun 5, 2023

Build failed (retrying...):

@bors
Copy link
Contributor

bors bot commented Jun 6, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 375eae5 into RIOT-OS:master Jun 6, 2023
@benpicco benpicco deleted the imath branch June 6, 2023 06:00
@benpicco benpicco added this to the Release 2023.07 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants