.TH MP 2
.SH NAME
-mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, strtomp, mpfmt,mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpmodadd, mpmodsub, mpmodmul, mpdiv, mpcmp, mpsel, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic
+mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, strtomp, mpfmt, mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mptod, dtomp, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpmodadd, mpmodsub, mpmodmul, mpdiv, mpcmp, mpsel, mpfactorial, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic
.SH SYNOPSIS
.B #include <u.h>
.br
uvlong mptouv(mpint*)
.PP
.B
+mpint* dtomp(double, mpint*)
+.PP
+.B
+double mptod(mpint*)
+.PP
+.B
void mpadd(mpint *b1, mpint *b2, mpint *sum)
.PP
.B
void mpright(mpint *b, int shift, mpint *res)
.PP
.B
+void mpand(mpint *b1, mpint *b2, mpint *res)
+.PP
+.B
+void mpbic(mpint *b1, mpint *b2, mpint *res)
+.PP
+.B
+void mpor(mpint *b1, mpint *b2, mpint *res)
+.PP
+.B
+void mpnot(mpint *b, mpint *res)
+.PP
+.B
+void mpxor(mpint *b1, mpint *b2, mpint *res)
+.PP
+.B
+void mptrunc(mpint *b, int n, mpint *res)
+.PP
+.B
+void mpxtend(mpint *b, int n, mpint *res)
+.PP
+.B
+void mpasr(mpint *b, int n, mpint *res)
+.PP
+.B
void mpmul(mpint *b1, mpint *b2, mpint *prod)
.PP
.B
void mpsel(int s, mpint *b1, mpint *b2, mpint *res)
.PP
.B
+mpint* mpfactorial(ulong n)
+.PP
+.B
void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x,
.br
.B
.IR strtomp ,
.IR itomp ,
.IR uitomp ,
+.IR btomp ,
and
-.IR btomp .
+.IR dtomp .
These functions, in addition to
.I mpnew
and
.B mpint
representations using the base indicated.
Only the bases 2, 4, 8, 10, 16, 32, and 64 are
-supported. Base 0 defaults to 16.
+supported.
.IR Strtomp
skips any leading spaces or tabs.
.IR Strtomp 's
return
.BR nil .
.I Mptoa
-returns a pointer to the filled buffer.
+returns a pointer to the
+.SM ASCII
+filled buffer.
If the parameter
.I buf
is
.BR nil ,
the buffer is allocated.
+Setting
+.I base
+to zero uses hexadecimal default.
.I Mpfmt
can be used with
.IR fmtinstall (2)
and
.IR print (2)
-to print hexadecimal representations of
+to print
+.SM ASCII
+representations of
.BR mpint s.
The conventional verb is
.LR B ,
.I mp.h
provides a
.LR pragma .
+The precision in the format string changes the base,
+defaulting to hexadecimal when omited.
.PP
.I Mptobe
and
.BR nil ,
a new integer is allocated and returned as the result.
.PP
-The integer conversions are:
+The integer (and floating point) conversions are:
.TF Mptouv
.TP
.I mptoui
.TP
.I vtomp
.BR "vlong" -> mpint
+.TP
+.I mptod
+.BR mpint -> "double"
+.TP
+.I dtomp
+.BR "double" -> mpint
.PD
.PP
When converting to the base integer types, if the integer is too large,
the largest integer of the appropriate sign
and size is returned.
.PP
+When converting to and from floating point, results are rounded using IEEE 754 "round to nearest".
+If the integer is too large in magnitude,
+.I mptod
+returns infinity of the appropriate sign.
+.PP
The mathematical functions are:
-.TF mpmagadd
+.TF mpfactorial
.TP
.I mpadd
.BR "sum = b1 + b2" .
.I b2
is assigned to
.IR res .
+.TP
+.I mpfactorial
+returns \fIn\fR!.
+.PD
+.PP
+Logical operations (treating negative numbers using two's complement):
+.TF mpxtend_
+.TP
+.I mpand
+.BR "res = b1 & b2" .
+.TP
+.I mpbic
+.BR "res = b1 & ~b2" .
+.TP
+.I mpor
+.BR "res = b1 | b2" .
+.TP
+.I mpxor
+.BR "res = b1 ^ b2" .
+.TP
+.I mpnot
+.BR "res = ~b1" .
+.TP
+.I mpasr
+.BR "res = b>>shift"
+(\fImpasr\fR, unlike
+.IR mpright ,
+uses two's complement).
+.TP
+.I mptrunc
+truncates
+.I b
+to
+.I n
+bits and stores the result in
+.IR res .
+The result is never negative.
+.TP
+.I mpxtend
+truncates
+.I b
+to
+.I n
+bits, sign extends the MSB and stores the result in
+.IR res .
.PD
.PP
Modular arithmetic: