Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 204171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:26:38+00:00 2026-05-11T17:26:38+00:00

I need to draw peak meters for audio in realtime. Minimum 44100 samples per

  • 0

I need to draw peak meters for audio in realtime. Minimum 44100 samples per second times a minimum 40 streams. Each buffer is between 64 and 1024 samples. I need to grab the abs max from each buffer. (These are then fed through a kind of lowpass filter and drawn at about 20ms intervals.)

for(int i = 0; i < numSamples; i++)
{ 
      absMaxOfBuffer = MAX( fabs( buffer[i] ), absMaxOfBuffer);
}

That’s how I do it now. I’d like to do it much faster. The buffers have floats in the -1 to 1 range, hence the fabs.

Question, is there some tricky comp-sci quicksort-esque way of doing this faster?

Failing that, branchless ABS and MAX functions for floats, do they exist?

edit:
Primary platform is Linux/gcc but a windows port is planned (probably with mingw).

edit, the second:
I gave the accept to onebyone because of the bit regarding the actual algo structure which was central to the question.
I’ll try unrolling the loop to four at the time, zeroing the signbits and then getting the max with SSE (maxps instruction) and see if that doesn’t peel the banana. Thanks for the suggestions, I’ve up-voted a few of you, as runners up. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:26:38+00:00Added an answer on May 11, 2026 at 5:26 pm

    fabs and comparison are both really fast for IEEE floats (like, single-integer-op fast in principle).

    If the compiler isn’t inlining both operations, then either poke it until it does, or find the implementation for your architecture and inline it yourself.

    You can maybe get something out of the fact that positive IEEE floats go in the same order as the integers with the same bit patterns. That is,

    f > g   iff   *(int*)&f > *(int*)&g
    

    So once you’ve fabs’ed, I think that a branch-free max for int will also work for float (assuming they’re the same size of course). There’s an explanation of why this works here: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm. But your compiler already knows all this, as does your CPU, so it may not make any difference.

    There is no complexity-faster way of doing it. Your algorithm is already O(n), and you can’t beat that and still look at every sample.

    I guess there’s probably something in your processor’s SIMD (that is, SSE2 on Intel) that would help, by processing more data per clock cycle than your code. But I don’t know what. If there is, then it quite possibly will be several times faster.

    You could probably parallelize on a multi-core CPU, especially since you’re dealing with 40 independent streams anyway. That will be at best a few factors faster. “Just” launch the appropriate number of extra threads, split the work between them, and use the lightest-weight primitive you can to indicate when they’re all complete (maybe a thread barrier). I’m not quite clear whether you’re plotting the max of all 40 streams, or the max of each separately, so maybe you don’t actually need to synchronise the worker threads, other than to ensure results are delivered to the next stage without data corruption.

    It’s probably worth taking a look at the disassembly to see how much the compiler has unrolled the loop. Try unrolling it a bit more, see if that makes any difference.

    Another thing to think about is how many cache misses you are getting, and whether it’s possible to reduce the number by giving the cache a few clues so it can load the right pages ahead of time. But I have no experience with this, and I wouldn’t hold out much hope. __builtin_prefetch is the magic incantation on gcc, and I guess the first experiment would be something like “prefetch the beginning of the next block before entering the loop for this block”.

    What percentage of the required speed are you currently at? Or is it a case of “as fast as possible”?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need draw some stuff on canvas and then lick on each. I made
I need to draw a circle as per my finger move so i have
I need to draw extensive realtime graphics on WinCE platform. For that purpose, which
I need to draw on a TPanel, ideally directly so I don't have another
I need to draw text onto a window HDC along a circular path using
I need to draw a .png image on an UIView. I have no clue
I need to draw a UILabel striked through. Therefore I subclassed UILabel and implemented
I need to draw a line one inch long on any device given a
I need to draw a graph of write accesses of two concurrently running threads.
I need to draw a transparent plane over a User control in my DotNET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.