summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/evenmore/evenmore-bitwise.tex
blob: 030229413de4f47fd6e0e7b7bcc54abb22616783 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
% language=us runpath=texruns:manuals/evenmore

\environment evenmore-style

\startcomponent evenmore-bitwise

\startchapter[title={Bitwise operations}]

Occasionally we use a bit set to pass (or store) options and one way of doing
that is to add up some constants. However, what you would like to do is to set a
specific bit. Of course one can write macros for that but performance wise one
can wonder if there are other ways. One solution is to extend the engine but that
has its own pitfalls. For instance, I played with additions to \type {\numexpr}
and although they worked okay and brought now performance degradation, I decided
to remove that experiment. One problem is that we have no real 32 bit cardinals
(unsigned integers) in \TEX\ and the engine makes sure that we never exceed the
minima and maxima. Another problem is that in expressions we then need either
verbose \type {and}, \type {or}, \type {xor}, \type {not} and other verbose
operators, if only because the usual symbols can have catcodes that are
unsuitable.

So in the end I decided to come up with a set of primitive like commands that
do the job. It is no problem to have a set of dedicated verbose commands and we
can extend the repertoire if needed. So this is what we have now:

\starttabulate[||i2c||]
\NC command                    \NC operator equivalent  \NC optional     \NC \NR
\NC \type {\bitwiseset      a} \NC \type {a}            \NC              \NC \NR
\NC \type {\bitwisenot      a} \NC \type {~a}           \NC              \NC \NR
\NC \type {\bitwisenil   a  b} \NC \type {a & (~ b)}    \NC \type {with} \NC \NR
\NC \type {\bitwiseand   a  b} \NC \type {a & b}        \NC \type {with} \NC \NR
\NC \type {\bitwiseor    a  b} \NC \type {a | b}        \NC \type {with} \NC \NR
\NC \type {\bitwisexor   a  b} \NC \type {a ~ b}        \NC \type {with} \NC \NR
\NC \type {\bitwiseshift a  b} \NC \type {a >> b}       \NC \type {by}   \NC \NR
\NC \type {\bitwiseshift a -b} \NC \type {a << b}       \NC \type {by}   \NC \NR
\NC \type {\ifbitwiseand a  b} \NC \type {(a & b) ~= 0} \NC              \NC \NR
\stoptabulate

Here a some examples:

\startbuffer
\scratchcounter = \bitwiseand "01      "02 \uchexnumbers{\scratchcounter} \quad
\scratchcounter = \bitwiseand "01 with "02 \uchexnumbers{\scratchcounter} \quad
\scratchcounter = \bitwiseand "03      "02 \uchexnumbers{\scratchcounter} \qquad
\scratchcounter = \bitwiseor  "01      "02 \uchexnumbers{\scratchcounter} \quad
\scratchcounter = \bitwiseor  "01 with "02 \uchexnumbers{\scratchcounter} \quad
\scratchcounter = \bitwiseor  "03      "02 \uchexnumbers{\scratchcounter} \qquad
\scratchcounter = \bitwisexor "01      "02 \uchexnumbers{\scratchcounter} \quad
\scratchcounter = \bitwisexor "01 with "02 \uchexnumbers{\scratchcounter} \quad
\scratchcounter = \bitwisexor "03      "02 \uchexnumbers{\scratchcounter}
\stopbuffer

\typebuffer

This gives the nine values:

{\tt\getbuffer}

Because they are numeric operations you can chain them, as in:

\starttyping
\scratchcounter = \bitwisenil \bitwisenil "0F      "02      "01 \relax
\scratchcounter = \bitwisenil \bitwisenil "0F with "02 with "01 \relax
\stoptyping

We try as good as possible to support all bits in the range from zero upto \type
{0xFFFFFFFF};

\startbuffer
\scratchcounter \bitwiseset "FFFFFFFF

\ifbitwiseand \scratchcounter "80000000 YES \else NOP \fi
\ifbitwiseand \scratchcounter "F0000000 YES \else NOP \fi
\ifbitwiseand \scratchcounter "10000000 YES \else NOP \fi

\scratchcounter \bitwisenot \scratchcounter

\ifbitwiseand \scratchcounter "80000000 YES \else NOP \fi
\ifbitwiseand \scratchcounter "F0000000 YES \else NOP \fi
\ifbitwiseand \scratchcounter "10000000 YES \else NOP \fi
\stopbuffer

\typebuffer

and we get:

\startpacked \tt \getbuffer \stoppacked

