datatypehelpers.pxd.in 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL data types helper functions
  4. #
  5. # Copyright 2013-2019 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. from awlsim.common.cython_support cimport *
  22. cdef extern from "<endian.h>": #@cy-posix
  23. cdef extern from "endian-win.h": #@cy-win
  24. enum: BYTE_ORDER
  25. enum: LITTLE_ENDIAN
  26. enum: BIG_ENDIAN
  27. uint16_t htobe16(uint16_t)
  28. uint16_t htole16(uint16_t)
  29. uint16_t be16toh(uint16_t)
  30. uint16_t le16toh(uint16_t)
  31. uint32_t htobe32(uint32_t)
  32. uint32_t htole32(uint32_t)
  33. uint32_t be32toh(uint32_t)
  34. uint32_t le32toh(uint32_t)
  35. cdef extern from "<byteswap.h>": #@cy-posix
  36. cdef extern from "byteswap-win.h": #@cy-win
  37. uint16_t bswap_16(uint16_t)
  38. uint32_t bswap_32(uint32_t)
  39. cdef inline uint16_t swapEndianWord(uint16_t word):
  40. return bswap_16(word)
  41. cdef inline uint32_t swapEndianDWord(uint32_t dword):
  42. return bswap_32(dword)
  43. cdef inline int32_t byteToSignedPyInt(uint8_t byte):
  44. return <int32_t>(<int8_t>byte)
  45. cdef inline int32_t wordToSignedPyInt(uint16_t word):
  46. return <int32_t>(<int16_t>word)
  47. cdef inline int32_t dwordToSignedPyInt(uint32_t dword):
  48. return <int32_t>dword
  49. cdef inline int64_t qwordToSignedPyInt(uint64_t qword):
  50. return <int64_t>qword
  51. cdef union _floatCastUnion:
  52. float fvalue
  53. uint32_t value32
  54. cdef uint32_t pyFloatToDWord(double pyfl)
  55. cdef inline double dwordToPyFloat(uint32_t dword):
  56. cdef _floatCastUnion u
  57. u.value32 = dword
  58. return <double>(u.fvalue)
  59. cdef class FloatConst(object):
  60. cdef public uint32_t minNormPosFloat32DWord
  61. cdef public double minNormPosFloat32
  62. cdef public uint32_t minNormNegFloat32DWord
  63. cdef public double minNormNegFloat32
  64. cdef public uint32_t maxNormNegFloat32DWord
  65. cdef public double maxNormNegFloat32
  66. cdef public uint32_t maxNormPosFloat32DWord
  67. cdef public double maxNormPosFloat32
  68. cdef public uint32_t posInfDWord
  69. cdef public double posInfFloat
  70. cdef public uint32_t negInfDWord
  71. cdef public double negInfFloat
  72. cdef public uint32_t pNaNDWord
  73. cdef public uint32_t nNaNDWord
  74. cdef public double nNaNFloat
  75. cdef public uint32_t negZeroDWord
  76. cdef public double epsilonFloat
  77. cdef public FloatConst floatConst
  78. cdef inline _Bool isNaN(uint32_t dword):
  79. return (dword & 0x7FFFFFFFu) > 0x7F800000u
  80. cdef inline _Bool isInf(uint32_t dword):
  81. return (dword & 0x7FFFFFFFu) == 0x7F800000u
  82. cdef inline _Bool isPosNegZero(uint32_t dword):
  83. return (dword & 0x7FFFFFFFu) == 0u
  84. cdef inline _Bool isDenormalPyFloat(double pyfl):
  85. return ((pyfl > 0.0 and pyfl < floatConst.minNormPosFloat32) or
  86. (pyfl < 0.0 and pyfl > floatConst.maxNormNegFloat32))
  87. cdef inline _Bool pyFloatEqual(double pyfl0, double pyfl1):
  88. return abs(pyfl0 - pyfl1) < floatConst.epsilonFloat
  89. cdef _Bool floatEqual(object fl0, object fl1)
  90. cdef inline uint32_t roundUp(uint32_t n, uint32_t s):
  91. return ((n + s - 1u) // s) * s
  92. cdef inline uint32_t intDivRoundUp(uint32_t n, uint32_t d):
  93. return (n + d - 1u) // d
  94. cdef uint32_t getMSB(uint32_t value)
  95. cdef inline _Bool isInteger(object value):
  96. #@cy3 return isinstance(value, int)
  97. #@cy2 return isinstance(value, (int, long))
  98. cdef inline _Bool isString(object value):
  99. #@cy3 return isinstance(value, str)
  100. #@cy2 return isinstance(value, (unicode, str))
  101. cdef inline uint32_t len_u32(object obj):
  102. return <uint32_t>(min(len(obj), <object>0xFFFFFFFFu))
  103. cdef inline uint16_t len_u16(object obj):
  104. return <uint16_t>(min(len(obj), <object>0xFFFFu))
  105. cdef inline uint8_t len_u8(object obj):
  106. return <uint8_t>(min(len(obj), <object>0xFFu))
  107. cdef inline int32_t len_s32(object obj):
  108. return <int32_t>(min(len(obj), <object>0x7FFFFFFFu))
  109. cdef inline int16_t len_s16(object obj):
  110. return <int16_t>(min(len(obj), <object>0x7FFFu))
  111. cdef inline int8_t len_s8(object obj):
  112. return <int8_t>(min(len(obj), <object>0x7Fu))
  113. cdef inline int32_t u32_to_s32(uint32_t value):
  114. return <int32_t>(min(value, 0x7FFFFFFFu))
  115. cdef inline int16_t u32_to_s16(uint32_t value):
  116. return <int16_t>(min(value, 0x7FFFu))
  117. cdef inline int8_t u32_to_s8(uint32_t value):
  118. return <int8_t>(min(value, 0x7Fu))
  119. cdef inline uint32_t s32_to_u32(int32_t value):
  120. return <uint32_t>(max(value, 0))
  121. cdef inline uint16_t s32_to_u16(int32_t value):
  122. return <uint16_t>(min(max(value, 0), 0xFFFF))
  123. cdef inline uint8_t s32_to_u8(int32_t value):
  124. return <uint8_t>(min(max(value, 0), 0xFF))