Server Help

Bot Questions - Turning coordinates into a string efficiently

grazzhoppa - Thu Aug 14, 2008 4:07 am
Post subject: Turning coordinates into a string efficiently
I have two methods of doing this: which do you believe is more CPU efficient for practical use? They are the same memory-usage wise.

The first involves:

The 2nd involves:


These split the map into 25 sectors and return the appropriate description of a sector based on a given point on the map.
Code: Show/Hide

const char labels[5][5][30] = {
    { "Upper-Left Corner", "Far-Left Upward", "Far-Left Center", "Far-Left Downward", "Lower-Left Corner" },
    { "Far-Up Leftward", "Center Upper-Left", "Center Left", "Center Lower-Left", "Far-Down Leftward"},
    { "Far-Up Center", "Center Upward", "Center", "Center Downward", "Far-Down Center"},
    { "Far-Up Rightward", "Center Upper-Right", "Center Right", "Center Lower-Right", "Far-Down Rightward"},
    { "Upper-Right Corner", "Far-Right Upward", "Far-Right Center", "Far-Right Downward", "Lower-Right Corner"}
};
const char* GeneratePlayerCoordsString(uint16_t xpos_px, uint16_t ypos_px) {
    uint8_t hori = xpos_px / (16384/5); // gives 0 based index into labels[][] array.  It works on basis of integer division.
    uint8_t vert = ypos_px / (16384/5);
    return labels[hori][vert];
}

Code: Show/Hide

const char* GeneratePlayerCoordsString(uint16_t xpos_px, uint16_t ypos_px) {
    if(xpos_px < 3276) {
        if(ypos_px < 3276) return "Upper-Left Corner";
        else if((ypos_px >= 3276) && (ypos_px < 6553)) return "Far-Left Upward";
        else if((ypos_px >= 6553) && (ypos_px < 9830)) return "Far-Left Center";
        else if((ypos_px >= 9830) && (ypos_px < 13107)) return "Far-Left Downward";
        else if(ypos_px >= 13107) return "Lower-Left Corner";
    }
    else if((xpos_px >= 3276) && (xpos_px < 6553)) {
        if(ypos_px < 3276) return "Far-Up Leftward";
        else if((ypos_px >= 3276) && (ypos_px < 6553)) return "Center Upper-Left";
        else if((ypos_px >= 6553) && (ypos_px < 9830)) return "Center Left";
        else if((ypos_px >= 9830) && (ypos_px < 13107)) return "Center Lower-Left";
        else if(ypos_px >= 13107) return "Far-Down Leftward";
    }
    else if((xpos_px >= 6553) && (xpos_px < 9830)) {
        if(ypos_px < 3276) return "Far-Up Center";
        else if((ypos_px >= 3276) && (ypos_px < 6553)) return "Center Upward";
        else if((ypos_px >= 6553) && (ypos_px < 9830)) return "Center";
        else if((ypos_px >= 9830) && (ypos_px < 13107)) return "Center Downward";
        else if(ypos_px >= 13107) return "Far-Down Center";
    }
    else if((xpos_px >= 9830) && (xpos_px < 13107)) {
        if(ypos_px < 3276) return "Far-Up Rightward";
        else if((ypos_px >= 3276) && (ypos_px < 6553)) return "Center Upper-Right";
        else if((ypos_px >= 6553) && (ypos_px < 9830)) return "Center Right";
        else if((ypos_px >= 9830) && (ypos_px < 13107)) return "Center Lower-Right";
        else if(ypos_px >= 13107) return "Far-Down Rightward";
    }
    else if(xpos_px >= 13107) {
        if(ypos_px < 3276) return "Upper-Right Corner";
        else if((ypos_px >= 3276) && (ypos_px < 6553)) return "Far-Right Upward";
        else if((ypos_px >= 6553) && (ypos_px < 9830)) return "Far-Right Center";
        else if((ypos_px >= 9830) && (ypos_px < 13107)) return "Far-Right Downward";
        else if(ypos_px >= 13107) return "Lower-Right Corner";
    }
    return "Unknown Location";
}


The function that uses division is assembled into this:
Code: Show/Hide

__Z15IntegerDivisiontt:
   pushl   %ebp
   movl   %esp, %ebp
   subl   $8, %esp
   movl   8(%ebp), %eax
   movl   12(%ebp), %edx
   movw   %ax, -2(%ebp)
   movw   %dx, -4(%ebp)
   movzwl   -2(%ebp), %eax
   movl   %eax, %edx
   shrl   $2, %edx
   movl   $1342505041, %eax
   mull   %edx
   movl   %edx, %eax
   shrl   $8, %eax
   movb   %al, -5(%ebp)
   movzwl   -4(%ebp), %eax
   movl   %eax, %edx
   shrl   $2, %edx
   movl   $1342505041, %eax
   mull   %edx
   movl   %edx, %eax
   shrl   $8, %eax
   movb   %al, -6(%ebp)
   movzbl   -5(%ebp), %edx
   imull   $150, %edx, %edx
   movzbl   -6(%ebp), %ecx
   movl   %ecx, %eax
   sall   $4, %eax
   subl   %ecx, %eax
   addl   %eax, %eax
   addl   %eax, %edx
   addl   $_labels, %edx
   movl   %edx, %eax
   leave
   ret

The function that uses comparisons is assembled into this:
Code: Show/Hide

__Z17IntegerComparisontt:
   pushl   %ebp
   movl   %esp, %ebp
   subl   $8, %esp
   movl   8(%ebp), %eax
   movl   12(%ebp), %edx
   movw   %ax, -2(%ebp)
   movw   %dx, -4(%ebp)
   cmpw   $3275, -2(%ebp)
   ja   L3
   cmpw   $3275, -4(%ebp)
   ja   L4
   movl   $LC0, -8(%ebp)
   jmp   L2
