Skip to content

Commit

Permalink
lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Jan 15, 2025
1 parent d1d02e7 commit 221a6f1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
29 changes: 16 additions & 13 deletions cpp/fury/python/pyunicode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

namespace fury {

static PyObject* unicode_latin1[256] = {nullptr};
static PyObject *unicode_latin1[256] = {nullptr};

static PyObject* get_latin1_char(unsigned char ch) {
PyObject* unicode = unicode_latin1[ch];
static PyObject *get_latin1_char(unsigned char ch) {
PyObject *unicode = unicode_latin1[ch];
if (!unicode) {
unicode = PyUnicode_New(1, ch);
if (!unicode) return NULL;
if (!unicode)
return NULL;
PyUnicode_1BYTE_DATA(unicode)[0] = ch;
// assert(_PyUnicode_CheckConsistency(unicode, 1));
unicode_latin1[ch] = unicode;
Expand All @@ -36,21 +37,23 @@ static PyObject* get_latin1_char(unsigned char ch) {
return unicode;
}

PyObject* Fury_PyUnicode_FromUCS1(const char* u, Py_ssize_t size) {
PyObject* res;
PyObject *Fury_PyUnicode_FromUCS1(const char *u, Py_ssize_t size) {
PyObject *res;
unsigned char max_char;
FURY_CHECK(size > 0);
if (size == 1) return get_latin1_char(u[0]);
max_char = isAscii(reinterpret_cast<const char*>(u), size) ? 127 : 255;
if (size == 1)
return get_latin1_char(u[0]);
max_char = isAscii(reinterpret_cast<const char *>(u), size) ? 127 : 255;
res = PyUnicode_New(size, max_char);
if (!res) return NULL;
if (!res)
return NULL;
memcpy(PyUnicode_1BYTE_DATA(res), u, size);
// assert(_PyUnicode_CheckConsistency(res, 1));
return res;
}

PyObject* Fury_PyUnicode_FromUCS2(const uint16_t* u, Py_ssize_t size) {
PyObject* res;
PyObject *Fury_PyUnicode_FromUCS2(const uint16_t *u, Py_ssize_t size) {
PyObject *res;
Py_UCS2 max_char;
FURY_CHECK(size > 0);
if (size == 1) {
Expand Down Expand Up @@ -79,9 +82,9 @@ PyObject* Fury_PyUnicode_FromUCS2(const uint16_t* u, Py_ssize_t size) {
if (max_char >= 256) {
memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2) * size);
} else {
copyArray(u, PyUnicode_1BYTE_DATA(res), size);
copyArray(u, PyUnicode_1BYTE_DATA(res), size);
}
// assert(_PyUnicode_CheckConsistency(res, 1));
return res;
}
} // namespace fury
} // namespace fury
12 changes: 6 additions & 6 deletions cpp/fury/python/pyunicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
* under the License.
*/

#include <string>
#include <cstring>
#include "fury/util/array_util.h"
#include "fury/util/buffer.h"
#include "fury/util/logging.h"
#include "fury/util/string_util.h"
#include "pyport.h"
#include "object.h"
#include "pyport.h"
#include "unicodeobject.h"
#include <cstring>
#include <string>

namespace fury {

// unicodeobject.c
PyObject* Fury_PyUnicode_FromUCS1(const char* u, Py_ssize_t size);
PyObject *Fury_PyUnicode_FromUCS1(const char *u, Py_ssize_t size);

PyObject* Fury_PyUnicode_FromUCS2(const uint16_t* u, Py_ssize_t size);
PyObject *Fury_PyUnicode_FromUCS2(const uint16_t *u, Py_ssize_t size);

} // namespace fury
} // namespace fury
40 changes: 20 additions & 20 deletions cpp/fury/util/array_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
*/

#pragma once
#include <cstdint>
#include "fury/util/platform.h"
#include <cstdint>

namespace fury {
#if defined(FURY_HAS_NEON)
inline uint16_t getMaxValue(const uint16_t* arr, size_t length) {
inline uint16_t getMaxValue(const uint16_t *arr, size_t length) {
if (length == 0) {
return 0; // Return 0 for empty arrays
return 0; // Return 0 for empty arrays
}
uint16x8_t max_val = vdupq_n_u16(0); // Initialize max vector to zero
uint16x8_t max_val = vdupq_n_u16(0); // Initialize max vector to zero

size_t i = 0;
for (; i + 8 <= length; i += 8) {
uint16x8_t current_val = vld1q_u16(&arr[i]);
max_val = vmaxq_u16(max_val, current_val); // Max operation
max_val = vmaxq_u16(max_val, current_val); // Max operation
}

// Find the max value in the resulting vector
Expand All @@ -54,7 +54,7 @@ inline uint16_t getMaxValue(const uint16_t* arr, size_t length) {
return max_neon;
}

inline void copyArray(const uint16_t* from, uint8_t* to, size_t length) {
inline void copyArray(const uint16_t *from, uint8_t *to, size_t length) {
size_t i = 0;
for (; i + 7 < length; i += 8) {
uint16x8_t src = vld1q_u16(&from[i]);
Expand All @@ -68,22 +68,22 @@ inline void copyArray(const uint16_t* from, uint8_t* to, size_t length) {
}
}
#elif defined(FURY_HAS_SSE2)
inline uint16_t getMaxValue(const uint16_t* arr, size_t length) {
inline uint16_t getMaxValue(const uint16_t *arr, size_t length) {
if (length == 0) {
return 0; // Return 0 for empty arrays
return 0; // Return 0 for empty arrays
}

__m128i max_val = _mm_setzero_si128(); // Initialize max vector with zeros
__m128i max_val = _mm_setzero_si128(); // Initialize max vector with zeros

size_t i = 0;
for (; i + 8 <= length; i += 8) {
__m128i current_val = _mm_loadu_si128((__m128i*)&arr[i]);
max_val = _mm_max_epu16(max_val, current_val); // Max operation
__m128i current_val = _mm_loadu_si128((__m128i *)&arr[i]);
max_val = _mm_max_epu16(max_val, current_val); // Max operation
}

// Find the max value in the resulting vector
uint16_t temp[8];
_mm_storeu_si128((__m128i*)temp, max_val);
_mm_storeu_si128((__m128i *)temp, max_val);
uint16_t max_sse = temp[0];
for (int j = 1; j < 8; j++) {
if (temp[j] > max_sse) {
Expand All @@ -100,13 +100,13 @@ inline uint16_t getMaxValue(const uint16_t* arr, size_t length) {
return max_sse;
}

inline void copyArray(const uint16_t* from, uint8_t* to, size_t length) {
inline void copyArray(const uint16_t *from, uint8_t *to, size_t length) {
size_t i = 0;
__m128i mask = _mm_set1_epi16(0xFF); // Mask to zero out the high byte
__m128i mask = _mm_set1_epi16(0xFF); // Mask to zero out the high byte
for (; i + 7 < length; i += 8) {
__m128i src = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&from[i]));
__m128i src = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&from[i]));
__m128i result = _mm_and_si128(src, mask);
_mm_storel_epi64(reinterpret_cast<__m128i*>(&to[i]),
_mm_storel_epi64(reinterpret_cast<__m128i *>(&to[i]),
_mm_packus_epi16(result, result));
}

Expand All @@ -116,9 +116,9 @@ inline void copyArray(const uint16_t* from, uint8_t* to, size_t length) {
}
}
#else
inline uint16_t getMaxValue(const uint16_t* arr, size_t length) {
inline uint16_t getMaxValue(const uint16_t *arr, size_t length) {
if (length == 0) {
return 0; // Return 0 for empty arrays
return 0; // Return 0 for empty arrays
}
uint16_t max_val = arr[0];
for (size_t i = 1; i < length; i++) {
Expand All @@ -129,11 +129,11 @@ inline uint16_t getMaxValue(const uint16_t* arr, size_t length) {
return max_val;
}

inline void copyArray(const uint16_t* from, uint8_t* to, size_t length) {
inline void copyArray(const uint16_t *from, uint8_t *to, size_t length) {
// Fallback for systems without SSE2/NEON
for (size_t i = 0; i < length; ++i) {
to[i] = static_cast<uint8_t>(from[i]);
}
}
#endif
} // namespace fury
} // namespace fury

0 comments on commit 221a6f1

Please sign in to comment.