Welcome, Guest. Please login or register.

Author Topic: Programming question! :)  (Read 3163 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Programming question! :)
« on: April 16, 2012, 03:44:36 PM »
I have a list that I want to compare a value against to determine the value's "rank", the problem is that once I have determined the rank I wish to end the search... In ASM this is easy (please bare with my half remembered 68k):
Code: [Select]


_start  move.l _number,d0
          cmpi.l #4185,d0
          blt.s _case4184
          cmpi.l #4507,d0
          blt.s _case4507
          cmpi.l #4883,d0
          blt.s _case4883
          cmpi.l #5327,d0
          blt.s _case5327
          move.l 0,d0
_break

           rts

_case4185
           move.l #4,d0
           jmp _break

_case4507
           move.l #3,d0
           jmp _break

_case4883
           move.l #2,d0
           jmp _break

_case5327
           move.l #1,d0
           jmp _break


But I want to write this in C... Suffice to say the actual list is much larger and CPU time is at a premium, is there any way I can write it without using Goto?

Offline jorkany

  • Hero Member
  • *****
  • Join Date: Sep 2006
  • Posts: 1009
    • Show only replies by jorkany
    • http://www.amigaos4.com
Re: Programming question! :)
« Reply #1 on: April 16, 2012, 03:49:46 PM »
Try using a switch statement. It should produce code very similar to what you have posted, usually using a jump table but check the asm output to make sure it's acceptable.



Also, you might want to consider examining your algorythmic boundaries!  (j/k)
 

Offline Thorham

  • Hero Member
  • *****
  • Join Date: Oct 2009
  • Posts: 1149
    • Show only replies by Thorham
Re: Programming question! :)
« Reply #2 on: April 16, 2012, 03:51:15 PM »
How long is that list exactly? If it's not too large you can use a table, which would certainly be the fastest way.
 

Offline TheBilgeRat

  • Hero Member
  • *****
  • Join Date: May 2010
  • Posts: 1657
    • Show only replies by TheBilgeRat
Re: Programming question! :)
« Reply #3 on: April 16, 2012, 04:09:03 PM »
Quote from: jorkany;688784
Try using a switch statement. It should produce code very similar to what you have posted, usually using a jump table but check the asm output to make sure it's acceptable.



Also, you might want to consider examining your algorythmic boundaries!  (j/k)


I see what you did there :D
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Programming question! :)
« Reply #4 on: April 16, 2012, 04:10:24 PM »
Quote from: jorkany;688784
Try using a switch statement. It should produce code very similar to what you have posted, usually using a jump table but check the asm output to make sure it's acceptable.



Also, you might want to consider examining your algorythmic boundaries!  (j/k)
Can't use a switch, notice that I score the number based on it being lower than a threshold value... The problem is that the threshold boundaries are not linear (and for the purpose of this task can't be calculated... I'm probably going to have a loop that compares against an array of threshold values. I was just hoping it could be done in a nice simple table for maximum performance :)

Offline Thorham

  • Hero Member
  • *****
  • Join Date: Oct 2009
  • Posts: 1149
    • Show only replies by Thorham
Re: Programming question! :)
« Reply #5 on: April 16, 2012, 04:24:18 PM »
Quote from: bloodline;688789
The problem is that the threshold boundaries are not linear (and for the purpose of this task can't be calculated...
What are those values exactly?

Quote from: bloodline;688789
I'm probably going to have a loop that compares against an array of threshold values. I was just hoping it could be done in a nice simple table for maximum performance :)
If you can have an array of threshold values, then you might still have your table. How big is the largest value?
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Programming question! :)
« Reply #6 on: April 16, 2012, 04:29:00 PM »
Quote from: Thorham;688793
What are those values exactly?

If you can have an array of threshold values, then you might still have your table. How big is the largest value?
The values are rotational time values and I need to set an index dependant on a specific time. They scale somewhat geometrically, but not exactly, and I don't have enough CPU time to calculate that anyway :-/

The largest value is 60,000 (a lookup table is totally out of the question) :)

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2280
  • Country: us
  • Gender: Male
    • Show only replies by SamuraiCrow
Re: Programming question! :)
« Reply #7 on: April 16, 2012, 04:33:42 PM »
Quote from: bloodline;688783
I have a list that I want to compare a value against to determine the value's "rank", the problem is that once I have determined the rank I wish to end the search... In ASM this is easy (please bare with my half remembered 68k):
Code: [Select]


_start  move.l _number,d0
          cmpi.l #4185,d0
          blt.s _case4184
          cmpi.l #4507,d0
          blt.s _case4507
          cmpi.l #4883,d0
          blt.s _case4883
          cmpi.l #5327,d0
          blt.s _case5327
          move.l 0,d0
