aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/common/datatypehelpers.pxd.in
blob: d14a3853e0880930fed148fad1093f36cc118d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
#
# AWL data types helper functions
#
# Copyright 2013-2018 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from awlsim.common.cython_support cimport *


cdef extern from "<endian.h>":		#@cy-posix
cdef extern from "endian-win.h":	#@cy-win
	enum: BYTE_ORDER
	enum: LITTLE_ENDIAN
	enum: BIG_ENDIAN

	uint16_t htobe16(uint16_t)
	uint16_t htole16(uint16_t)
	uint16_t be16toh(uint16_t)
	uint16_t le16toh(uint16_t)

	uint32_t htobe32(uint32_t)
	uint32_t htole32(uint32_t)
	uint32_t be32toh(uint32_t)
	uint32_t le32toh(uint32_t)

cdef extern from "<byteswap.h>":	#@cy-posix
cdef extern from "byteswap-win.h":	#@cy-win
	uint16_t bswap_16(uint16_t)
	uint32_t bswap_32(uint32_t)


cdef inline uint16_t swapEndianWord(uint16_t word):
	return bswap_16(word)

cdef inline uint32_t swapEndianDWord(uint32_t dword):
	return bswap_32(dword)


cdef inline int32_t byteToSignedPyInt(uint8_t byte):
	return <int32_t>(<int8_t>byte)

cdef inline int32_t wordToSignedPyInt(uint16_t word):
	return <int32_t>(<int16_t>word)

cdef inline int32_t dwordToSignedPyInt(uint32_t dword):
	return <int32_t>dword

cdef inline int64_t qwordToSignedPyInt(uint64_t qword):
	return <int64_t>qword


cdef union _floatCastUnion:
	float fvalue
	uint32_t value32

cdef uint32_t pyFloatToDWord(double pyfl)

cdef inline double dwordToPyFloat(uint32_t dword):
	cdef _floatCastUnion u
	u.value32 = dword
	return <double>(u.fvalue)


cdef class FloatConst(object):
	cdef public uint32_t minNormPosFloat32DWord
	cdef public double minNormPosFloat32

	cdef public uint32_t minNormNegFloat32DWord
	cdef public double minNormNegFloat32

	cdef public uint32_t maxNormNegFloat32DWord
	cdef public double maxNormNegFloat32

	cdef public uint32_t maxNormPosFloat32DWord
	cdef public double maxNormPosFloat32

	cdef public uint32_t posInfDWord
	cdef public double posInfFloat

	cdef public uint32_t negInfDWord
	cdef public double negInfFloat

	cdef public uint32_t pNaNDWord

	cdef public uint32_t nNaNDWord
	cdef public double nNaNFloat

	cdef public uint32_t negZeroDWord

	cdef public double epsilonFloat

cdef public FloatConst floatConst


cdef inline _Bool isNaN(uint32_t dword):
	return (dword & 0x7FFFFFFFu) > 0x7F800000u

cdef inline _Bool isInf(uint32_t dword):
	return (dword & 0x7FFFFFFFu) == 0x7F800000u

cdef inline _Bool isPosNegZero(uint32_t dword):
	return (dword & 0x7FFFFFFFu) == 0u

cdef inline _Bool isDenormalPyFloat(double pyfl):
	return ((pyfl > 0.0 and pyfl < floatConst.minNormPosFloat32) or
	        (pyfl < 0.0 and pyfl > floatConst.maxNormNegFloat32))

cdef inline _Bool pyFloatEqual(double pyfl0, double pyfl1):
	return abs(pyfl0 - pyfl1) < floatConst.epsilonFloat

cdef _Bool floatEqual(object fl0, object fl1)


cdef inline uint32_t roundUp(uint32_t n, uint32_t s):
	return ((n + s - 1u) // s) * s

cdef inline uint32_t intDivRoundUp(uint32_t n, uint32_t d):
	return (n + d - 1u) // d

cdef uint32_t getMSB(uint32_t value)


cdef inline _Bool isInteger(object value):
#@cy3	return isinstance(value, int)
#@cy2	return isinstance(value, (int, long))

cdef inline _Bool isString(object value):
#@cy3	return isinstance(value, str)
#@cy2	return isinstance(value, (unicode, str))
bues.ch cgit interface