Skip to content

Commit

Permalink
signed 8-bit integer support
Browse files Browse the repository at this point in the history
  • Loading branch information
verstatx committed Oct 4, 2023
1 parent b59a1ae commit a6fe81d
Show file tree
Hide file tree
Showing 441 changed files with 1,155 additions and 161 deletions.
31 changes: 16 additions & 15 deletions include/af/arith.h
Original file line number Diff line number Diff line change
Expand Up @@ -904,21 +904,22 @@ extern "C" {
be performed by ArrayFire. The following table shows which casts will
be optimized out. outer -> inner -> outer
| inner-> | f32 | f64 | c32 | c64 | s32 | u32 | u8 | b8 | s64 | u64 | s16 | u16 | f16 |
|---------|-----|-----|-----|-----|-----|-----|----|----|-----|-----|-----|-----|-----|
| f32 | x | x | x | x | | | | | | | | | x |
| f64 | x | x | x | x | | | | | | | | | x |
| c32 | x | x | x | x | | | | | | | | | x |
| c64 | x | x | x | x | | | | | | | | | x |
| s32 | x | x | x | x | x | x | | | x | x | | | x |
| u32 | x | x | x | x | x | x | | | x | x | | | x |
| u8 | x | x | x | x | x | x | x | x | x | x | x | x | x |
| b8 | x | x | x | x | x | x | x | x | x | x | x | x | x |
| s64 | x | x | x | x | | | | | x | x | | | x |
| u64 | x | x | x | x | | | | | x | x | | | x |
| s16 | x | x | x | x | x | x | | | x | x | x | x | x |
| u16 | x | x | x | x | x | x | | | x | x | x | x | x |
| f16 | x | x | x | x | | | | | | | | | x |
| inner-> | f32 | f64 | c32 | c64 | s32 | u32 | s8 | u8 | b8 | s64 | u64 | s16 | u16 | f16 |
|---------|-----|-----|-----|-----|-----|-----|----|----|----|-----|-----|-----|-----|-----|
| f32 | x | x | x | x | | | | | | | | | | x |
| f64 | x | x | x | x | | | | | | | | | | x |
| c32 | x | x | x | x | | | | | | | | | | x |
| c64 | x | x | x | x | | | | | | | | | | x |
| s32 | x | x | x | x | x | x | | | | x | x | | | x |
| u32 | x | x | x | x | x | x | | | | x | x | | | x |
| s8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
| u8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
| b8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
| s64 | x | x | x | x | | | | | | x | x | | | x |
| u64 | x | x | x | x | | | | | | x | x | | | x |
| s16 | x | x | x | x | x | x | | | | x | x | x | x | x |
| u16 | x | x | x | x | x | x | | | | x | x | x | x | x |
| f16 | x | x | x | x | | | | | | | | | | x |
If you want to avoid this behavior use, af_eval after the first cast
operation. This will ensure that the cast operation is performed on the
Expand Down
43 changes: 26 additions & 17 deletions include/af/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace af
array_proxy& operator OP(const unsigned &a); \
array_proxy& operator OP(const bool &a); \
array_proxy& operator OP(const char &a); \
array_proxy& operator OP(const signed char &a); \
array_proxy& operator OP(const unsigned char &a); \
array_proxy& operator OP(const long &a); \
array_proxy& operator OP(const unsigned long &a); \
Expand Down Expand Up @@ -761,8 +762,8 @@ namespace af
bool isfloating() const;

/**
\brief Returns true if the array type is \ref u8, \ref b8, \ref s32
\ref u32, \ref s64, \ref u64, \ref s16, \ref u16
\brief Returns true if the array type is \ref s8, \ref u8, \ref b8,
\ref s32, \ref u32, \ref s64, \ref u64, \ref s16, \ref u16
*/
bool isinteger() const;

Expand Down Expand Up @@ -952,21 +953,22 @@ namespace af
/// and then back to f64, then the cast to f32 will be skipped and that
/// operation will *NOT* be performed by ArrayFire. The following table
/// shows which casts will be optimized out. outer -> inner -> outer
/// | inner-> | f32 | f64 | c32 | c64 | s32 | u32 | u8 | b8 | s64 | u64 | s16 | u16 | f16 |
/// |---------|-----|-----|-----|-----|-----|-----|----|----|-----|-----|-----|-----|-----|
/// | f32 | x | x | x | x | | | | | | | | | x |
/// | f64 | x | x | x | x | | | | | | | | | x |
/// | c32 | x | x | x | x | | | | | | | | | x |
/// | c64 | x | x | x | x | | | | | | | | | x |
/// | s32 | x | x | x | x | x | x | | | x | x | | | x |
/// | u32 | x | x | x | x | x | x | | | x | x | | | x |
/// | u8 | x | x | x | x | x | x | x | x | x | x | x | x | x |
/// | b8 | x | x | x | x | x | x | x | x | x | x | x | x | x |
/// | s64 | x | x | x | x | | | | | x | x | | | x |
/// | u64 | x | x | x | x | | | | | x | x | | | x |
/// | s16 | x | x | x | x | x | x | | | x | x | x | x | x |
/// | u16 | x | x | x | x | x | x | | | x | x | x | x | x |
/// | f16 | x | x | x | x | | | | | | | | | x |
/// | inner-> | f32 | f64 | c32 | c64 | s32 | u32 | s8 | u8 | b8 | s64 | u64 | s16 | u16 | f16 |
/// |---------|-----|-----|-----|-----|-----|-----|----|----|----|-----|-----|-----|-----|-----|
/// | f32 | x | x | x | x | | | | | | | | | | x |
/// | f64 | x | x | x | x | | | | | | | | | | x |
/// | c32 | x | x | x | x | | | | | | | | | | x |
/// | c64 | x | x | x | x | | | | | | | | | | x |
/// | s32 | x | x | x | x | x | x | | | | x | x | | | x |
/// | u32 | x | x | x | x | x | x | | | | x | x | | | x |
/// | s8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
/// | u8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
/// | b8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
/// | s64 | x | x | x | x | | | | | | x | x | | | x |
/// | u64 | x | x | x | x | | | | | | x | x | | | x |
/// | s16 | x | x | x | x | x | x | | | | x | x | x | x | x |
/// | u16 | x | x | x | x | x | x | | | | x | x | x | x | x |
/// | f16 | x | x | x | x | | | | | | | | | | x |
/// If you want to avoid this behavior use af_eval after the first cast
/// operation. This will ensure that the cast operation is performed on
/// the af::array
Expand Down Expand Up @@ -997,6 +999,7 @@ namespace af
array& OP2(const unsigned &val); /**< \copydoc OP2##(const array &) */ \
array& OP2(const bool &val); /**< \copydoc OP2##(const array &) */ \
array& OP2(const char &val); /**< \copydoc OP2##(const array &) */ \
array& OP2(const signed char &val); /**< \copydoc OP2##(const array &) */ \
array& OP2(const unsigned char &val); /**< \copydoc OP2##(const array &) */ \
array& OP2(const long &val); /**< \copydoc OP2##(const array &) */ \
array& OP2(const unsigned long &val); /**< \copydoc OP2##(const array &) */ \
Expand Down Expand Up @@ -1143,6 +1146,7 @@ namespace af
AFAPI array OP (const int& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const unsigned& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const char& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const signed char& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const unsigned char& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const long& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const unsigned long& lhs, const array& rhs); /**< \copydoc OP##(const array&, const array&) */ \
Expand All @@ -1156,6 +1160,7 @@ namespace af
AFAPI array OP (const array& lhs, const int& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const array& lhs, const unsigned& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const array& lhs, const char& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const array& lhs, const signed char& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const array& lhs, const unsigned char& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const array& lhs, const long& rhs); /**< \copydoc OP##(const array&, const array&) */ \
AFAPI array OP (const array& lhs, const unsigned long& rhs); /**< \copydoc OP##(const array&, const array&) */ \
Expand Down Expand Up @@ -1393,6 +1398,7 @@ namespace af
AFAPI array operator&(const array& lhs, const long long& rhs);
AFAPI array operator&(const array& lhs, const long& rhs);
AFAPI array operator&(const array& lhs, const short& rhs);
AFAPI array operator&(const array& lhs, const signed char& rhs);
AFAPI array operator&(const array& lhs, const unsigned char& rhs);
AFAPI array operator&(const array& lhs, const unsigned long long& rhs);
AFAPI array operator&(const array& lhs, const unsigned long& rhs);
Expand All @@ -1408,6 +1414,7 @@ namespace af
AFAPI array operator&(const long long& lhs, const array& rhs);
AFAPI array operator&(const long& lhs, const array& rhs);
AFAPI array operator&(const short& lhs, const array& rhs);
AFAPI array operator&(const signed char& lhs, const array& rhs);
AFAPI array operator&(const unsigned char& lhs, const array& rhs);
AFAPI array operator&(const unsigned long long& lhs, const array& rhs);
AFAPI array operator&(const unsigned long& lhs, const array& rhs);
Expand Down Expand Up @@ -1436,6 +1443,7 @@ namespace af
AFAPI array operator&&(const array& lhs, const long long& rhs);
AFAPI array operator&&(const array& lhs, const long& rhs);
AFAPI array operator&&(const array& lhs, const short& rhs);
AFAPI array operator&&(const array& lhs, const signed char& rhs);
AFAPI array operator&&(const array& lhs, const unsigned char& rhs);
AFAPI array operator&&(const array& lhs, const unsigned long long& rhs);
AFAPI array operator&&(const array& lhs, const unsigned long& rhs);
Expand All @@ -1451,6 +1459,7 @@ namespace af
AFAPI array operator&&(const long long& lhs, const array& rhs);
AFAPI array operator&&(const long& lhs, const array& rhs);
AFAPI array operator&&(const short& lhs, const array& rhs);
AFAPI array operator&&(const signed char& lhs, const array& rhs);
AFAPI array operator&&(const unsigned char& lhs, const array& rhs);
AFAPI array operator&&(const unsigned long long& lhs, const array& rhs);
AFAPI array operator&&(const unsigned long& lhs, const array& rhs);
Expand Down
1 change: 1 addition & 0 deletions include/af/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ typedef enum {
#if AF_API_VERSION >= 37
, f16 ///< 16-bit floating point value
#endif
, s8 ///< 8-bit signed integral value /// TODO AF_API_VERSION
} af_dtype;

typedef enum {
Expand Down
14 changes: 14 additions & 0 deletions include/af/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ namespace af
/// \ingroup device_func_half
AFAPI bool isHalfAvailable(const int device);

/// \brief Queries the current device for int4 support
///
/// \param[in] device the ID of the device to query
///
/// \returns true if the \p device supports int4 operations.
/// false otherwise
/// \ingroup device_func_int4
AFAPI bool isInt4Available(const int device);

/// \brief Sets the current device
///
/// \param[in] device The ID of the target device
Expand Down Expand Up @@ -338,6 +347,11 @@ extern "C" {
*/
AFAPI af_err af_get_half_support(bool *available, const int device);

/**
\ingroup device_func_int4
*/
AFAPI af_err af_get_int4_support(bool *available, const int device);

/**
\ingroup device_func_set
*/
Expand Down
10 changes: 10 additions & 0 deletions include/af/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ struct dtype_traits<half> {
static const char* getName() { return "half"; }
};
#endif

template<>
struct dtype_traits<signed char> {
enum {
af_type = s8 ,
ctype = f32
};
typedef signed char base_type;
static const char* getName() { return "schar"; }
};
}

#endif
1 change: 1 addition & 0 deletions src/api/c/anisotropic_diffusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ af_err af_anisotropic_diffusion(af_array* out, const af_array in,
case u32:
case s16:
case u16:
case s8:
case u8:
output = diffusion<float>(input, dt, K, iterations, F, eq);
break;
Expand Down
14 changes: 14 additions & 0 deletions src/api/c/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using detail::uchar;
using detail::uint;
using detail::uintl;
using detail::ushort;
using detail::schar;

af_err af_get_data_ptr(void *data, const af_array arr) {
try {
Expand All @@ -47,6 +48,7 @@ af_err af_get_data_ptr(void *data, const af_array arr) {
case b8: copyData(static_cast<char* >(data), arr); break;
case s32: copyData(static_cast<int* >(data), arr); break;
case u32: copyData(static_cast<unsigned*>(data), arr); break;
case s8: copyData(static_cast<schar* >(data), arr); break;
case u8: copyData(static_cast<uchar* >(data), arr); break;
case s64: copyData(static_cast<intl* >(data), arr); break;
case u64: copyData(static_cast<uintl* >(data), arr); break;
Expand Down Expand Up @@ -96,6 +98,9 @@ af_err af_create_array(af_array *result, const void *const data,
case u32:
out = createHandleFromData(d, static_cast<const uint *>(data));
break;
case s8:
out = createHandleFromData(d, static_cast<const schar *>(data));
break;
case u8:
out = createHandleFromData(d, static_cast<const uchar *>(data));
break;
Expand Down Expand Up @@ -175,6 +180,7 @@ af_err af_copy_array(af_array *out, const af_array in) {
case b8: res = copyArray<char>(in); break;
case s32: res = copyArray<int>(in); break;
case u32: res = copyArray<uint>(in); break;
case s8: res = copyArray<schar>(in); break;
case u8: res = copyArray<uchar>(in); break;
case s64: res = copyArray<intl>(in); break;
case u64: res = copyArray<uintl>(in); break;
Expand Down Expand Up @@ -205,6 +211,7 @@ af_err af_get_data_ref_count(int *use_count, const af_array in) {
case b8: res = getUseCount<char>(in); break;
case s32: res = getUseCount<int>(in); break;
case u32: res = getUseCount<uint>(in); break;
case s8: res = getUseCount<schar>(in); break;
case u8: res = getUseCount<uchar>(in); break;
case s64: res = getUseCount<intl>(in); break;
case u64: res = getUseCount<uintl>(in); break;
Expand Down Expand Up @@ -242,6 +249,7 @@ af_err af_release_array(af_array arr) {
case b8: releaseHandle<char>(arr); break;
case s32: releaseHandle<int>(arr); break;
case u32: releaseHandle<uint>(arr); break;
case s8: releaseHandle<schar>(arr); break;
case u8: releaseHandle<uchar>(arr); break;
case s64: releaseHandle<intl>(arr); break;
case u64: releaseHandle<uintl>(arr); break;
Expand Down Expand Up @@ -308,6 +316,9 @@ af_err af_write_array(af_array arr, const void *data, const size_t bytes,
case u32:
write_array(arr, static_cast<const uint *>(data), bytes, src);
break;
case s8:
write_array(arr, static_cast<const schar *>(data), bytes, src);
break;
case u8:
write_array(arr, static_cast<const uchar *>(data), bytes, src);
break;
Expand Down Expand Up @@ -433,6 +444,9 @@ af_err af_get_scalar(void *output_value, const af_array arr) {
case u32:
getScalar<uint>(reinterpret_cast<uint *>(output_value), arr);
break;
case s8:
getScalar<schar>(reinterpret_cast<schar *>(output_value), arr);
break;
case u8:
getScalar<uchar>(reinterpret_cast<uchar *>(output_value), arr);
break;
Expand Down
4 changes: 4 additions & 0 deletions src/api/c/assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ using detail::uchar;
using detail::uint;
using detail::uintl;
using detail::ushort;
using detail::schar;

template<typename Tout, typename Tin>
static void assign(Array<Tout>& out, const vector<af_seq> seqs,
Expand Down Expand Up @@ -122,6 +123,7 @@ static if_real<T> assign(Array<T>& out, const vector<af_seq> iv,
case u64: assign<T, uintl>(out, iv, getArray<uintl>(in)); break;
case s16: assign<T, short>(out, iv, getArray<short>(in)); break;
case u16: assign<T, ushort>(out, iv, getArray<ushort>(in)); break;
case s8: assign<T, schar>(out, iv, getArray<schar>(in)); break;
case u8: assign<T, uchar>(out, iv, getArray<uchar>(in)); break;
case b8: assign<T, char>(out, iv, getArray<char>(in)); break;
case f16: assign<T, half>(out, iv, getArray<half>(in)); break;
Expand Down Expand Up @@ -201,6 +203,7 @@ af_err af_assign_seq(af_array* out, const af_array lhs, const unsigned ndims,
case u64: assign(getArray<uintl>(res), inSeqs, rhs); break;
case s16: assign(getArray<short>(res), inSeqs, rhs); break;
case u16: assign(getArray<ushort>(res), inSeqs, rhs); break;
case s8: assign(getArray<schar>(res), inSeqs, rhs); break;
case u8: assign(getArray<uchar>(res), inSeqs, rhs); break;
case b8: assign(getArray<char>(res), inSeqs, rhs); break;
case f16: assign(getArray<half>(res), inSeqs, rhs); break;
Expand Down Expand Up @@ -385,6 +388,7 @@ af_err af_assign_gen(af_array* out, const af_array lhs, const dim_t ndims,
case s32: genAssign<int>(output, ptr, rhs); break;
case s16: genAssign<short>(output, ptr, rhs); break;
case u16: genAssign<ushort>(output, ptr, rhs); break;
case s8: genAssign<schar>(output, ptr, rhs); break;
case u8: genAssign<uchar>(output, ptr, rhs); break;
case b8: genAssign<char>(output, ptr, rhs); break;
case f16: genAssign<half>(output, ptr, rhs); break;
Expand Down

0 comments on commit a6fe81d

Please sign in to comment.