- BlackBox:
    name: GHC.Integer.Type.divInteger
    kind: Declaration
    type: 'divInteger :: Integer
      -> Integer -> Integer'
    template: |-
      // divInteger begin
      wire ~GENSYM[resultPos][1];
      wire ~GENSYM[dividerNeg][2];
      wire signed [~SIZE[~TYPO]:0] ~GENSYM[dividend2][3];
      wire signed [~SIZE[~TYPO]:0] ~GENSYM[dividendE][4];
      wire signed [~SIZE[~TYPO]:0] ~GENSYM[dividerE][5];
      wire signed [~SIZE[~TYPO]:0] ~GENSYM[quot_res][6];

      assign ~SYM[1] = ~VAR[dividend][0][~SIZE[~TYPO]-1] == ~VAR[divider][1][~SIZE[~TYPO]-1];
      assign ~SYM[2] = ~VAR[divider][1][~SIZE[~TYPO]-1] == 1'b1;
      assign ~SYM[4] = $signed({{~VAR[dividend][0][~SIZE[~TYPO]-1]},~VAR[dividend][0]});  // sign extension
      assign ~SYM[5] = $signed({{~VAR[divider][1][~SIZE[~TYPO]-1]} ,~VAR[divider][1]} );  // sign extension

      assign ~SYM[3] = ~SYM[1] ? ~SYM[4]
                               : (~SYM[2] ? (~SYM[4] - ~SYM[5] - ~SIZE[~TYPO]'sd1)
                                          : (~SYM[4] - ~SYM[5] + ~SIZE[~TYPO]'sd1));

      assign ~SYM[6] = ~SYM[3] / ~SYM[5];
      assign ~RESULT = $signed(~SYM[6][~SIZE[~TYPO]-1:0]);
      // divInteger end
    warning: 'GHC.Integer.Type.divInteger: Integers are dynamically sized in simulation,
      but fixed-length after synthesis. Use carefully.'
- BlackBox:
    name: GHC.Integer.Type.modInteger
    kind: Declaration
    type: 'modInteger :: Integer
      -> Integer -> Integer'
    template: |-
      // modInteger begin
      // remainder
      wire ~SIGD[~GENSYM[rem_res][0]][0];
      assign ~SYM[0] = ~VAR[dividend][0] % ~VAR[divider][1];

      // modulo
      assign ~RESULT = (~VAR[dividend][0][~SIZE[~TYPO]-1] == ~VAR[divider][1][~SIZE[~TYPO]-1]) ?
                       ~SYM[0] :
                       ((~SYM[0] == ~SIZE[~TYPO]'sd0) ? ~SIZE[~TYPO]'sd0 : ~SYM[0] + ~VAR[divider][1]);
      // modInteger end
    warning: 'GHC.Integer.Type.modInteger: Integers are dynamically sized in simulation,
      but fixed-length after synthesis. Use carefully.'
- BlackBox:
    name: GHC.Integer.Type.divModInteger
    kind: Declaration
    type: 'divModInteger ::
      Integer -> Integer -> (# Integer, Integer #)'
    template: |-
      // divModInteger begin
      wire ~GENSYM[resultPos][1];
      wire ~GENSYM[dividerNeg][2];
      wire signed [~SIZE[~TYP[0]]:0] ~GENSYM[dividend2][3];
      wire signed [~SIZE[~TYP[0]]:0] ~GENSYM[dividendE][4];
      wire signed [~SIZE[~TYP[0]]:0] ~GENSYM[dividerE][5];
      wire signed [~SIZE[~TYP[0]]:0] ~GENSYM[quot_res][6];
      wire signed [~SIZE[~TYP[0]]-1:0] ~GENSYM[div_res][7];

      assign ~SYM[1] = ~VAR[dividend][0][~SIZE[~TYP[0]]-1] == ~VAR[divider][1][~SIZE[~TYP[0]]-1];
      assign ~SYM[2] = ~VAR[divider][1][~SIZE[~TYP[0]]-1] == 1'b1;
      assign ~SYM[4] = $signed({{~VAR[dividend][0][~SIZE[~TYP[0]]-1]},~VAR[dividend][0]});  // sign extension
      assign ~SYM[5] = $signed({{~VAR[divider][1][~SIZE[~TYP[0]]-1]} ,~VAR[divider][1]} );  // sign extension

      assign ~SYM[3] = ~SYM[1] ? ~SYM[4]
                               : (~SYM[2] ? (~SYM[4] - ~SYM[5] - ~SIZE[~TYP[0]]'sd1)
                                          : (~SYM[4] - ~SYM[5] + ~SIZE[~TYP[0]]'sd1));

      assign ~SYM[6] = ~SYM[3] / ~SYM[5];
      assign ~SYM[7] = $signed(~SYM[6][~SIZE[~TYP[0]]-1:0]);

      wire ~SIGD[~GENSYM[rem_res][8]][0];
      wire ~SIGD[~GENSYM[mod_res][9]][0];
      assign ~SYM[8] = ~VAR[dividend][0] % ~VAR[divider][1];

      // modulo
      assign ~SYM[9] = (~VAR[dividend][0][~SIZE[~TYP[0]]-1] == ~VAR[divider][1][~SIZE[~TYP[0]]-1]) ?
                       ~SYM[8] :
                       ((~SYM[8] == ~SIZE[~TYP[0]]'sd0) ? ~SIZE[~TYP[0]]'sd0 : ~SYM[8] + ~VAR[divider][1]);

      assign ~RESULT = {~SYM[7],~SYM[9]};
      // divModInteger end
    warning: 'GHC.Integer.Type.divModInteger: Integers are dynamically sized in simulation,
      but fixed-length after synthesis. Use carefully.'
- BlackBox:
    name: GHC.Integer.Type.quotRemInteger
    kind: Declaration
    type: 'quotRemInteger
      :: Integer -> Integer -> (# Integer, Integer #)'
    template: |-
      // quotRemInteger begin
      wire ~SIGD[~GENSYM[quot_res][0]][0];
      wire ~SIGD[~GENSYM[rem_res][1]][0];
      assign ~SYM[0] = ~ARG[0] / ~ARG[1];
      assign ~SYM[1] = ~ARG[0] % ~ARG[1];

      assign ~RESULT = {~SYM[0],~SYM[1]};
      // quotRemInteger end
    warning: 'GHC.Integer.Type.quotRemInteger: Integers are dynamically sized in simulation,
      but fixed-length after synthesis. Use carefully.'
