// file: bw_add_0.cc
//

// isip include files
//
#include "bw_train.h"
#include "bw_train_constants.h"

// method: log_add_cc
//
// arguments:
//  float_8 x : (input) the variable x
//  float_8 y : (input) the variable y
//
// return: a float_8 value resulting from the addition
//
// this method inputs x = log(a), y = log(b) and returns log(a+b)
//
float_8 log_add_cc(float_8 x_a, float_8 y_a) {

  // local variables
  //
  float_8 diff = (float_8)0.0;
  float_8 z = (float_8)0.0;

  // compute log(a+b)
  //
  if (y_a < x_a) {

    // compute negative difference
    //
    diff = y_a - x_a;
    if (diff < BW_MINUS_LOG_INF) {  
      z = (x_a < BW_LOG_MIN) ? BW_LOG_ZERO : x_a;
    }
    else {
      z = log(1.0 + exp(diff)) + x_a;
    }
  }
  else {

    // compute negative difference
    //
    diff = x_a - y_a;
    if (diff < BW_MINUS_LOG_INF) {
      z = (y_a < BW_LOG_MIN) ? BW_LOG_ZERO : y_a;
    }
    else {
      z = log(1.0 + exp(diff)) + y_a;
    }
  }

  // return gracefully
  //
  return z;
}
