← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
image_patchify_rgb8_bf16_checked.c
Go to the documentation of this file.
1#include "bf16_utils.h"
2
3int ck_image_patchify_rgb8_bf16_checked(const uint8_t *rgb, uint16_t *patches,
4 size_t rgb_elements, size_t patch_elements,
5 size_t height, size_t width, size_t patch)
6{
7 if (!height || height > 1024 || !width || width > 1024 ||
8 !patch || patch > 32 || height % patch || width % patch)
9 return -2;
10 const size_t elements = height * width * 3;
11 if (rgb_elements != elements || patch_elements != elements)
12 return -2;
13 const uintptr_t in = (uintptr_t)rgb, out = (uintptr_t)patches;
14 const size_t bytes = elements * sizeof(*patches);
15 if (!rgb || !patches || out % sizeof(*patches) ||
16 in > UINTPTR_MAX - elements || out > UINTPTR_MAX - bytes ||
17 (in < out + bytes && out < in + elements))
18 return -1;
19
20 uint16_t lut[256];
21 for (unsigned i = 0; i < 256; ++i) {
22 const float unit = (float)i / 255.0f;
23 lut[i] = float_to_bf16((unit - 0.5f) / 0.5f);
24 }
25 // Match the OIHW convolution weights: raster patches, then channel/y/x within a patch.
26 for (size_t y = 0; y < height; y += patch)
27 for (size_t x = 0; x < width; x += patch)
28 for (size_t c = 0; c < 3; ++c)
29 for (size_t dy = 0; dy < patch; ++dy)
30 for (size_t dx = 0; dx < patch; ++dx)
31 *patches++ = lut[rgb[((y + dy) * width + x + dx) * 3 + c]];
32 return 0;
33}
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
int ck_image_patchify_rgb8_bf16_checked(const uint8_t *rgb, uint16_t *patches, size_t rgb_elements, size_t patch_elements, size_t height, size_t width, size_t patch)