Of course you can just use normal counters and \TEX\ integers but using the bit
commands have the advantage that they do some checking and can do real \type {or}
etc operations. Here is some food for thought:

\startbuffer
\scratchcounter \bitwiseand "01 "02
\scratchcounter \numexpr    "01+"02\relax

\ifcase      \bitwiseand \scratchcounterone \plusone \else \fi
\ifbitwiseand            \scratchcounterone \plusone \else \fi
\ifnum                   \scratchcounterone=\plusone \else \fi
\stopbuffer

\typebuffer

and we get:

\startpacked \tt \getbuffer \stoppacked

You can also go real binary, but we only provide a combined setter|/|getter for
that, but you can mix that one with the other commands:

\startbuffer
\scratchcounterone   = \bitwise 1101
\scratchcountertwo   = \bitwise 11011101110111011101110111011101
\scratchcounterthree = \bitwiseor \bitwise 0001 \bitwise 1100

{0x\uchexnumber{\scratchcounterone}   \bitwise\scratchcounterone  }\par
{0x\uchexnumber{\scratchcountertwo}   \bitwise\scratchcountertwo  }\par
{0x\uchexnumber{\scratchcounterthree} \bitwise\scratchcounterthree}\par
\stopbuffer

\typebuffer

We get bits back:

\startpacked \tt \getbuffer \stoppacked

The above commands are typical for the things we can do with \LUAMETATEX\ and
\LMTX\ and are unlikely to become available in \MKIV.

There is a special command for (re)setting a bit in a register:

\startbuffer
             \scratchcounter  0
\bitwiseflip \scratchcounter  1 [\the\scratchcounter]
\bitwiseflip \scratchcounter  4 [\the\scratchcounter]
\bitwiseflip \scratchcounter  8 [\the\scratchcounter]
\bitwiseflip \scratchcounter -5 [\the\scratchcounter]
\stopbuffer

\typebuffer

This gives: \inlinebuffer. Of course a global assignment works too:

\startbuffer
 \global              \globalscratchcounter  0
{\global \bitwiseflip \globalscratchcounter  2 } [\the\globalscratchcounter]
{\global \bitwiseflip \globalscratchcounter  4 } [\the\globalscratchcounter]
{\global \bitwiseflip \globalscratchcounter  8 } [\the\globalscratchcounter]
{\global \bitwiseflip \globalscratchcounter -6 } [\the\globalscratchcounter]
\stopbuffer

\typebuffer

Here we get: \inlinebuffer. A side effect of it being an number makes that
this is also valid:

\starttyping
\scratchcounterone\bitwiseflip \scratchcountertwo -16
\stoptyping

\stopchapter

\stopcomponent

% (\scratchcounterone \bitwiseset "F        \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FF       \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FFF      \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FFFF     \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FFFFF    \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FFFFFF   \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FFFFFFF  \uchexnumber{\scratchcounterone})\par
% (\scratchcounterone \bitwiseset "FFFFFFFF \uchexnumber{\scratchcounterone})\par
%
% (\scratchcounterone \bitwiseset       "0000FFFF
%  \scratchcounterone \bitwiseshift     \scratchcounterone  -16
%  \uchexnumber{\scratchcounterone})\par
%
% \scratchcounterone \bitwiseset "FFFFFFFF
% \scratchcountertwo \bitwiseset "FFFFFFFE
%
% \the\scratchcounterone : \uchexnumber{\scratchcounterone}\par
% \the\scratchcountertwo : \uchexnumber{\scratchcountertwo}\par

% I need to check this on the garden run as it looks like that server is some 50%
% faster than my (in terms of computers) old laptop.

% \testfeatureonce{10000}{\scratchcounter \bitwiseand "01 "02      }                \elapsedtime\par
% \testfeatureonce{10000}{\scratchcounter \numexpr    "01+"02\relax}                \elapsedtime\par
% \testfeatureonce{10000}{\ifcase\bitwiseand \scratchcounterone \plusone \else \fi} \elapsedtime\par
% \testfeatureonce{10000}{\ifbitwiseand      \scratchcounterone \plusone \else \fi} \elapsedtime\par
% %testfeatureonce{10000}{\ifnum             \scratchcounterone=\plusone \else \fi} \elapsedtime\par
%
% \testfeatureonce{100000}{\scratchcounter = \bitwise 1101 } \elapsedtime\par
% \testfeatureonce{100000}{\scratchcounter = \bitwise 11011101110111011101110111011101 } \elapsedtime\par