Skip to content

Commit 3712a2d

Browse files
SeeForTwotensorflower-gardener
authored andcommitted
Fix macros for converting little endian to host for TF_TSRT_OFFSET GetSize
Make the macro that converts little endian data do nothing on little endian hosts, and byte swap otherwise. This only affects getting the size of TStrings of type "Offset". Added a test for TStrings of type "Offset" that checks if type and size are consistent. PiperOrigin-RevId: 400789721 Change-Id: I1398bffd842ab1631614b212b7c3a2af88d99538
1 parent 14e5329 commit 3712a2d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Diff for: tensorflow/core/platform/ctstring_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ static inline uint32_t TF_swap32(uint32_t host_int) {
6363
#endif
6464

6565
#if TF_TSTRING_LITTLE_ENDIAN
66-
#define TF_le32toh(x) TF_swap32(x)
67-
#else // TF_TSTRING_LITTLE_ENDIAN
6866
#define TF_le32toh(x) x
67+
#else // TF_TSTRING_LITTLE_ENDIAN
68+
#define TF_le32toh(x) TF_swap32(x)
6969
#endif // TF_TSTRING_LITTLE_ENDIAN
7070

7171
static inline size_t TF_align16(size_t i) { return (i + 0xF) & ~0xF; }

Diff for: tensorflow/core/platform/ctstring_test.cc

+27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
#include <memory>
1919
#include <string>
2020

21+
#include "tensorflow/core/platform/ctstring_internal.h"
2122
#include "tensorflow/core/platform/test.h"
2223

2324
static const char kLongString[] =
@@ -380,3 +381,29 @@ TEST(TF_CTStringTest, ResizeReserve) {
380381
TF_TString_Dealloc(&s70);
381382
}
382383
}
384+
385+
TEST(TF_CTStringTest, OffsetType) {
386+
{
387+
TF_TString s71;
388+
389+
TF_TString_Init(&s71);
390+
size_t header_length = 24;
391+
size_t size = 8;
392+
TF_TString_ResizeUninitialized(&s71, header_length + size);
393+
uint32_t save_size = s71.u.offset.size;
394+
uint32_t save_offset = s71.u.offset.offset;
395+
uint32_t save_count = s71.u.offset.count;
396+
397+
s71.u.offset.size = TF_TString_ToInternalSizeT(size, TF_TSTR_OFFSET);
398+
s71.u.offset.offset = header_length;
399+
s71.u.offset.count = 0;
400+
EXPECT_EQ(size, TF_TString_GetSize(&s71));
401+
EXPECT_EQ(TF_TSTR_OFFSET, TF_TString_GetType(&s71));
402+
403+
// restore state so string can be deallocated
404+
s71.u.offset.size = save_size;
405+
s71.u.offset.offset = save_offset;
406+
s71.u.offset.count = save_count;
407+
TF_TString_Dealloc(&s71);
408+
}
409+
}

0 commit comments

Comments
 (0)