Ticket #3281: ffs_arch_x86_opt.patch

File ffs_arch_x86_opt.patch, 6.8 KB (added by tqh, 10 years ago)

x86 ffs optimization

  • src/system/kernel/lib/arch/x86/arch_string.S

     
    114114FUNCTION_END(memset_generic)
    115115SYMBOL(memset_generic_end):
    116116
     117/* int *ffs(int value); */
     118.align 4
     119FUNCTION(ffs_generic):
     120    bsf     4(%esp), %eax
     121    ret
     122FUNCTION_END(ffs_generic)   
     123SYMBOL(ffs_generic_end):
    117124
    118125#if !_BOOT_MODE
    119126
     
    126133    jmp     *(gOptimizedFunctions + X86_OPTIMIZED_FUNCTIONS_memset)
    127134FUNCTION_END(memset)
    128135
     136FUNCTION(ffs):
     137    jmp     *(gOptimizedFunctions + X86_OPTIMIZED_FUNCTIONS_ffs)
     138FUNCTION_END(memset)
     139
    129140#endif  // !_BOOT_MODE
  • src/system/kernel/arch/x86/arch_cpu.cpp

     
    9393extern int memcpy_generic_end;
    9494extern "C" void memset_generic(void* dest, int value, size_t count);
    9595extern int memset_generic_end;
     96extern "C" int ffs_generic(int value);
     97extern int ffs_generic_end;
    9698
     99
    97100x86_optimized_functions gOptimizedFunctions = {
    98101    memcpy_generic,
    99102    &memcpy_generic_end,
    100103    memset_generic,
    101     &memset_generic_end
     104    &memset_generic_end,
     105    ffs_generic,
     106    &ffs_generic_end
    102107};
    103108
    104109
     
    660665    fill_commpage_entry(COMMPAGE_ENTRY_X86_MEMSET,
    661666        (const void*)gOptimizedFunctions.memset, memsetLen);
    662667
     668    size_t ffsLen = (addr_t)gOptimizedFunctions.ffs_end
     669        - (addr_t)gOptimizedFunctions.ffs;
     670    fill_commpage_entry(COMMPAGE_ENTRY_X86_FFS,
     671        (const void*)gOptimizedFunctions.ffs, ffsLen);
     672
    663673    // add the functions to the commpage image
    664674    image_id image = get_commpage_image();
    665675    elf_add_memory_image_symbol(image, "commpage_memcpy",
     
    668678    elf_add_memory_image_symbol(image, "commpage_memset",
    669679        ((addr_t*)USER_COMMPAGE_ADDR)[COMMPAGE_ENTRY_X86_MEMSET], memsetLen,
    670680        B_SYMBOL_TYPE_TEXT);
     681    elf_add_memory_image_symbol(image, "commpage_ffs",
     682        ((addr_t*)USER_COMMPAGE_ADDR)[COMMPAGE_ENTRY_X86_FFS], ffsLen,
     683        B_SYMBOL_TYPE_TEXT);
    671684
    672685    return B_OK;
    673686}
    674687
    675 
    676688void
    677689i386_set_tss_and_kstack(addr_t kstack)
    678690{
  • src/system/kernel/arch/x86/asm_offsets.cpp

     
    5959        memcpy);
    6060    DEFINE_OFFSET_MACRO(X86_OPTIMIZED_FUNCTIONS, x86_optimized_functions,
    6161        memset);
     62    DEFINE_OFFSET_MACRO(X86_OPTIMIZED_FUNCTIONS, x86_optimized_functions,
     63        ffs);
     64   
    6265}
  • src/system/libroot/posix/string/Jamfile

     
    44    bcmp.c
    55    bcopy.c
    66    bzero.c
    7     ffs.cpp
    87    memccpy.c
    98    memchr.c
    109    memcmp.c
  • src/system/libroot/posix/string/arch/ppc/Jamfile

     
    77MergeObject posix_string_arch_$(TARGET_ARCH).o :
    88    memcpy.c
    99    memset.c
     10    ffs.c
    1011;
  • src/system/libroot/posix/string/arch/m68k/Jamfile

     
    77MergeObject posix_string_arch_$(TARGET_ARCH).o :
    88    arch_string.S
    99    memset.c
     10    ffs.c
    1011;
  • src/system/libroot/posix/string/arch/x86/arch_string.S

     
    1616FUNCTION(memset):
    1717    jmp *(USER_COMMPAGE_ADDR + COMMPAGE_ENTRY_X86_MEMSET * 4)
    1818FUNCTION_END(memset)
     19
     20FUNCTION(ffs):
     21    jmp *(USER_COMMPAGE_ADDR + COMMPAGE_ENTRY_X86_FFS * 4)
     22FUNCTION_END(ffs)
  • src/system/libroot/posix/string/arch/generic/ffs.c

     
     1/*
     2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
     3 * Distributed under the terms of the MIT License.
     4 */
     5
     6#include <strings.h>
     7
     8#if !defined(ARCH_x86)
     9// find first (least significant) set bit
     10int
     11ffs(int value)
     12{
     13    if (!value)
     14        return 0;
     15
     16    // ToDo: This can certainly be optimized (e.g. by binary search). Or not
     17    // unlikely there's a single assembler instruction...
     18    for (int i = 1; i <= (int)sizeof(value) * 8; i++, value >>= 1) {
     19        if (value & 1)
     20            return i;
     21    }
     22
     23    // never gets here
     24    return 0;
     25}
     26#endif
  • src/system/libroot/posix/string/ffs.cpp

     
    1 /*
    2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
    3  * Distributed under the terms of the MIT License.
    4  */
    5 
    6 #include <strings.h>
    7 
    8 // find first (least significant) set bit
    9 int
    10 ffs(int value)
    11 {
    12     if (!value)
    13         return 0;
    14 
    15     // ToDo: This can certainly be optimized (e.g. by binary search). Or not
    16     // unlikely there's a single assembler instruction...
    17     for (int i = 1; i <= (int)sizeof(value) * 8; i++, value >>= 1) {
    18         if (value & 1)
    19             return i;
    20     }
    21 
    22     // never gets here
    23     return 0;
    24 }
  • headers/private/kernel/arch/x86/arch_cpu.h

     
    107107    void*   memcpy_end;
    108108    void    (*memset)(void* dest, int value, size_t count);
    109109    void*   memset_end;
     110    int     (*ffs)(int value);
     111    void*   ffs_end;
    110112} x86_optimized_functions;
    111113
    112114typedef struct x86_cpu_module_info {
  • headers/private/system/arch/x86/arch_commpage_defs.h

     
    1212#define COMMPAGE_ENTRY_X86_SYSCALL  (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 0)
    1313#define COMMPAGE_ENTRY_X86_MEMCPY   (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 1)
    1414#define COMMPAGE_ENTRY_X86_MEMSET   (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 2)
     15#define COMMPAGE_ENTRY_X86_FFS      (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 3)
    1516
    1617#define ARCH_USER_COMMPAGE_ADDR (0xffff0000)
    1718