forked from B-Lang-org/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrelude.tex
7293 lines (6297 loc) · 190 KB
/
Prelude.tex
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\subsection{Type classes}
\index{type classes}
A type class groups related functions and operators and allows for
instances across the various datatypes which are members of the typeclass.
Hence the function
names within a type class are \emph{overloaded} across the various
type class members.
A {\tt typeclass} declaration creates a type class. An {\tt instance}
declaration defines a datatype as belonging to a type class. A
datatype may belong to zero or many type classes.
\com{typeclasses}
The Prelude package declares the following type
classes:
\begin{center}
\begin{tabular}{|p{1in}|p{4 in}|}
\hline
\multicolumn{2}{|c|}{Prelude Type Classes}\\
\hline
\te{Bits}&Types that can be converted to bit vectors and back.\\
\hline
\te{Eq}&Types on which equality is defined.\\
\hline
\te{Literal}&Types which can be created from integer literals.\\
\hline
\te{RealLiteral}&Types which can be created from real literals.\\
\hline
\te{Arith}&Types on which arithmetic operations are defined.\\
\hline
\te{Ord}&Types on which comparison operations are defined.\\
\hline
\te{Bounded}&Types with a finite range.\\
\hline
\te{Bitwise}&Types on which bitwise operations are defined.\\
\hline
\te{BitReduction}&Types on which bitwise operations on a single
operand to produce a single bit result are defined.\\
\hline
\te{BitExtend}&Types on which extend operations are defined.\\
\hline
\te{SaturatingArith}&Types with functions to describe how overflow
and underflow should be handled.\\
\hline
\te{Alias} & Types which can be used interchangeably.\\
\hline
\te{NumAlias} & Types which give a new name to a numeric type.\\
\hline
\te{StrAlias} & Types which give a new name to a string type.\\
\hline
\te{FShow} & Types which can convert a value to a \te{Fmt}
representation for use with \te{\$display} system tasks.\\
\hline
\te{StringLiteral} &Types which can be created around strings.\\
\hline
\end{tabular}
\end{center}
\com{Bits}
\subsubsection{Bits}
\te{Bits} defines the class of types that can be converted to bit
vectors and back. Membership in this class is required for a data
type to be stored in a state, such as a Register or a FIFO, or to be used
at a synthesized module boundary. Often instance of this
class can be automatically derived using the \te{deriving} statement.
\index{Bits@\te{Bits} (type class)}
\index{pack@\texttt{pack} (\texttt{Bits} type class overloaded function)}
\index{unpack@\texttt{unpack} (\texttt{Bits} type class overloaded function)}
\index[function]{Prelude!pack}
\index[function]{Prelude!unpack}
\index[typeclass]{Bits}
\begin{libverbatim}
typeclass Bits #(type a, numeric type n);
function Bit#(n) pack(a x);
function a unpack(Bit#(n) x);
endtypeclass
\end{libverbatim}
Note: the numeric keyword is not required
The functions {\tt pack} and {\tt unpack} are provided to convert
elements to {\tt Bit\#()} and to convert {\tt Bit\#()} elements to another datatype.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Bits} Functions}\\
\hline
\hline
\te{pack}&Converts element {\tt a} of datatype {\tt data\_t} to a
element of datatype {\tt Bit\#()} of {\tt size\_a}. \\
\cline{2-2}
&\\
& \te{function Bit\#(size\_a) pack(data\_t a);}\\
&\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{unpack}&Converts an element {\tt a} of datatype {\tt Bit\#()} and {\tt
size\_a} into an element with of element type {\tt data\_t}.\\
\cline{2-2}
&\\
& \te{function data\_t unpack(Bit\#(size\_a) a);}\\
&\\
\hline
\end{tabular}
\end{center}
{\bf Examples}
\begin{verbatim}
Reg#(int) cycle <- mkReg (0);
..
rule r;
...
if (pack(cycle)[0] == 0) a = a + 1;
else a = a + 2;
if (pack(cycle)[1:0] == 3) a = a + 3;
Int#(10) src_step = unpack(config6[9:0]);
Bool src_rdy_en = unpack(config6[16]);
\end{verbatim}
\com{Eq}
\subsubsection{Eq}
\te{Eq} defines the class of types whose values can be compared for equality.
Instances of the \te{Eq} class are often automatically derived using
the \te{deriving} statement.
\index{Eq@\te{Eq} (type class)}
\index{==@{\te{==}} (\te{Eq} class method)}
\index{/=@{\te{/=}} (\te{Eq} class method)}
\index[function]{Prelude!==}
\index[function]{Prelude!"!=}
\index[typeclass]{Eq}
\begin{libverbatim}
typeclass Eq #(type data_t);
function Bool \== (data_t x, data_t y);
function Bool \/= (data_t x, data_t y);
endtypeclass
\end{libverbatim}
The equality functions {\tt ==} and {\tt !=} are Boolean functions
which return a value of {\tt True} if the equality condition is met.
When defining an instance of an
\te{Eq} typeclass, the \verb'\==' and \verb'\/=' notations must be
used. If using or referring to the functions, the standard {\V} operators {\tt
==} and {\tt !=} may be used.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Eq} Functions}\\
\hline
\hline
\te{==}& Returns {\tt True} if {\tt x} is equal to {\tt y}.\\
\cline{2-2}
&\begin{libverbatim}function Bool \== (data_t x, data_t y,);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{/=}& Returns {\tt True} if {\tt x} is not equal to {\tt y}.\\
\cline{2-2}
&\\
&\begin{libverbatim}function Bool \/= (data_t x, data_t y,);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
{\bf Examples}
\begin{verbatim}
return (pack(i) & 3) == 0;
if (a != maxInt)
\end{verbatim}
\com{Literal}
\subsubsection{Literal}
\label{literal}
\te{Literal} defines the class of types which can be created from
integer literals.
\index{Literal@\te{Literal} (type class)}
\index{fromInteger@\te{fromInteger} (\te{Literal} class method)}
\index[function]{Prelude!fromInteger}
\index[typeclass]{Literal}
\begin{libverbatim}
typeclass Literal #(type data_t);
function data_t fromInteger(Integer x);
function Bool inLiteralRange(data_t target, Integer x);
endtypeclass
\end{libverbatim}
The {\tt fromInteger} function converts an {\tt Integer} into an
element of datatype {\tt data\_t}. Whenever you write an integer
literal in \BSV (such as ``0'' or ``1''), there is an implied \te{fromInteger}
applied to it, which turns the literal into the type you are using it
as (such as \te{Int}, \te{UInt}, \te{Bit}, etc.). By defining an
instance of \te{Literal} for your own datatypes, you can create values
from literals just as for these predefined types.
The typeclass also provides a function \texttt{inLiteralRange} that takes
an argument of the target type and an \texttt{Integer} and returns a
\texttt{Bool} that indicates whether the \texttt{Integer} argument is in
the legal range of the target type. For example, assuming \texttt{x} has type
\texttt{Bit\#(4)}, \texttt{inLiteralRange(x, 15)} would return \texttt{True},
but \texttt{inLiteralRange(x,22)} would return \texttt{False}.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Literal} Functions}\\
\hline
\hline
\te{fromInteger}&Converts an element {\tt x} of datatype {\tt Integer} into an
element of data type \te{data\_t}\\
\cline{2-2}
&\begin{libverbatim} function data_t fromInteger(Integer x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\hline
\te{inLiteralRange}&Tests whether an element {\tt x} of datatype {\tt Integer} is in
the legal range of data type \te{data\_t}\\
\cline{2-2}
&\begin{libverbatim} function Bool inLiteralRange(data_t target, Integer x);
\end{libverbatim}
\\ \hline
\end{tabular}
\end{center}
{\bf Examples}
\begin{verbatim}
function foo (Vector#(n,int) xs) provisos (Log#(n,k));
Integer maxindex = valueof(n) - 1;
Int#(k) index;
index = fromInteger(maxindex);
...
endfunction
function Bool inLiteralRange(RegAddress a, Integer i);
return(i >= 0 && i < 83);
endfunction
\end{verbatim}
\com{RealLiteral}
\subsubsection{RealLiteral}
\label{prelude-realliteral}
\te{RealLiteral} defines the class of types which can be created from
real literals.
\index{RealLiteral@\te{RealLiteral} (type class)}
\index{fromReal@\te{fromReal} (\te{RealLiteral} class method)}
\index[function]{Prelude!fromReal}
\index[typeclass]{RealLiteral}
\begin{libverbatim}
typeclass RealLiteral #(type data_t);
function data_t fromReal(Real x);
endtypeclass
\end{libverbatim}
The {\tt fromReal} function converts a {\tt Real} into an
element of datatype {\tt data\_t}. Whenever you write a real
literal in \BSV (such as ``3.14''), there is an implied \te{fromReal}
applied to it, which turns the real into the specified type.
By defining an
instance of \te{RealLiteral} for a datatype, you can create values
from reals for any type.
% The typeclass also provides a function \texttt{inLiteralRange} that takes
% an argument of the target type and an \texttt{Integer} and returns a
% \texttt{Bool} that indicates whether the \texttt{Integer} argument is in
% the legal range of the target type. For example, assuming \texttt{x} has type
% \texttt{Bit\#(4)}, \texttt{inLiteralRange(x, 15)} would return \texttt{True},
% but \texttt{inLiteralRange(x,22)} would return \texttt{False}.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{RealLiteral} Functions}\\
\hline
\hline
\te{fromReal}&Converts an element {\tt x} of datatype {\tt Real} into an
element of data type \te{data\_t}\\
\cline{2-2}
&\begin{libverbatim} function data_t fromReal(Real x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
% \begin{center}
% \begin{tabular}{|p{1 in}|p{4in}|}
% \hline
% \hline
% \te{inLiteralRange}&Tests whether an element {\tt x} of datatype {\tt Integer} is in
% the legal range of data type \te{data\_t}\\
% \cline{2-2}
% &\begin{libverbatim} function Bool inLiteralRange(data_t target, Integer x);
% \end{libverbatim}
% \\ \hline
% \end{tabular}
% \end{center}
{\bf Examples}
\begin{verbatim}
FixedPoint#(is, fs) f = fromReal(n); //n is a Real number
\end{verbatim}
\subsubsection{SizedLiteral}
\label{sizedliteral}
\te{SizedLiteral} defines the class of types which can be created from
integer literals with a specified size.
\index{SizedLiteral@\te{SizedLiteral} (type class)}
\index{fromSizedInteger@\te{fromSizedInteger} (\te{SizedLiteral} class method)}
\index[function]{Prelude!fromSizedInteger}
\index[typeclass]{SizedLiteral}
\begin{libverbatim}
typeclass SizedLiteral #(type data_t, type size_t)
dependencies (data_t determines size_t);
function data_t fromSizedInteger(Bit#(size_t);
endtypeclass
\end{libverbatim}
The {\tt fromSizedInteger} function converts a literal of type
\te{Bit\#(size\_t)} into an
element of datatype {\tt data\_t}. Whenever you write a sized
literal like \te{1'b0}, there is an implied \te{fromSizedInteger}
which turns the literal into the type you are using it as, with the
defined size. Instances are defined for the types \te{Bit}, \te{UInt}, and \te{Int}.
\begin{center}
\begin{tabular}{|p{1.2 in}|p{3.8in}|}
\hline
\multicolumn{2}{|c|}{\te{SizedLiteral} Functions}\\
\hline
\hline
\te{fromSizedInteger}&Converts an element of \te{Bit\#(size\_t)} into an
element of data type \te{data\_t}\\
\cline{2-2}
&\begin{libverbatim} function data_t fromSizedInteger(Bit#(size_t));
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\com{Arith}
\subsubsection{Arith}
\te{Arith} defines the class of types on which arithmetic operations are defined.
\index{Arith@\te{Arith} (type class)}
\index{+@{\te{+}} (\te{Arith} class method)}
\index{-@{\te{-}} (\te{Arith} class method)}
\index{negate@\te{negate} (\te{Arith} class method)}
\index{*@{\te{*}} (\te{Arith} class method)}
\index{\%@{\te{\%}}(\te{Arith} class mod method)}
\index{/@{\te{/}} (\te{Arith} class div method)}
\index{abs@{\te{abs}} (\te{Arith} class method)}
\index{signum@{\te{signum}} (\te{Arith} class method)}
\index{**@{\te{**}} (\te{Arith} class method)}
\index{exp\_e@{\te{exp\_e}} (\te{Arith} class method)}
\index{log@{\te{log}} (\te{Arith} class method)}
\index{logb@{\te{logb}} (\te{Arith} class method)}
\index{log2@{\te{log2}} (\te{Arith} class method)}
\index{log10@{\te{log10}} (\te{Arith} class method)}
\index[function]{Prelude!+}
\index[function]{Prelude!-}
\index[function]{Prelude!negate}
\index[function]{Prelude!*}
\index[function]{Prelude!\%}
\index[function]{Prelude!/}
\index[function]{Prelude!abs}
\index[function]{Prelude!signum}
\index[function]{Prelude!**}
\index[function]{Prelude!exp}
\index[function]{Prelude!log}
\index[function]{Prelude!logb}
\index[function]{Prelude!log2}
\index[function]{Prelude!log10}
\index[typeclass]{Arith}
\begin{libverbatim}
typeclass Arith #(type data_t)
provisos (Literal#(data_t));
function data_t \+ (data_t x, data_t y);
function data_t \- (data_t x, data_t y);
function data_t negate (data_t x);
function data_t \* (data_t x, data_t y);
function data_t \/ (data_t x, data_t y);
function data_t \% (data_t x, data_t y);
function data_t abs (data_t x);
function data_t signum (data_t x);
function data_t \** (data_t x, data_t y);
function data_t exp_e (data_t x);
function data_t log (data_t x);
function data_t logb (data_t b, data_t x);
function data_t log2 (data_t x);
function data_t log10 (data_t x);
endtypeclass
\end{libverbatim}
The {\tt Arith} functions provide arithmetic operations.
For the arithmetic symbols, when defining an instance of the
\te{Arith} typeclass, the escaped operator names must be used as
shown in the tables below. The \te{negate} name may be used instead
of the operator for negation. If using or referring to these
functions, the standard (non-escaped) {\V}
operators can be used.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Arith} Functions}\\
\hline
\hline
\te{+}&Element {\tt x} is added to element {\tt y}. \\
\cline{2-2}
&\begin{libverbatim}function data_t \+ (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{-}&Element {\tt y} is subtracted from element {\tt x}. \\
\cline{2-2}
&\begin{libverbatim}function data_t \- (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{negate}&Change the sign of the number. When using the function the {\V}
negate operator, {\tt -}, may be used.\\
{\tt -}& \\
\cline{2-2}
&\begin{libverbatim}function data_t negate (data_t x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{*}&Element {\tt x} is multiplied by {\tt y}. \\
\cline{2-2}
&\begin{libverbatim}function data_t \* (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{/}&Element {\tt x} is divided by {\tt y}. The definition depends
on the type - many types truncate the remainder . Note: may not be
synthesizable with downstream tools. \\
\cline{2-2}
&\begin{libverbatim}function data_t \/ (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{\%}&Returns the remainder of $x/y$. Obeys the identity
$((x/y)*y) + (x\%y) = x$.\\
\cline{2-2}
&\begin{libverbatim}function data_t \% (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
Note: Division by 0 is undefined. Both $x/0$ and $x\%0$ will generate
errors at compile-time and run-time for most instances.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{abs}&Returns the absolute value of \te{x}.\\
\cline{2-2}
&\begin{libverbatim}function data_t abs (data_t x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{signum}&Returns a unit value with the same sign as \te{x}, such that
\te{abs(x)*signum(x) = x}. \te{signum(12)} returns \te{1} and
\te{signum(-12)} returns \te{-1}. \\
\cline{2-2}
&\begin{libverbatim}function data_t signum (data_t x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{**}&The element \te{x} is raised to the \te{y} power (\te{x**y} = $x^y$).\\
\cline{2-2}
&\begin{libverbatim}function data_t \** (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{log2}&Returns the base \te{2} logarithm of \te{x} ($\log{_2} x$).\\
\cline{2-2}
&\begin{libverbatim}
function data_t log2(data_t x) ;
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{exp\_e}&\te{e} is raised to the power of \te{x} ($e^x$). \\
\cline{2-2}
&\begin{libverbatim}function data_t exp_e (data_t x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{log}&Returns the base \te{e} logarithm of \te{x} ($\log{_e} x$).\\
\cline{2-2}
&\begin{libverbatim}function data_t log (data_t x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{logb}&Returns the base \te{b} logarithm of \te{x} ($\log{_b} x$).\\
\cline{2-2}
&\begin{libverbatim}function data_t logb (data_t b, data_t x);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{log10}&Returns the base 10 logarithm of x ($\log{_{10}} x$).\\
\cline{2-2}
&\begin{libverbatim}
function data_t log10(data_t x) ;
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
{\bf Examples}
\begin{verbatim}
real u = log(1);
real x = 128.0;
real y = log2(x);
real z = 100.0;
real v = log10(z);
real w = logb(3,9.0);
real a = -x;
real b = abs(x);
\end{verbatim}
\com{Ord}
\subsubsection{Ord}
\label{sec-ord}
\te{Ord} defines the class of types for which an \emph{order} is
defined, allowing comparison operations. A complete definition of an
instance of \te{Ord} requires defining either \te{<=}
or \te{compare}.
\index{Ord@\te{Ord} (type class)}
\index{<@{\te{<}} (\te{Ord} class method)}
\index{<=@{\te{<=}} (\te{Ord} class method)}
\index{>@{\te{>}} (\te{Ord} class method)}
\index{>=@{\te{>=}} (\te{Ord} class method)}
\index[function]{Prelude!<}
\index[function]{Prelude!>}
\index[function]{Prelude!<=}
\index[function]{Prelude!>=}
\index[typeclass]{Ord}
\index[function]{Prelude!compare}
\index[function]{Prelude!min}
\index[function]{Prelude!max}
\index{compare@\te{compare} (\te{Ord} class method)}
\index{min@\te{min} (\te{Ord} class method)}
\index{max@\te{max} (\te{Ord} class method)}
\begin{libverbatim}
typeclass Ord #(type data_t);
function Bool \< (data_t x, data_t y);
function Bool \<= (data_t x, data_t y);
function Bool \> (data_t x, data_t y);
function Bool \>= (data_t x, data_t y);
function Ordering compare(data_t x, data_t y);
function data_t min(data_t x, data_t y);
function data_t max(data_t x, data_t y);
endtypeclass
\end{libverbatim}
The functions \te{<}, \te{<=}, \te{>}, and \te{>=} are Boolean functions which return a
value of {\tt True} if the comparison condition is met.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Ord} Functions}\\
\hline
\hline
\te{<}&Returns {\tt True} if {\tt x} is less than {\tt y}.\\
\cline{2-2}
&\begin{libverbatim}function Bool \< (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{<=}&Returns {\tt True} if {\tt x} is less than or equal to {\tt y}.\\
\cline{2-2}
&\begin{libverbatim}function Bool \<= (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{>}&Returns {\tt True} if {\tt x} is greater than {\tt y}.\\
\cline{2-2}
&\begin{libverbatim}function Bool \> (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{>=}&Returns {\tt True} if {\tt x} is greater than or equal to {\tt y}.\\
\cline{2-2}
&\begin{libverbatim}function Bool \>= (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
The function \te{compare} returns a value of the \te{Ordering}
(Section \ref{sec-ordering}) data type (\te{LT}, \te{GT}, or \te{EQ}).
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{compare}&Returns the \te{Ordering} value describing the
relationship of \te{x} to \te{y}.\\
\cline{2-2}
&\begin{libverbatim}
function Ordering compare (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
The functions \te{min} and \te{max} return a value of datatype
\te{data\_t} which is either the minimum or maximum of the two values,
depending on the function.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{min}&Returns the minimum of the values \te{x} and \te{y}.\\
\cline{2-2}
&\begin{libverbatim}
function data_t min (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{max}&Returns the maximum of the values \te{x} and \te{y}.\\
\cline{2-2}
&\begin{libverbatim}
function data_t max (data_t x, data_t y);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
{\bf Examples}
\begin{verbatim}
rule r1 (x <= y);
rule r2 (x > y);
function Ordering onKey(Record r1, Record r2);
return compare(r1.key,r2.key);
endfunction
...
List#(Record) sorted_rs = sortBy(onKey,rs);
List#(List#(Record)) grouped_rs = groupBy(equiv,sorted_rs);
let read_count = min(reads_remaining, 16);
\end{verbatim}
\com{Bounded}
\subsubsection{Bounded}
\te{Bounded} defines the class of types with a finite range and
provides functions to define the range.
\index{Bounded@\te{Bounded} (type class)}
\index{minBound@\te{minBound} (\te{Bounded} class method)}
\index{maxBound@\te{maxBound} (\te{Bounded} class method)}
\index[function]{Prelude!minBound}
\index[function]{Prelude!maxBound}
\index[typeclass]{Bounded}
\begin{libverbatim}
typeclass Bounded #(type data_t);
data_t minBound;
data_t maxBound;
endtypeclass
\end{libverbatim}
The {\tt Bounded} functions {\tt minBound} and {\tt maxBound} define
the minimum and maximum values for the type {\tt data\_t}.
Instances of the \te{Bounded} class are often automatically derived
using the \te{deriving} statement.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Bounded} Functions}\\
\hline
\hline
\te{minBound}&The minimum value the type {\tt data\_t} can have.\\
\cline{2-2}
&\begin{libverbatim}data_t minBound;
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\te{maxBound}&The maximum value the type {\tt data\_t} can have.\\
\cline{2-2}
&\begin{libverbatim}data_t maxBound;
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
{\bf Examples}
\begin{verbatim}
module mkGenericRandomizer (Randomize#(a))
provisos (Bits#(a, sa), Bounded#(a));
typedef struct {
Bit#(2) red;
Bit#(1) blue;
} RgbColor deriving (Eq, Bits, Bounded);
\end{verbatim}
\com{Bitwise}
\subsubsection{Bitwise}
\te{Bitwise} defines the class of types on which bitwise operations
are defined.
\index{Bitwise@\te{Bitwise} (type class)}
\index{\&@\te{\&} (\te{Bitwise} class method)}
\index{$\mid$ (\te{Bitwise} class method)}
%\index{^@\te{\^} (\te{Bitwise} class method)}
%\index{~^@{\te{~\^}} (\te{Bitwise} class method)}
%\index{~@\te{~} (\te{Bitwise} class method)}
%\index{^~@{\te{\^~}} (\te{Bitwise} class method)}
\index{{\Caret}@\te{\Caret} (\te{Bitwise} class method)}
\index{{\Tilde}{\Caret} (\te{Bitwise} class method)}
\index{{\Caret}{\Tilde} (\te{Bitwise} class method)}
\index{{\Tilde} (\te{Bitwise} class method)}
\index{invert@\te{invert} (\te{Bitwise} class method)}
\index[function]{Prelude!\&}
\index[function]{Prelude!$\mid$}
\index[function]{Prelude!{\Caret}}
\index[function]{Prelude!{\Tilde}}
\index[function]{Prelude!{\Caret}{\Tilde}}
\index[function]{Prelude!{\Tilde}{\Caret}}
\index[function]{Prelude!invert}
\index[typeclass]{Bitwise}
\begin{libverbatim}
typeclass Bitwise #(type data_t);
function data_t \& (data_t x1, data_t x2);
function data_t \| (data_t x1, data_t x2);
function data_t \^ (data_t x1, data_t x2);
function data_t \~^ (data_t x1, data_t x2);
function data_t \^~ (data_t x1, data_t x2);
function data_t invert (data_t x1);
function data_t \<< (data_t x1, x2);
function data_t \>> (data_t x1, x2);
function Bit#(1) msb (data_t x);
function Bit#(1) lsb (data_t x);
endtypeclass
\end{libverbatim}
The {\tt Bitwise} functions compare two operands bit by bit to
calculate a result. That is, the bit in the first operand is compared
to its equivalent bit in the second operand to calculate a single
bit for
the result.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\multicolumn{2}{|c|}{\te{Bitwise} Functions}\\
\hline
\hline
\verb'&'&Performs an {\em and} operation on each bit in {\tt x1} and
{\tt x2} to calculate the result. \\
\cline{2-2}
&\begin{libverbatim}function data_t \& (data_t x1, data_t x2);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\verb'|' &Performs an {\em or} operation on each bit in {\tt x1} and
{\tt x2} to calculate the result. \\
\cline{2-2}
&\begin{libverbatim} function data_t \| (data_t x1, data_t x2);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\verb'^' &Performs an {\em exclusive or} operation on each bit in {\tt x1} and
{\tt x2} to calculate the result. \\
\cline{2-2}
&\begin{libverbatim}function data_t \^ (data_t x1, data_t x2);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\verb'~^' &Performs an {\em exclusive nor} operation on each bit in {\tt x1} and
{\tt x2} to calculate the result. \\
\verb'^~'&\\
\cline{2-2}
&\begin{libverbatim}
function data_t \~^ (data_t x1, data_t x2);
function data_t \^~ (data_t x1, data_t x2);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\verb|~| &Performs a {\em unary negation} operation on each bit in
{\tt x1}. When using this function, the corresponding {\V} operator, \verb|~|, may be used.\\
{\tt invert}& \\
\cline{2-2}
&\begin{libverbatim}function data_t invert (data_t x1);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\index{$<<$ (\te{Bitwise} class method)}
\index{$>>$@\te{$>>$} (\te{Bitwise} class method)}
\index[function]{Prelude!$<<$}
\index[function]{Prelude!$>>$}
The \verb|<<| and \verb|>>| operators perform left and right shift
operations. Whether the shift is an arithmetic shift (\te{Int}) or a
logical shift (\te{Bit}, \te{UInt}) is dependent on how the type is defined.
\begin{center}
\begin{tabular}{|p{1 in}|p{4in}|}
\hline
\verb|<<| &Performs a {\em left shift} operation of {\tt x1} by the number
of bit positions given by {\tt x2}. \te{x2} must be of an
acceptable index type
(\te{Integer}, \te{Bit\#(n)}, \te{Int\#(n)} or \te{UInt\#(n)}).\\
\cline{2-2}
&\begin{libverbatim}function data_t \<< (data_t x1, x2);
\end{libverbatim}
\\
\hline
\end{tabular}
\end{center}
\begin{center}