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 8240181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:03+00:00 2026-06-07T20:24:03+00:00

Within a loop i have to implement a sort of clipping if ( isLast

  • 0

Within a loop i have to implement a sort of clipping

if ( isLast )
{
    val = ( val < 0 ) ? 0 : val;
    val = ( val > 255 ) ? 255 : val;        
}

However this "clipping" takes up almost half the time of execution of the loop in Neon .
This is what the whole loop looks like-

for (row = 0; row < height; row++)
{
  for (col = 0; col < width; col++)
  {
      Int sum;
      //...Calculate the sum
   
      Short val = ( sum + offset ) >> shift;
      if ( isLast )
      {
           val = ( val < 0 ) ? 0 : val;
           val = ( val > 255 ) ? 255 : val;        
      }
      dst[col] = val;
   }

}

This is how the clipping has been implemented in Neon

     cmp       %10,#1                           //if(isLast)         
     bne       3f                                         
     vmov.i32   %4, d4[0]                       //put val in %4
     cmp       %4,#0                            //if( val < 0 )
     blt       4f                               
     b         5f                               
     4:                                         
     mov       %4,#0                             
     vmov.i32   d4[0],%4                        
     5:                                         
     cmp       %4,%11                          //if( val > maxVal )
     bgt       6f                               
     b         3f                               
     6:                                         
     mov       %4,%11                            
     vmov.i32   d4[0],%4                       
     3:  

     

This is the mapping of variables to registers-

isLast-    %10
maxVal-    %11

Any suggestions to make it faster ?
Thanks

EDIT-

The clipping now looks like-

     "cmp       %10,#1                            \n\t"//if(isLast)      
     "bne       3f                                \n\t"          
     "vmin.s32   d4,d4,d13                        \n\t"
     "vmax.s32   d4,d4,d12                        \n\t"
     "3:                                          \n\t" 

//d13 contains maxVal(255)
//d12 contains 0

Time consumed by this portion of the code has dropped from 223ms to 18ms

  • 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-06-07T20:24:04+00:00Added an answer on June 7, 2026 at 8:24 pm

    Using normal compares with NEON is almost always a bad idea because it forces the contents of a NEON register into a general purpose ARM register, and this costs lots of cycles.

    You can use the vmin and vmax NEON instructions. Here is a little example that clamps an array of integers to any min/max values.

    void clampArray (int minimum,
                     int maximum,
                     int * input,
                     int * output,
                     int numElements)
    {
      // get two NEON values with your minimum and maximum in each lane:
      int32x2_t lower  = vdup_n_s32 (minimum);
      int32x2_t higher = vdup_n_s32 (maximum);
      int i;
    
      for (i=0; i<numElements; i+=2)
      {
        // load two integers
        int32x2_t x = vld1_s32 (&input[i]);
    
        // clamp against maximum:
        x = vmin_s32 (x, higher);
    
        // clamp against minimum
        x = vmax_s32 (x, lower);
    
        // store two integers
        vst1_s32 (&output[i], x);
      }
    } 
    

    Warning: This code assumes the numElements is always a multiple of two, and I haven’t tested it.

    You may even make it faster if you process four elements at a time using the vminq / vmaxq instructions and load/store four integers per iteration.

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

Sidebar

Related Questions

I have the following thats within a loop... echo <span class='srch-val'>.apply_filters( $value\n, $value).</span>; I
I have an nsthread, a while loop within this one. It's getting objects from
I am using a foreach loop within PHP similar to this: foreach ($class->getAttributes() as
I have a razor view with a BeginForm command and a loop within that
I have a loop and within that loop I'm running a Dispatcher Timer. Here's
I have a piece of code that is within an infinite loop. I would
I currently have a database that within a loop checks a table if a
Why does an event handler never get called if it's added within a loop
In my python script, I need to call within a for loop an executable,
Can you create a line of code, within a while-loop, that will create a

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.