1 add_subdirectory(memory_utils)
3 if(LIBC_CONF_STRING_UNSAFE_WIDE_READ)
4 list(APPEND string_config_options "-DLIBC_COPT_STRING_UNSAFE_WIDE_READ")
6 if(string_config_options)
7 list(PREPEND string_config_options "COMPILE_OPTIONS")
15 .memory_utils.inline_bzero
16 .memory_utils.inline_memcpy
18 libc.src.__support.common
19 libc.src.__support.CPP.bitset
20 ${string_config_options}
24 allocating_string_utils
26 allocating_string_utils.h
28 .memory_utils.inline_memcpy
30 libc.src.__support.CPP.optional
31 libc.src.__support.macros.config
34 add_entrypoint_object(
42 add_entrypoint_object(
52 add_entrypoint_object(
60 add_entrypoint_object(
67 .memory_utils.inline_memcpy
70 add_entrypoint_object(
77 .memory_utils.inline_memmem
80 add_entrypoint_object(
90 add_entrypoint_object(
98 add_entrypoint_object(
108 add_entrypoint_object(
119 add_entrypoint_object(
126 .memory_utils.inline_bzero
129 add_entrypoint_object(
140 add_entrypoint_object(
150 add_entrypoint_object(
160 add_entrypoint_object(
167 .memory_utils.inline_strcmp
170 add_entrypoint_object(
177 .memory_utils.inline_strcmp
178 libc.src.__support.ctype_utils
181 add_entrypoint_object(
188 .memory_utils.inline_strstr
189 libc.src.__support.ctype_utils
192 add_entrypoint_object(
200 add_entrypoint_object(
207 .memory_utils.inline_memcpy
211 add_entrypoint_object(
221 add_entrypoint_object(
228 .memory_utils.inline_memcpy
234 add_entrypoint_object(
241 libc.src.__support.StringUtil.error_to_string
244 add_entrypoint_object(
251 libc.src.__support.StringUtil.error_to_string
254 add_entrypoint_object(
265 add_entrypoint_object(
276 add_entrypoint_object(
287 add_entrypoint_object(
298 add_entrypoint_object(
305 .memory_utils.inline_strcmp
308 add_entrypoint_object(
315 .memory_utils.inline_strcmp
316 libc.src.__support.ctype_utils
319 add_entrypoint_object(
327 add_entrypoint_object(
334 .memory_utils.inline_memcpy
337 libc.src.__support.CPP.new
340 add_entrypoint_object(
350 add_entrypoint_object(
360 add_entrypoint_object(
370 add_entrypoint_object(
380 add_entrypoint_object(
387 libc.src.__support.StringUtil.signal_to_string
390 add_entrypoint_object(
397 libc.src.__support.CPP.bitset
400 add_entrypoint_object(
407 .memory_utils.inline_strstr
410 add_entrypoint_object(
420 add_entrypoint_object(
430 add_entrypoint_object(
438 .memory_utils.inline_memcpy
441 # Helper to define a function with multiple implementations
442 # - Computes flags to satisfy required/rejected features and arch,
443 # - Declares an entry point,
444 # - Attach the REQUIRE_CPU_FEATURES property to the target,
445 # - Add the fully qualified target to `${name}_implementations` global property for tests.
446 function(add_implementation name impl_name)
447 cmake_parse_arguments(
449 "" # Optional arguments
450 "" # Single value arguments
451 "REQUIRE;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;MLLVM_COMPILE_OPTIONS" # Multi value arguments
454 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
455 # Note that '-mllvm' needs to be prefixed with 'SHELL:' to prevent CMake flag deduplication.
456 foreach(opt IN LISTS ADD_IMPL_MLLVM_COMPILE_OPTIONS)
457 list(APPEND ADD_IMPL_COMPILE_OPTIONS "SHELL:-mllvm ${opt}")
461 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
462 # Prevent warning when passing x86 SIMD types as template arguments.
463 # e.g. "warning: ignoring attributes on template argument ā__m128iā [-Wignored-attributes]"
464 list(APPEND ADD_IMPL_COMPILE_OPTIONS "-Wno-ignored-attributes")
467 add_entrypoint_object(${impl_name}
469 SRCS ${ADD_IMPL_SRCS}
470 HDRS ${ADD_IMPL_HDRS}
471 DEPENDS ${ADD_IMPL_DEPENDS}
472 COMPILE_OPTIONS -O3 ${ADD_IMPL_COMPILE_OPTIONS}
474 get_fq_target_name(${impl_name} fq_target_name)
475 set_target_properties(${fq_target_name} PROPERTIES REQUIRE_CPU_FEATURES "${ADD_IMPL_REQUIRE}")
476 set_property(GLOBAL APPEND PROPERTY "${name}_implementations" "${fq_target_name}")
479 # ------------------------------------------------------------------------------
481 # ------------------------------------------------------------------------------
483 function(add_bcmp bcmp_name)
484 add_implementation(bcmp ${bcmp_name}
485 SRCS ${LIBC_SOURCE_DIR}/src/string/bcmp.cpp
486 HDRS ${LIBC_SOURCE_DIR}/src/string/bcmp.h
488 .memory_utils.memory_utils
494 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
495 add_bcmp(bcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
496 add_bcmp(bcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
497 add_bcmp(bcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
498 add_bcmp(bcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
499 add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
501 elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
504 add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
508 # ------------------------------------------------------------------------------
510 # ------------------------------------------------------------------------------
512 function(add_bzero bzero_name)
513 add_implementation(bzero ${bzero_name}
514 SRCS ${LIBC_SOURCE_DIR}/src/string/bzero.cpp
515 HDRS ${LIBC_SOURCE_DIR}/src/string/bzero.h
517 .memory_utils.inline_memset
523 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
524 add_bzero(bzero_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
525 add_bzero(bzero_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
526 add_bzero(bzero_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
527 add_bzero(bzero_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
528 add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
530 elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
533 add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
537 # ------------------------------------------------------------------------------
539 # ------------------------------------------------------------------------------
541 function(add_memcmp memcmp_name)
542 add_implementation(memcmp ${memcmp_name}
543 SRCS ${LIBC_SOURCE_DIR}/src/string/memcmp.cpp
544 HDRS ${LIBC_SOURCE_DIR}/src/string/memcmp.h
546 .memory_utils.inline_memcmp
552 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
553 add_memcmp(memcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
554 add_memcmp(memcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
555 add_memcmp(memcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
556 add_memcmp(memcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
557 add_memcmp(memcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
559 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
560 add_memcmp(memcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
562 elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
565 add_memcmp(memcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
569 # ------------------------------------------------------------------------------
571 # ------------------------------------------------------------------------------
573 function(add_memcpy memcpy_name)
574 add_implementation(memcpy ${memcpy_name}
575 SRCS ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp
576 HDRS ${LIBC_SOURCE_DIR}/src/string/memcpy.h
578 .memory_utils.inline_memcpy
584 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
585 add_memcpy(memcpy_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
586 add_memcpy(memcpy_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
587 add_memcpy(memcpy_x86_64_opt_avx COMPILE_OPTIONS -march=sandybridge REQUIRE AVX)
588 add_memcpy(memcpy_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
589 add_memcpy(memcpy_x86_64_opt_sw_prefetch_sse4 COMPILE_OPTIONS -DLIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING -march=nehalem REQUIRE SSE4_2)
590 add_memcpy(memcpy_x86_64_opt_sw_prefetch_avx COMPILE_OPTIONS -DLIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING -march=sandybridge REQUIRE AVX)
591 add_memcpy(memcpy_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
593 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
594 # Disable tail merging as it leads to lower performance.
595 add_memcpy(memcpy_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
596 MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
597 add_memcpy(memcpy MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
598 elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
601 add_memcpy(memcpy_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
605 # ------------------------------------------------------------------------------
607 # ------------------------------------------------------------------------------
609 function(add_memmove memmove_name)
610 add_implementation(memmove ${memmove_name}
611 SRCS ${LIBC_SOURCE_DIR}/src/string/memmove.cpp
612 HDRS ${LIBC_SOURCE_DIR}/src/string/memmove.h
614 .memory_utils.inline_memcpy
620 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
621 add_memmove(memmove_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
622 add_memmove(memmove_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
623 add_memmove(memmove_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
624 add_memmove(memmove_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
625 add_memmove(memmove_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
627 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
628 # Disable tail merging as it leads to lower performance.
629 add_memmove(memmove_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
630 MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
631 add_memmove(memmove MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
632 elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
635 add_memmove(memmove_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
639 # ------------------------------------------------------------------------------
641 # ------------------------------------------------------------------------------
643 function(add_memset memset_name)
644 add_implementation(memset ${memset_name}
645 SRCS ${LIBC_SOURCE_DIR}/src/string/memset.cpp
646 HDRS ${LIBC_SOURCE_DIR}/src/string/memset.h
648 .memory_utils.inline_memset
654 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
655 add_memset(memset_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
656 add_memset(memset_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
657 add_memset(memset_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
658 add_memset(memset_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
659 add_memset(memset_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
661 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
662 # Disable tail merging as it leads to lower performance.
663 add_memset(memset_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
664 MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
665 add_memset(memset MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
666 elseif(LIBC_TARGET_ARCHITECTURE_IS_GPU)
669 add_memset(memset_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})