// PiDecimal_GDA calculates π to the desired precision using
// github.com/ericlagergren/decimal with the operating mode set to GDA.
427207
func PiDecimal_GDA(prec int) *decimal.Big {
	var (
		ctx = decimal.Context{
			Precision:     adjustPrecision(prec),
			OperatingMode: decimal.GDA,
		}

		lasts = new(decimal.Big)
		t     = decimal.New(3, 0)
		s     = decimal.New(3, 0)
		n     = decimal.New(1, 0)
		na    = new(decimal.Big)
		d     = new(decimal.Big)
		da    = decimal.New(24, 0)
	)

	for s.Cmp(lasts) != 0 {
		lasts.Copy(s)
		ctx.Add(n, n, na)
		ctx.Add(na, na, eight)
		ctx.Add(d, d, da)
		ctx.Add(da, da, thirtyTwo)
		ctx.Mul(t, t, n)
		ctx.Quo(t, t, d)
		ctx.Add(s, s, t)
	}
	return s.Round(prec)
}
535816
func (b *builtinRoundDecSig) evalDecimal(row chunk.Row) (*types.MyDecimal, bool, error) {
	val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
	if isNull || err != nil {
		return nil, isNull, err
	}
	to := new(types.MyDecimal)
	if err = val.Round(to, 0, types.ModeHalfEven); err != nil {
		return nil, true, err
	}
	return to, false, nil
}
479050
func (col *Column) EvalDecimal(ctx sessionctx.Context, row chunk.Row) (*types.MyDecimal, bool, error) {
	if row.IsNull(col.Index) {
		return nil, true, nil
	}
	return row.GetMyDecimal(col.Index), false, nil
}
497382
func Pi(z *decimal.Big) *decimal.Big {
	return pi(z, decimal.Context{Precision: precision(z)})
}
996188
func (sf *ScalarFunction) EvalDecimal(ctx sessionctx.Context, row chunk.Row) (*types.MyDecimal, bool, error) {
	return sf.Function.evalDecimal(row)
}
33667
func PiInf(prec int) *inf.Dec {
	var (
		lasts = inf.NewDec(0, 0)
		t     = inf.NewDec(3, 0)
		s     = inf.NewDec(3, 0)
		n     = inf.NewDec(1, 0)
		na    = inf.NewDec(0, 0)
		d     = inf.NewDec(0, 0)
		da    = inf.NewDec(24, 0)

		work = adjustPrecision(prec)
	)

	for s.Cmp(lasts) != 0 {
		lasts.Set(s)
		n.Add(n, na)
		na.Add(na, infEight)
		d.Add(d, da)
		da.Add(da, infThirtyTwo)
		t.Mul(t, n)
		t.QuoRound(t, d, inf.Scale(work), inf.RoundHalfUp)
		s.Add(s, t)
	}
	// -1 because inf's precision == digits after radix
	return s.Round(s, inf.Scale(prec-1), inf.RoundHalfUp)
}
431050
func calcID(n *ast.MsgNode) uint64 {
	var buf bytes.Buffer
	writeFingerprint(&buf, n, false)

	var fp = fingerprint(buf.Bytes())
	if n.Meaning != "" {
		var topbit uint64
		if fp&(1<<63) > 0 {
			topbit = 1
		}
		fp = (fp << 1) + topbit + fingerprint([]byte(n.Meaning))
	}

	return fp & 0x7fffffffffffffff
}
104101
func Pids() ([]int32, error) {
	var ret []int32
	pid, err := process.Pids()
	if err != nil {
		return ret, err
	}

	return pid, err
}
434218
func BGRA(p []uint8) {
	for i := 0; i < len(p); i += 4 {
		p[i+0], p[i+2] = p[i+2], p[i+0]
	}
}
1064217
func (device *XlibDevice) DebugSetPrecision(precision int) {
	C.cairo_xlib_device_debug_set_precision(device.Ptr, C.int(precision))
	if err := device.status(); err != nil {
		panic(err)
	}
}
893026
func PiShopSpring(prec int32) ssdec.Decimal {
	var (
		lasts = ssdec.New(0, 0)
		t     = ssdec.New(3, 0)
		s     = ssdec.New(3, 0)
		n     = ssdec.New(1, 0)
		na    = ssdec.New(0, 0)
		d     = ssdec.New(0, 0)
		da    = ssdec.New(24, 0)

		work = int32(adjustPrecision(int(prec)))
	)

	for s.Cmp(lasts) != 0 {
		lasts = s
		n = n.Add(na)
		na = na.Add(ssdecEight)
		d = d.Add(da)
		da = da.Add(ssdecThirtyTwo)
		t = t.Mul(n)
		t = t.DivRound(d, work)
		s = s.Add(t)
	}
	// -1 because shopSpring's prec == digits after radix
	return s.Round(prec - 1)
}
1025211
func ppnd(p float64) float64 {
	q := p - 0.5
	var r float64
	if math.Abs(q) <= split1 {
		r = const1 - q*q
		return q * (((a3_p*r+a2_p)*r+a1_p)*r + a0_p) / (((b3_p*r+b2_p)*r+b1_p)*r + 1.0)
	} else {
		if q < 0.0 {
			r = p
		} else {
			r = 1.0 - p
		}
		if r <= 0.0 {
			return 0.0
		}
		r = math.Sqrt(-math.Log(r))
		var normal_dev float64
		if r <= split2 {
			r -= const2
			normal_dev = (((c3_p*r+c2_p)*r+c1_p)*r + c0_p) / ((d2_p*r+d1_p)*r + 1.0)
		} else {
			r -= split2
			normal_dev = (((e3_p*r+e2_p)*r+e1_p)*r + e0_p) / ((f2_p*r+f1_p)*r + 1.0)
		}
		if q < 0.0 {
			normal_dev = -normal_dev
		}
		return normal_dev
	}
}
11888
func Digits(digits int) string {
	max := int(math.Pow10(digits)) - 1
	num := privateRand.Intn(max)
	format := fmt.Sprintf("%%0%dd", digits)
	return fmt.Sprintf(format, num)
}
42334
func (b *builtinRoundWithFracDecSig) evalDecimal(row chunk.Row) (*types.MyDecimal, bool, error) {
	val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
	if isNull || err != nil {
		return nil, isNull, err
	}
	frac, isNull, err := b.args[1].EvalInt(b.ctx, row)
	if isNull || err != nil {
		return nil, isNull, err
	}
	to := new(types.MyDecimal)
	if err = val.Round(to, mathutil.Min(int(frac), b.tp.Decimal), types.ModeHalfEven); err != nil {
		return nil, true, err
	}
	return to, false, nil
}
427164
func GetPrecisionMultiplier(precision string) int64 {
	d := time.Nanosecond
	switch precision {
	case "us":
		d = time.Microsecond
	case "ms":
		d = time.Millisecond
	case "s":
		d = time.Second
	}
	return int64(d)
}
758134
func RoundDec32(x float32, prec int) float32 {
	if prec < 1 {
		return x
	}
	dec1 := float32(math.Pow10(prec))
	dec2 := float32(math.Pow10(prec + 1))
	tmp := int(x * dec1)
	last := int(x*dec2) - tmp*10
	if norm(last) >= 5 && last >= 0 {
		tmp += 1
	} else if norm(last) >= 5 && last < 0 {
		tmp -= 1
	}
	return float32(tmp) / dec1
}
495898
func Decimal(num []byte, prec int) []byte {
	// omit first + and register mantissa start and end, whether it's negative and the exponent
	neg := false
	start := 0
	dot := -1
	end := len(num)
	if 0 < end && (num[0] == '+' || num[0] == '-') {
		if num[0] == '-' {
			neg = true
		}
		start++
	}
	for i, c := range num[start:] {
		if c == '.' {
			dot = start + i
			break
		}
	}
	if dot == -1 {
		dot = end
	}

	// trim leading zeros but leave at least one digit
	for start < end-1 && num[start] == '0' {
		start++
	}
	// trim trailing zeros
	i := end - 1
	for ; i > dot; i-- {
		if num[i] != '0' {
			end = i + 1
			break
		}
	}
	if i == dot {
		end = dot
		if start == end {
			num[start] = '0'
			return num[start : start+1]
		}
	} else if start == end-1 && num[start] == '0' {
		return num[start:end]
	}

	// apply precision
	if prec > -1 && dot+1+prec < end {
		end = dot + 1 + prec
		inc := num[end] >= '5'
		if inc || num[end-1] == '0' {
			// process either an increase from a lesser significant decimal (>= 5)
			// or remove trailing zeros after the dot, or both
			for i := end - 1; i > start; i-- {
				if i == dot {
					end--
				} else if inc {
					if num[i] == '9' {
						if i > dot {
							end--
						} else {
							num[i] = '0'
							break
						}
					} else {
						num[i]++
						inc = false
						break
					}
				} else if i > dot && num[i] == '0' {
					end--
				} else {
					break
				}
			}
		}
		if dot == start && end == start+1 {
			if inc {
				num[start] = '1'
			} else {
				num[start] = '0'
			}
		} else {
			if dot+1 == end {
				end--
			}
			if inc {
				if num[start] == '9' {
					num[start] = '0'
					copy(num[start+1:], num[start:end])
					end++
					num[start] = '1'
				} else {
					num[start]++
				}
			}
		}
	}

	if neg {
		start--
		num[start] = '-'
	}
	return num[start:end]
}