ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c      written by the UFO converter
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      DOUBLE COMPLEX FUNCTION COND(CONDITION,TRUECASE,FALSECASE)
      IMPLICIT NONE
      DOUBLE COMPLEX CONDITION,TRUECASE,FALSECASE
      IF(CONDITION.EQ.(0.0D0,0.0D0)) THEN
        COND=TRUECASE
      ELSE
        COND=FALSECASE
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION CONDIF(CONDITION,TRUECASE,FALSECASE)
      IMPLICIT NONE
      LOGICAL CONDITION
      DOUBLE COMPLEX TRUECASE,FALSECASE
      IF(CONDITION) THEN
        CONDIF=TRUECASE
      ELSE
        CONDIF=FALSECASE
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION RECMS(CONDITION,EXPR)
      IMPLICIT NONE
      LOGICAL CONDITION
      DOUBLE COMPLEX EXPR
      IF(CONDITION)THEN
        RECMS=EXPR
      ELSE
        RECMS=DCMPLX(DBLE(EXPR))
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION REGLOG(ARG)
      IMPLICIT NONE
      DOUBLE COMPLEX TWOPII
      PARAMETER (TWOPII=2.0D0*3.1415926535897932D0*(0.0D0,1.0D0))
      DOUBLE COMPLEX ARG
      IF(ARG.EQ.(0.0D0,0.0D0)) THEN
        REGLOG=(0.0D0,0.0D0)
      ELSE
        REGLOG=LOG(ARG)
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION REGLOGP(ARG)
      IMPLICIT NONE
      DOUBLE COMPLEX TWOPII
      PARAMETER (TWOPII=2.0D0*3.1415926535897932D0*(0.0D0,1.0D0))
      DOUBLE COMPLEX ARG
      IF(ARG.EQ.(0.0D0,0.0D0))THEN
        REGLOGP=(0.0D0,0.0D0)
      ELSE
        IF(DBLE(ARG).LT.0.0D0.AND.DIMAG(ARG).LT.0.0D0)THEN
          REGLOGP=LOG(ARG) + TWOPII
        ELSE
          REGLOGP=LOG(ARG)
        ENDIF
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION REGLOGM(ARG)
      IMPLICIT NONE
      DOUBLE COMPLEX TWOPII
      PARAMETER (TWOPII=2.0D0*3.1415926535897932D0*(0.0D0,1.0D0))
      DOUBLE COMPLEX ARG
      IF(ARG.EQ.(0.0D0,0.0D0))THEN
        REGLOGM=(0.0D0,0.0D0)
      ELSE
        IF(DBLE(ARG).LT.0.0D0.AND.DIMAG(ARG).GT.0.0D0)THEN
          REGLOGM=LOG(ARG) - TWOPII
        ELSE
          REGLOGM=LOG(ARG)
        ENDIF
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION REGSQRT(ARG_IN)
      IMPLICIT NONE
      DOUBLE COMPLEX ARG_IN
      DOUBLE COMPLEX ARG
      ARG=ARG_IN
      IF(DABS(DIMAG(ARG)).EQ.0.0D0)THEN
        ARG=DCMPLX(DBLE(ARG),0.0D0)
      ENDIF
      IF(DABS(DBLE(ARG)).EQ.0.0D0)THEN
        ARG=DCMPLX(0.0D0,DIMAG(ARG))
      ENDIF
      REGSQRT=SQRT(ARG)
      END

      DOUBLE COMPLEX FUNCTION GRREGLOG(LOGSW,EXPR1_IN,EXPR2_IN)
      IMPLICIT NONE
      DOUBLE COMPLEX TWOPII
      PARAMETER (TWOPII=2.0D0*3.1415926535897932D0*(0.0D0,1.0D0))
      DOUBLE COMPLEX EXPR1_IN,EXPR2_IN
      DOUBLE COMPLEX EXPR1,EXPR2
      DOUBLE PRECISION LOGSW
      DOUBLE PRECISION IMAGEXPR
      LOGICAL FIRSTSHEET
      EXPR1=EXPR1_IN
      EXPR2=EXPR2_IN
      IF(DABS(DIMAG(EXPR1)).EQ.0.0D0)THEN
        EXPR1=DCMPLX(DBLE(EXPR1),0.0D0)
      ENDIF
      IF(DABS(DBLE(EXPR1)).EQ.0.0D0)THEN
        EXPR1=DCMPLX(0.0D0,DIMAG(EXPR1))
      ENDIF
      IF(DABS(DIMAG(EXPR2)).EQ.0.0D0)THEN
        EXPR2=DCMPLX(DBLE(EXPR2),0.0D0)
      ENDIF
      IF(DABS(DBLE(EXPR2)).EQ.0.0D0)THEN
        EXPR2=DCMPLX(0.0D0,DIMAG(EXPR2))
      ENDIF
      IF(EXPR1.EQ.(0.0D0,0.0D0))THEN
        GRREGLOG=(0.0D0,0.0D0)
      ELSE
        IMAGEXPR=DIMAG(EXPR1)*DIMAG(EXPR2)
        FIRSTSHEET=IMAGEXPR.GE.0.0D0
        FIRSTSHEET=FIRSTSHEET.OR.DBLE(EXPR1).GE.0.0D0
        FIRSTSHEET=FIRSTSHEET.OR.DBLE(EXPR2).GE.0.0D0
        IF(FIRSTSHEET)THEN
          GRREGLOG=LOG(EXPR1)
        ELSE
          IF(DIMAG(EXPR1).GT.0.0D0)THEN
            GRREGLOG=LOG(EXPR1) - LOGSW*TWOPII
          ELSE
            GRREGLOG=LOG(EXPR1) + LOGSW*TWOPII
          ENDIF
        ENDIF
      ENDIF
      END

      DOUBLE COMPLEX FUNCTION ARG(COMNUM)
      IMPLICIT NONE
      DOUBLE COMPLEX COMNUM
      DOUBLE COMPLEX IIM
      IIM = (0.0D0,1.0D0)
      IF(COMNUM.EQ.(0.0D0,0.0D0)) THEN
        ARG=(0.0D0,0.0D0)
      ELSE
        ARG=LOG(COMNUM/ABS(COMNUM))/IIM
      ENDIF
      END


      COMPLEX*32 FUNCTION MP_COND(CONDITION,TRUECASE,FALSECASE)
      IMPLICIT NONE
      COMPLEX*32 CONDITION,TRUECASE,FALSECASE
      IF(CONDITION.EQ.(0.0E0_16,0.0E0_16)) THEN
        MP_COND=TRUECASE
      ELSE
        MP_COND=FALSECASE
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_CONDIF(CONDITION,TRUECASE,FALSECASE)
      IMPLICIT NONE
      LOGICAL CONDITION
      COMPLEX*32 TRUECASE,FALSECASE
      IF(CONDITION) THEN
        MP_CONDIF=TRUECASE
      ELSE
        MP_CONDIF=FALSECASE
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_RECMS(CONDITION,EXPR)
      IMPLICIT NONE
      LOGICAL CONDITION
      COMPLEX*32 EXPR
      IF(CONDITION)THEN
        MP_RECMS=EXPR
      ELSE
        MP_RECMS=CMPLX(REAL(EXPR),KIND=16)
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_REGLOG(ARG)
      IMPLICIT NONE
      COMPLEX*32 TWOPII
      PARAMETER (TWOPII=2.0E0_16
     $ *3.14169258478796109557151794433593750E0_16*(0.0E0_16,1.0E0_16))
      COMPLEX*32 ARG
      IF(ARG.EQ.(0.0E0_16,0.0E0_16)) THEN
        MP_REGLOG=(0.0E0_16,0.0E0_16)
      ELSE
        MP_REGLOG=LOG(ARG)
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_REGLOGP(ARG)
      IMPLICIT NONE
      COMPLEX*32 TWOPII
      PARAMETER (TWOPII=2.0E0_16
     $ *3.14169258478796109557151794433593750E0_16*(0.0E0_16,1.0E0_16))
      COMPLEX*32 ARG
      IF(ARG.EQ.(0.0E0_16,0.0E0_16))THEN
        MP_REGLOGP=(0.0E0_16,0.0E0_16)
      ELSE
        IF(REAL(ARG,KIND=16).LT.0.0E0_16.AND.IMAGPART(ARG).LT.0.0E0_16)
     $   THEN
          MP_REGLOGP=LOG(ARG) + TWOPII
        ELSE
          MP_REGLOGP=LOG(ARG)
        ENDIF
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_REGLOGM(ARG)
      IMPLICIT NONE
      COMPLEX*32 TWOPII
      PARAMETER (TWOPII=2.0E0_16
     $ *3.14169258478796109557151794433593750E0_16*(0.0E0_16,1.0E0_16))
      COMPLEX*32 ARG
      IF(ARG.EQ.(0.0E0_16,0.0E0_16))THEN
        MP_REGLOGM=(0.0E0_16,0.0E0_16)
      ELSE
        IF(REAL(ARG,KIND=16).LT.0.0E0_16.AND.IMAGPART(ARG).GT.0.0E0_16)
     $   THEN
          MP_REGLOGM=LOG(ARG) - TWOPII
        ELSE
          MP_REGLOGM=LOG(ARG)
        ENDIF
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_REGSQRT(ARG_IN)
      IMPLICIT NONE
      COMPLEX*32 ARG_IN
      COMPLEX*32 ARG
      ARG=ARG_IN
      IF(ABS(IMAGPART(ARG)).EQ.0.0E0_16)THEN
        ARG=CMPLX(REAL(ARG,KIND=16),0.0E0_16)
      ENDIF
      IF(ABS(REAL(ARG,KIND=16)).EQ.0.0E0_16)THEN
        ARG=CMPLX(0.0E0_16,IMAGPART(ARG))
      ENDIF
      MP_REGSQRT=SQRT(ARG)
      END


      COMPLEX*32 FUNCTION MP_GRREGLOG(LOGSW,EXPR1_IN,EXPR2_IN)
      IMPLICIT NONE
      COMPLEX*32 TWOPII
      PARAMETER (TWOPII=2.0E0_16
     $ *3.14169258478796109557151794433593750E0_16*(0.0E0_16,1.0E0_16))
      COMPLEX*32 EXPR1_IN,EXPR2_IN
      COMPLEX*32 EXPR1,EXPR2
      REAL*16 LOGSW
      REAL*16 IMAGEXPR
      LOGICAL FIRSTSHEET
      EXPR1=EXPR1_IN
      EXPR2=EXPR2_IN
      IF(ABS(IMAGPART(EXPR1)).EQ.0.0E0_16)THEN
        EXPR1=CMPLX(REAL(EXPR1,KIND=16),0.0E0_16)
      ENDIF
      IF(ABS(REAL(EXPR1,KIND=16)).EQ.0.0E0_16)THEN
        EXPR1=CMPLX(0.0E0_16,IMAGPART(EXPR1))
      ENDIF
      IF(ABS(IMAGPART(EXPR2)).EQ.0.0E0_16)THEN
        EXPR2=CMPLX(REAL(EXPR2,KIND=16),0.0E0_16)
      ENDIF
      IF(ABS(REAL(EXPR2,KIND=16)).EQ.0.0E0_16)THEN
        EXPR2=CMPLX(0.0E0_16,IMAGPART(EXPR2))
      ENDIF
      IF(EXPR1.EQ.(0.0E0_16,0.0E0_16))THEN
        MP_GRREGLOG=(0.0E0_16,0.0E0_16)
      ELSE
        IMAGEXPR=IMAGPART(EXPR1)*IMAGPART(EXPR2)
        FIRSTSHEET=IMAGEXPR.GE.0.0E0_16
        FIRSTSHEET=FIRSTSHEET.OR.REAL(EXPR1,KIND=16).GE.0.0E0_16
        FIRSTSHEET=FIRSTSHEET.OR.REAL(EXPR2,KIND=16).GE.0.0E0_16
        IF(FIRSTSHEET)THEN
          MP_GRREGLOG=LOG(EXPR1)
        ELSE
          IF(IMAGPART(EXPR1).GT.0.0E0_16)THEN
            MP_GRREGLOG=LOG(EXPR1) - LOGSW*TWOPII
          ELSE
            MP_GRREGLOG=LOG(EXPR1) + LOGSW*TWOPII
          ENDIF
        ENDIF
      ENDIF
      END

      COMPLEX*32 FUNCTION MP_ARG(COMNUM)
      IMPLICIT NONE
      COMPLEX*32 COMNUM
      COMPLEX*32 IMM
      IMM = (0.0E0_16,1.0E0_16)
      IF(COMNUM.EQ.(0.0E0_16,0.0E0_16)) THEN
        MP_ARG=(0.0E0_16,0.0E0_16)
      ELSE
        MP_ARG=LOG(COMNUM/ABS(COMNUM))/IMM
      ENDIF
      END
