Skip to content

Commit 19cfe14

Browse files
jan-wassenbergcopybara-github
authored andcommitted
Warning fixes (casts) and fix Windows build for aligned_alloc
PiperOrigin-RevId: 689734618
1 parent 52af531 commit 19cfe14

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

compression/blob_store.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ BlobError BlobReader::ReadAll(hwy::ThreadPool& pool) {
275275
[pfile, &requests, &err](uint64_t i, size_t /*thread*/) {
276276
if (!pfile->Read(requests[i].offset, requests[i].size,
277277
requests[i].data)) {
278-
fprintf(stderr, "Failed to read blob %zu\n", i);
278+
fprintf(stderr, "Failed to read blob %zu\n",
279+
static_cast<size_t>(i));
279280
err.test_and_set();
280281
}
281282
});

paligemma/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cc_library(
1414
deps = [
1515
"//compression:io",
1616
"@highway//:hwy",
17+
"@highway//:profiler",
1718
],
1819
)
1920

paligemma/image.cc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// limitations under the License.
1515

1616
#include "paligemma/image.h"
17-
#include "compression/io.h"
1817

18+
#include <stddef.h>
1919
#include <stdint.h>
2020

2121
#include <algorithm>
@@ -28,7 +28,10 @@
2828
#include <utility>
2929
#include <vector>
3030

31+
#include "compression/io.h"
32+
#include "hwy/aligned_allocator.h" // hwy::Span
3133
#include "hwy/base.h"
34+
#include "hwy/profiler.h"
3235

3336
namespace gcpp {
3437
namespace {
@@ -95,12 +98,12 @@ bool Image::ReadPPM(const std::string& filename) {
9598
std::cerr << filename << " does not exist\n";
9699
return false;
97100
}
98-
auto content = ReadFileToString(path);
101+
const std::string content = ReadFileToString(path);
99102
return ReadPPM(hwy::Span<const char>(content.data(), content.size()));
100103
}
101104

102105
bool Image::ReadPPM(const hwy::Span<const char>& buf) {
103-
auto pos = CheckP6Format(buf.cbegin(), buf.cend());
106+
const char* pos = CheckP6Format(buf.cbegin(), buf.cend());
104107
if (!pos) {
105108
std::cerr << "We only support binary PPM (P6)\n";
106109
return false;
@@ -134,8 +137,8 @@ bool Image::ReadPPM(const hwy::Span<const char>& buf) {
134137
return false;
135138
}
136139
++pos;
137-
auto data_size = width * height * 3;
138-
if (buf.cend() - pos < data_size) {
140+
const size_t data_size = width * height * 3;
141+
if (buf.cend() - pos < static_cast<ptrdiff_t>(data_size)) {
139142
std::cerr << "Insufficient data remaining\n";
140143
return false;
141144
}
@@ -190,28 +193,30 @@ bool Image::WriteBinary(const std::string& filename) const {
190193
// We want the N-th patch (of 256) of size kPatchSize x kPatchSize x 3.
191194
// Patches are numbered in usual "pixel-order".
192195
void Image::GetPatch(size_t patch_num, float* patch) const {
196+
PROFILER_FUNC;
193197
constexpr size_t kDataSize = kImageSize * kImageSize * 3;
194198
HWY_ASSERT(size() == kDataSize);
195199
constexpr size_t kPatchDataSize = kPatchSize * kPatchSize * 3;
196-
int i_offs = patch_num / kNumPatches;
197-
int j_offs = patch_num % kNumPatches;
200+
size_t i_offs = patch_num / kNumPatches;
201+
size_t j_offs = patch_num % kNumPatches;
198202
HWY_ASSERT(0 <= i_offs && i_offs < kNumPatches);
199203
HWY_ASSERT(0 <= j_offs && j_offs < kNumPatches);
200204
i_offs *= kPatchSize;
201205
j_offs *= kPatchSize;
202206
// This can be made faster, but let's first see whether it matters.
203207
const float* image_data = data();
204-
for (int i = 0; i < kPatchSize; ++i) {
205-
for (int j = 0; j < kPatchSize; ++j) {
206-
for (int k = 0; k < 3; ++k) {
207-
const int patch_index = (i * kPatchSize + j) * 3 + k;
208+
for (size_t i = 0; i < kPatchSize; ++i) {
209+
for (size_t j = 0; j < kPatchSize; ++j) {
210+
for (size_t k = 0; k < 3; ++k) {
211+
const size_t patch_index = (i * kPatchSize + j) * 3 + k;
208212
HWY_ASSERT(patch_index < kPatchDataSize);
209-
const int image_index =
213+
const size_t image_index =
210214
((i + i_offs) * kImageSize + (j + j_offs)) * 3 + k;
211215
HWY_ASSERT(image_index < kDataSize);
212216
patch[patch_index] = image_data[image_index];
213217
}
214218
}
215219
}
216220
}
221+
217222
} // namespace gcpp

util/allocator.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <stddef.h>
2020
#include <stdint.h>
2121

22-
#include <cstdlib> // std::aligned_alloc
22+
#include <cstdlib> // std::aligned_alloc / _aligned_malloc
2323

2424
// IWYU pragma: begin_exports
2525
#include "util/threading.h"
@@ -140,15 +140,19 @@ class Allocator {
140140
}
141141

142142
// AlignedFreeUniquePtr has a deleter that can call an arbitrary `free`, but
143-
// with an extra opaque pointer, which we discard via this adapter.
143+
// with an extra opaque pointer, which we discard via `call_free`.
144+
#if defined(__ANDROID_API__) && __ANDROID_API__ < 28
144145
const auto call_free = [](void* ptr, void*) { std::free(ptr); };
145-
#if !defined(__ANDROID_API__) || __ANDROID_API__ >= 28
146-
T* p = static_cast<T*>(std::aligned_alloc(Alignment(), bytes));
147-
#else
148146
void* mem = nullptr;
149147
int err = posix_memalign(&mem, Alignment(), bytes);
150148
HWY_ASSERT(err == 0);
151149
T* p = static_cast<T*>(mem);
150+
#elif HWY_OS_WIN
151+
const auto call_free = [](void* ptr, void*) { _aligned_free(ptr); };
152+
T* p = static_cast<T*>(_aligned_malloc(bytes, Alignment()));
153+
#else
154+
const auto call_free = [](void* ptr, void*) { std::free(ptr); };
155+
T* p = static_cast<T*>(std::aligned_alloc(Alignment(), bytes));
152156
#endif
153157
return hwy::AlignedFreeUniquePtr<T[]>(
154158
p, hwy::AlignedFreer(call_free, nullptr));

0 commit comments

Comments
 (0)