amiga.org
     
iconAll times are GMT -6. The time now is 10:43 PM. | Welcome to Forum, please register to access all of our features.

» Amiga.org » Operating System Specific Discussions » Other Operating Systems » Programming question! :)

Other Operating Systems This forum is to allow our members to discuss other (non-Amiga-related) operating systems.

Reply
 
Thread Tools Display Modes
Old 04-16-2012, 08:44 AM   #1
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Programming question! :)

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:
_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?
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is offline   Reply With Quote
Old 04-16-2012, 08:49 AM   #2
jorkany
Defender of the Faith
Points: 9,119, Level: 64 Points: 9,119, Level: 64 Points: 9,119, Level: 64
Activity: 7% Activity: 7% Activity: 7%
 
jorkany's Avatar
 
Join Date: Sep 2006
Posts: 1,007
Blog Entries: 1
Default Re: Programming question! :)

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)
jorkany is offline   Reply With Quote
Old 04-16-2012, 08:51 AM   #3
Thorham
Cult Member
Points: 4,050, Level: 40 Points: 4,050, Level: 40 Points: 4,050, Level: 40
Activity: 26% Activity: 26% Activity: 26%
 
Thorham's Avatar
 
Join Date: Oct 2009
Posts: 553
Default Re: Programming question! :)

How long is that list exactly? If it's not too large you can use a table, which would certainly be the fastest way.
Thorham is offline   Reply With Quote
Old 04-16-2012, 09:09 AM   #4
TheBilgeRat
Defender of the Faith
Points: 8,533, Level: 62 Points: 8,533, Level: 62 Points: 8,533, Level: 62
Activity: 60% Activity: 60% Activity: 60%
 
TheBilgeRat's Avatar
 
Join Date: May 2010
Location: Dundee, OR
Posts: 1,621
Default Re: Programming question! :)

Quote:
Originally Posted by jorkany View Post
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
TheBilgeRat is offline   Reply With Quote
Old 04-16-2012, 09:10 AM   #5
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by jorkany View Post
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
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is offline   Reply With Quote
Old 04-16-2012, 09:24 AM   #6
Thorham
Cult Member
Points: 4,050, Level: 40 Points: 4,050, Level: 40 Points: 4,050, Level: 40
Activity: 26% Activity: 26% Activity: 26%
 
Thorham's Avatar
 
Join Date: Oct 2009
Posts: 553
Default Re: Programming question! :)

Quote:
Originally Posted by bloodline View Post
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:
Originally Posted by bloodline View Post
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?
Thorham is offline   Reply With Quote
Old 04-16-2012, 09:29 AM   #7
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Thorham View Post
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)
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is offline   Reply With Quote
Old 04-16-2012, 09:33 AM   #8
SamuraiCrow
Premium Member
Points: 13,253, Level: 75 Points: 13,253, Level: 75 Points: 13,253, Level: 75
Activity: 5% Activity: 5% Activity: 5%
 
SamuraiCrow's Avatar
 
Join Date: Feb 2002
Location: South Dakota, USA
Posts: 2,017
Blog Entries: 2
Send a message via Yahoo to SamuraiCrow Send a message via Skype™ to SamuraiCrow
Default Re: Programming question! :)

Quote:
Originally Posted by bloodline View Post
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:
_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:
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.
SamuraiCrow is offline   Reply With Quote
Old 04-16-2012, 09:36 AM   #9
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by SamuraiCrow View Post
Code:
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
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is offline   Reply With Quote
Old 04-16-2012, 10:30 AM   #10
Thorham
Cult Member
Points: 4,050, Level: 40 Points: 4,050, Level: 40 Points: 4,050, Level: 40
Activity: 26% Activity: 26% Activity: 26%
 
Thorham's Avatar
 
Join Date: Oct 2009
Posts: 553
Default Re: Programming question! :)

Quote:
Originally Posted by bloodline View Post
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:
Originally Posted by bloodline View Post
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.
Thorham is offline   Reply With Quote
Old 04-16-2012, 11:04 AM   #11
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Thorham View Post
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
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is offline   Reply With Quote
Old 04-16-2012, 11:12 AM   #12
Thorham
Cult Member
Points: 4,050, Level: 40 Points: 4,050, Level: 40 Points: 4,050, Level: 40
Activity: 26% Activity: 26% Activity: 26%
 
Thorham's Avatar
 
Join Date: Oct 2009
Posts: 553
Default Re: Programming question! :)

Quote:
Originally Posted by bloodline View Post
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
Thorham is offline   Reply With Quote
Old 04-16-2012, 12:17 PM   #13
Fats
Technoid
Points: 9,230, Level: 64 Points: 9,230, Level: 64 Points: 9,230, Level: 64
Activity: 5% Activity: 5% Activity: 5%
 
Fats's Avatar
 
Join Date: Mar 2002
Location: Belgium
Posts: 498
Default Re: Programming question! :)

Quote:
Originally Posted by SamuraiCrow View Post
Code:
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:
  rank =
    (num<4185) ? 4 :
    (num<4507) ? 3 :
    (num<4883) ? 2 :
    (num<5327) ? 1 :
    0;
greets,
Staf.
__________________
Trust me... I know what I'm doing
Fats is offline   Reply With Quote
Old 04-16-2012, 12:47 PM   #14
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Ahhh! Good call, I'm quite comfortable with usig a function, but I won't forget this either!! Many thanks for your help
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is offline   Reply With Quote
Old 04-16-2012, 02:29 PM   #15
Duce
Off to greener pastures
Points: 5,290, Level: 46 Points: 5,290, Level: 46 Points: 5,290, Level: 46
Activity: 27% Activity: 27% Activity: 27%
 
Join Date: Jul 2009
Posts: 1,056
Default Re: Programming question! :)

Threads like this are what makes A.org great. People helping people out.
Duce is online now   Reply With Quote
Reply

Bookmarks

Tags
programming , question

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump