FastLED 3.9.15
Loading...
Searching...
No Matches
memcpy.h
Go to the documentation of this file.
1
3
4#pragma once
5
6// allow-include-after-namespace
7
9#include "fl/stl/bit_cast.h"
10#include "fl/stl/stdint.h"
11#include "fl/stl/cstddef.h"
12
14
15namespace fl {
16namespace isr {
17
22FASTLED_FORCE_INLINE bool is_aligned(const void* ptr, size_t alignment) {
23 return (fl::ptr_to_int(ptr) & (alignment - 1)) == 0;
24}
25
33 const u32* FL_RESTRICT_PARAM src,
34 size_t count) {
35 for (size_t i = 0; i < count; i++) {
36 dst[i] = src[i];
37 }
38}
39
47 const u16* FL_RESTRICT_PARAM src,
48 size_t count) {
49 for (size_t i = 0; i < count; i++) {
50 dst[i] = src[i];
51 }
52}
53
61 const u8* FL_RESTRICT_PARAM src,
62 size_t count) {
63 for (size_t i = 0; i < count; i++) {
64 dst[i] = src[i];
65 }
66}
67
76 const void* FL_RESTRICT_PARAM src,
77 size_t num_bytes) {
78 uintptr_t dst_addr = fl::ptr_to_int(dst);
79 uintptr_t src_addr = fl::ptr_to_int(src);
80
81 size_t align2 = (((dst_addr | src_addr | num_bytes) & 1) == 0);
82 size_t align4 = (((dst_addr | src_addr | num_bytes) & 3) == 0);
83
84 int index = align2 + align4;
85
86 switch (index) {
87 case 2:
88 memcpy_32(static_cast<u32*>(dst),
89 static_cast<const u32*>(src),
90 num_bytes >> 2);
91 break;
92 case 1:
93 memcpy_16(static_cast<u16*>(dst),
94 static_cast<const u16*>(src),
95 num_bytes >> 1);
96 break;
97 case 0:
98 default:
99 memcpy_byte(static_cast<u8*>(dst),
100 static_cast<const u8*>(src),
101 num_bytes);
102 break;
103 }
104}
105
110void memset_zero_byte(u8* dest, size_t count) {
111 for (size_t i = 0; i < count; i++) {
112 dest[i] = 0x0;
113 }
114}
115
121void memset_zero_word(u8* dest, size_t count) {
122 u32* dest32 = fl::bit_cast<u32*>(dest);
123 size_t count32 = count / 4;
124 size_t remainder = count % 4;
125
126 for (size_t i = 0; i < count32; i++) {
127 dest32[i] = 0;
128 }
129
130 if (remainder > 0) {
131 u8* remainder_ptr = dest + (count32 * 4);
132 memset_zero_byte(remainder_ptr, remainder);
133 }
134}
135
141void memset_zero(u8* dest, size_t count) {
142 uintptr_t address = fl::ptr_to_int(dest);
143
144 if ((address % 4 == 0) && (count >= 4)) {
145 memset_zero_word(dest, count);
146 } else {
147 memset_zero_byte(dest, count);
148 }
149}
150
151} // namespace isr
152} // namespace fl
153
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memcpy_16(u16 *FL_RESTRICT_PARAM dst, const u16 *FL_RESTRICT_PARAM src, size_t count)
ISR-optimized 16-bit block copy for 2-byte aligned memory.
Definition memcpy.h:46
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memset_zero_word(u8 *dest, size_t count)
ISR-safe word-aligned memset (4-byte writes)
Definition memcpy.h:121
FASTLED_FORCE_INLINE bool is_aligned(const void *ptr, size_t alignment)
Check if a pointer is aligned to a specific byte boundary.
Definition memcpy.h:22
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memcpy_byte(u8 *FL_RESTRICT_PARAM dst, const u8 *FL_RESTRICT_PARAM src, size_t count)
ISR-optimized byte copy.
Definition memcpy.h:60
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memset_zero_byte(u8 *dest, size_t count)
ISR-safe memset replacement (byte-by-byte zero)
Definition memcpy.h:110
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memcpy(void *FL_RESTRICT_PARAM dst, const void *FL_RESTRICT_PARAM src, size_t num_bytes)
ISR-optimized memcpy with alignment detection and switch dispatch.
Definition memcpy.h:75
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memcpy_32(u32 *FL_RESTRICT_PARAM dst, const u32 *FL_RESTRICT_PARAM src, size_t count)
ISR-optimized 32-bit block copy for 4-byte aligned memory.
Definition memcpy.h:32
FL_OPTIMIZE_FUNCTION FL_IRAM FASTLED_FORCE_INLINE void memset_zero(u8 *dest, size_t count)
ISR-safe memset with alignment optimization.
Definition memcpy.h:141
unsigned char u8
Definition stdint.h:131
uptr ptr_to_int(T *ptr) FL_NOEXCEPT
Definition bit_cast.h:71
fl::uptr uintptr_t
Definition s16x16x4.h:224
To bit_cast(const From &from) FL_NOEXCEPT
Definition bit_cast.h:48
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FASTLED_FORCE_INLINE
#define FL_OPTIMIZATION_LEVEL_O3_END
#define FL_OPTIMIZE_FUNCTION
#define FL_IRAM
#define FL_RESTRICT_PARAM