code: def mod_pi2(man, exp, mag, wp):
      # Reduce to standard interval
      if mag > 0:
          i = 0
          while 1:
              cancellation_prec = 20 << i
              wpmod = wp + mag + cancellation_prec
              pi2 = pi_fixed(wpmod - 1)
              pi4 = pi2 >> 1
              offset = wpmod + exp
              if offset >= 0:
                  t = man << offset
              else:
                  t = man >> (-offset)
              n, y = divmod(t, pi2)
              if y > pi4:
                  small = pi2 - y
              else:
                  small = y
              if small >> (wp + mag - 10):
                  n = int(n)
                  t = y >> mag
                  wp = wpmod - mag
                  break
              i += 1
      else:
          wp += -mag
          offset = exp + wp
          if offset >= 0:
              t = man << offset
          else:
              t = man >> (-offset)
          n = 0
      return t, n, wp
  
imports: import math
  from bisect import bisect
  from .backend import xrange
  from .backend import MPZ, MPZ_ZERO, MPZ_ONE, MPZ_TWO, MPZ_FIVE, BACKEND
  from .libmpf import round_floor, round_ceiling, round_down, round_up, round_nearest, round_fast, ComplexResult, bitcount, bctable, lshift, rshift, giant_steps, sqrt_fixed, from_int, to_int, from_man_exp, to_fixed, to_float, from_float, from_rational, normalize, fzero, fone, fnone, fhalf, finf, fninf, fnan, mpf_cmp, mpf_sign, mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_div, mpf_shift, mpf_rdiv_int, mpf_pow_int, mpf_sqrt, reciprocal_rnd, negative_rnd, mpf_perturb, isqrt_fast
  from .libintmath import ifib
label_desc: Use image node_id 46 for calling a global function with example usage: mod_pi2(man, exp, mag, wp) and returns: t,  n,  wp