L4:
   cmpw   $3275, -4(%ebp)
   jbe   L6
   cmpw   $6552, -4(%ebp)
   ja   L6
   movl   $LC1, -8(%ebp)
   jmp   L2
L6:
   cmpw   $6552, -4(%ebp)
   jbe   L8
   cmpw   $9829, -4(%ebp)
   ja   L8
   movl   $LC2, -8(%ebp)
   jmp   L2
L8:
   cmpw   $9829, -4(%ebp)
   jbe   L10
   cmpw   $13106, -4(%ebp)
   ja   L10
   movl   $LC3, -8(%ebp)
   jmp   L2
L10:
   cmpw   $13106, -4(%ebp)
   jbe   L13
   movl   $LC4, -8(%ebp)
   jmp   L2
L3:
   cmpw   $3275, -2(%ebp)
   jbe   L14
   cmpw   $6552, -2(%ebp)
   ja   L14
   cmpw   $3275, -4(%ebp)
   ja   L15
   movl   $LC5, -8(%ebp)
   jmp   L2
L15:
   cmpw   $3275, -4(%ebp)
   jbe   L17
   cmpw   $6552, -4(%ebp)
   ja   L17
   movl   $LC6, -8(%ebp)
   jmp   L2
L17:
   cmpw   $6552, -4(%ebp)
   jbe   L19
   cmpw   $9829, -4(%ebp)
   ja   L19
   movl   $LC7, -8(%ebp)
   jmp   L2
L19:
   cmpw   $9829, -4(%ebp)
   jbe   L21
   cmpw   $13106, -4(%ebp)
   ja   L21
   movl   $LC8, -8(%ebp)
   jmp   L2
L21:
   cmpw   $13106, -4(%ebp)
   jbe   L13
   movl   $LC9, -8(%ebp)
   jmp   L2
L14:
   cmpw   $6552, -2(%ebp)
   jbe   L25
   cmpw   $9829, -2(%ebp)
   ja   L25
   cmpw   $3275, -4(%ebp)
   ja   L26
   movl   $LC10, -8(%ebp)
   jmp   L2
L26:
   cmpw   $3275, -4(%ebp)
   jbe   L28
   cmpw   $6552, -4(%ebp)
   ja   L28
   movl   $LC11, -8(%ebp)
   jmp   L2
L28:
   cmpw   $6552, -4(%ebp)
   jbe   L30
   cmpw   $9829, -4(%ebp)
   ja   L30
   movl   $LC12, -8(%ebp)
   jmp   L2
L30:
   cmpw   $9829, -4(%ebp)
   jbe   L32
   cmpw   $13106, -4(%ebp)
   ja   L32
   movl   $LC13, -8(%ebp)
   jmp   L2
L32:
   cmpw   $13106, -4(%ebp)
   jbe   L13
   movl   $LC14, -8(%ebp)
   jmp   L2
L25:
   cmpw   $9829, -2(%ebp)
   jbe   L36
   cmpw   $13106, -2(%ebp)
   ja   L36
   cmpw   $3275, -4(%ebp)
   ja   L37
   movl   $LC15, -8(%ebp)
   jmp   L2
L37:
   cmpw   $3275, -4(%ebp)
   jbe   L39
   cmpw   $6552, -4(%ebp)
   ja   L39
   movl   $LC16, -8(%ebp)
   jmp   L2
L39:
   cmpw   $6552, -4(%ebp)
   jbe   L41
   cmpw   $9829, -4(%ebp)
   ja   L41
   movl   $LC17, -8(%ebp)
   jmp   L2
L41:
   cmpw   $9829, -4(%ebp)
   jbe   L43
   cmpw   $13106, -4(%ebp)
   ja   L43
   movl   $LC18, -8(%ebp)
   jmp   L2
L43:
   cmpw   $13106, -4(%ebp)
   jbe   L13
   movl   $LC19, -8(%ebp)
   jmp   L2
L36:
   cmpw   $13106, -2(%ebp)
   jbe   L13
   cmpw   $3275, -4(%ebp)
   ja   L48
   movl   $LC20, -8(%ebp)
   jmp   L2
L48:
   cmpw   $3275, -4(%ebp)
   jbe   L50
   cmpw   $6552, -4(%ebp)
   ja   L50
   movl   $LC21, -8(%ebp)
   jmp   L2
L50:
   cmpw   $6552, -4(%ebp)
   jbe   L52
   cmpw   $9829, -4(%ebp)
   ja   L52
   movl   $LC22, -8(%ebp)
   jmp   L2
L52:
   cmpw   $9829, -4(%ebp)
   jbe   L54
   cmpw   $13106, -4(%ebp)
   ja   L54
   movl   $LC23, -8(%ebp)
   jmp   L2
L54:
   cmpw   $13106, -4(%ebp)
   jbe   L13
   movl   $LC24, -8(%ebp)
   jmp   L2
L13:
   movl   $LC25, -8(%ebp)
L2:
   movl   -8(%ebp), %eax
   leave
   ret

Dr Brain - Thu Aug 14, 2008 7:54 am
Post subject:
Unless you're doing this several million times a second, efficiency doesn't matter nearly as much as how long it takes YOU, the programmer, to implement it.
Samapico - Thu Aug 14, 2008 5:36 pm
Post subject:
Yeah, don't waste several hours to save 2 seconds of processing time over 2 years... :/

Though the division thing with the array is interesting, and I think it's obviously the most efficient
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group