fix(newlib): use optimized misalign access functions from libc.a

This commit is contained in:
Alexey Lapshin
2025-05-01 13:35:20 +07:00
parent 0be466755b
commit e28af6a002
4 changed files with 42 additions and 24 deletions

View File

@@ -38,20 +38,23 @@ if(CONFIG_STDATOMIC_S32C1I_SPIRAM_WORKAROUND)
endif() endif()
if(CONFIG_LIBC_OPTIMIZED_MISALIGNED_ACCESS) if(CONFIG_LIBC_OPTIMIZED_MISALIGNED_ACCESS)
list(APPEND srcs if(CMAKE_C_COMPILER_ID MATCHES "Clang")
"src/string/memcmp.c" # TODO IDF-13089: remove this block and modify src/newlib.lf when clang was upgraded
"src/string/memmove.c" list(APPEND srcs
"src/string/strncmp.c" "src/string/memcmp.c"
"src/string/strncpy.c" "src/string/memmove.c"
"src/port/riscv/memcpy.c" "src/string/strncmp.c"
"src/port/riscv/strcpy.c" "src/string/strncpy.c"
"src/port/riscv/strcmp.S") "src/port/riscv/memcpy.c"
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_memcmp_impl") "src/port/riscv/strcpy.c"
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_memmove_impl") "src/port/riscv/strcmp.S")
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strncmp_impl") list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_memcmp_impl")
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strncpy_impl") list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_memmove_impl")
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strcpy_impl") list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strncmp_impl")
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strcmp_impl") list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strncpy_impl")
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strcpy_impl")
list(APPEND EXTRA_LINK_FLAGS "-u esp_libc_include_strcmp_impl")
endif()
endif() endif()
if(CONFIG_LIBC_NEWLIB) if(CONFIG_LIBC_NEWLIB)
@@ -69,12 +72,10 @@ endif()
set(ldfragments "") set(ldfragments "")
list(APPEND ldfragments "src/newlib.lf" "src/system_libs.lf") list(APPEND ldfragments "src/newlib.lf" "src/system_libs.lf")
if(CONFIG_SPIRAM_CACHE_WORKAROUND) if(CONFIG_LIBC_NEWLIB)
if(CONFIG_LIBC_NEWLIB) list(APPEND ldfragments src/libc.lf)
list(APPEND ldfragments src/esp32-spiram-rom-functions-c.lf) else()
else() list(APPEND ldfragments src/picolibc/libc.lf)
list(APPEND ldfragments src/picolibc/esp32-spiram-rom-functions-c.lf)
endif()
endif() endif()
idf_component_register(SRCS "${srcs}" idf_component_register(SRCS "${srcs}"

View File

@@ -3,10 +3,6 @@
# and/or applications may assume that because these functions normally are in ROM, they are accessible even when flash is # and/or applications may assume that because these functions normally are in ROM, they are accessible even when flash is
# inaccessible. To work around this, this ld fragment places these functions in RAM instead. If the ROM functions are used, # inaccessible. To work around this, this ld fragment places these functions in RAM instead. If the ROM functions are used,
# these defines do nothing, so they can still be included in that situation. # these defines do nothing, so they can still be included in that situation.
#
#
# Note: the only difference between esp32-spiram-rom-functions-c.lf
# and esp32-spiram-rom-functions-psram-workaround.lf is the archive name.
[mapping:libc] [mapping:libc]
archive: archive:
@@ -15,6 +11,8 @@ archive:
else: else:
libc.a libc.a
entries: entries:
if LIBC_OPTIMIZED_MISALIGNED_ACCESS = y:
libc_a-memcpy (noflash)
if SPIRAM_CACHE_WORKAROUND = y: if SPIRAM_CACHE_WORKAROUND = y:
# The following libs are either used in a lot of places or in critical # The following libs are either used in a lot of places or in critical
# code. (such as panic or abort) # code. (such as panic or abort)

View File

@@ -9,3 +9,18 @@ entries:
stdatomic (noflash) stdatomic (noflash)
if STDATOMIC_S32C1I_SPIRAM_WORKAROUND = y: if STDATOMIC_S32C1I_SPIRAM_WORKAROUND = y:
stdatomic_s32c1i (noflash) stdatomic_s32c1i (noflash)
if STDATOMIC_S32C1I_SPIRAM_WORKAROUND = y:
stdatomic_s32c1i (noflash)
if CONFIG_LIBC_OPTIMIZED_MISALIGNED_ACCESS = y && IDF_TOOLCHAIN_GCC = y:
# memcpy() should be in IRAM due to early system use
# Others can stay in flash (performance difference is minimal)
#
# Performance Comparison:
# | func | flash | iram |
# |---------|-------|-------|
# | memcmp | 15986 | 15170 |
# | strcpy | 17499 | 16660 |
# | strcmp | 13125 | 11147 |
# | strncpy | 17386 | 16668 |
# | strncmp | 22161 | 21782 |
libc_a-memcpy (noflash)

View File

@@ -8,6 +8,10 @@
archive: archive:
libc.a libc.a
entries: entries:
if LIBC_OPTIMIZED_MISALIGNED_ACCESS = y && IDF_TARGET_ARCH_RISCV = y:
memcpy-asm (noflash)
if LIBC_OPTIMIZED_MISALIGNED_ACCESS = y && IDF_TARGET_ARCH_XTENSA = y:
libc_machine_xtensa_memcpy (noflash)
if SPIRAM_CACHE_WORKAROUND = y: if SPIRAM_CACHE_WORKAROUND = y:
# The following libs are either used in a lot of places or in critical code. (such as panic or abort) # The following libs are either used in a lot of places or in critical code. (such as panic or abort)
# Thus, they shall always be placed in IRAM. # Thus, they shall always be placed in IRAM.