_break

           rts

_case4185
           move.l #4,d0
           jmp _break

_case4507
           move.l #3,d0
           jmp _break

_case4883
           move.l #2,d0
           jmp _break

_case5327
           move.l #1,d0
           jmp _break


But I want to write this in C... Suffice to say the actual list is much larger and CPU time is at a premium, is there any way I can write it without using Goto?

Code: [Select]

int test(num)
{
  if (num<4185)return 4;
  if (num<4507)return 3;
  if (num<4883)return 2;
  if (num<5327)return 1;
  return 0;
}

This is an exact conversion of the Assembly code you gave and it doesn't use any goto statements.
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Programming question! :)
« Reply #8 on: April 16, 2012, 04:36:23 PM »
Quote from: SamuraiCrow;688795
Code: [Select]

int test(num)
{
  if (num<4185)return 4;
  if (num<4507)return 3;
  if (num<4883)return 2;
  if (num<5327)return 1;
  return 0;
}

This is an exact conversion of the Assembly code you gave and it doesn't use any goto statements.
Damn, I couldn't see for looking! Cheers :) hahahah

Offline Thorham

  • Hero Member
  • *****
  • Join Date: Oct 2009
  • Posts: 1149
    • Show only replies by Thorham
Re: Programming question! :)
« Reply #9 on: April 16, 2012, 05:30:31 PM »
Quote from: bloodline;688794
The values are rotational time values and I need to set an index dependant on a specific time. They scale somewhat geometrically, but not exactly, and I don't have enough CPU time to calculate that anyway :-/
I was going to say that you don't have to calculate anything if you know the thresholds beforehand, and can just use a table, but ...

Quote from: bloodline;688794
The largest value is 60,000 (a lookup table is totally out of the question) :)
... if a 60000 entry long array is out of the question, then what's the target system (memory constraints)? Is such a table too large? Or is the problem that the thresholds are floating point? If it's floating point, and you don't need more than three decimals of precision (which 60,000 leads me to believe), then a table may still possible (fixed point).

Please provide us with some more information here, because you've left out crucial details.
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Programming question! :)
« Reply #10 on: April 16, 2012, 06:04:55 PM »
Quote from: Thorham;688807
I was going to say that you don't have to calculate anything if you know the thresholds beforehand, and can just use a table, but ...

... if a 60000 entry long array is out of the question, then what's the target system (memory constraints)? Is such a table too large? Or is the problem that the thresholds are floating point? If it's floating point, and you don't need more than three decimals of precision (which 60,000 leads me to believe), then a table may still possible (fixed point).

Please provide us with some more information here, because you've left out crucial details.
The target system is an ATMega328 :) and Sam Crow has poked my brain and given me exactly the solution I was trying to think of... Funny how I could see it in 68k Asm (that haven't used for 15 years), but not in C

Offline Thorham

  • Hero Member
  • *****
  • Join Date: Oct 2009
  • Posts: 1149
    • Show only replies by Thorham
Re: Programming question! :)
« Reply #11 on: April 16, 2012, 06:12:44 PM »
Quote from: bloodline;688813
The target system is an ATMega328 :) and Sam Crow has poked my brain and given me exactly the solution I was trying to think of... Funny how I could see it in 68k Asm (that haven't used for 15 years), but not in C
Ah, that clears it up then. On a system with such a small amount of memory, a table is indeed out of the question :)
 

Offline Fats

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 672
    • Show only replies by Fats
Re: Programming question! :)
« Reply #12 on: April 16, 2012, 07:17:40 PM »
Quote from: SamuraiCrow;688795
Code: [Select]

int test(num)
{
  if (num<4185)return 4;
  if (num<4507)return 3;
  if (num<4883)return 2;
  if (num<5327)return 1;
  return 0;
}



If you don't want to do it in a function you could use ? : operator like:

Code: [Select]

  rank =
    (num<4185) ? 4 :
    (num<4507) ? 3 :
    (num<4883) ? 2 :
    (num<5327) ? 1 :
    0;


greets,
Staf.
Trust me...                                              I know what I\'m doing
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Programming question! :)
« Reply #13 on: April 16, 2012, 07:47:55 PM »
Ahhh! Good call, I'm quite comfortable with usig a function, but I won't forget this either!! Many thanks for your help :)

Offline Duce

  • Off to greener pastures
  • Hero Member
  • *****
  • Join Date: Jul 2009
  • Posts: 1699
    • Show only replies by Duce
    • http://amigabbs.blogspot.com/
Re: Programming question! :)
« Reply #14 on: April 16, 2012, 09:29:45 PM »
Threads like this are what makes A.org great.  People helping people out.