API Reference

Note

The full README is available on GitHub.

Typedefs

typedef struct gate gate_t
typedef enum gate_kind_t gate_kind_t

Gate kinds

Enums

enum gate_kind_t

Gate kinds

Values:

enumerator NAND
enumerator AND
enumerator OR
enumerator NOR
enumerator XOR
enumerator XNOR

Functions

gate_t *gate_new(gate_kind_t kind, unsigned n)

Creates a new gate of the specified type with n inputs.

Parameters:
  • kind – The type of gate to create (e.g. NAND, AND, OR).

  • n – The number of inputs for the gate.

Returns:

  • Pointer to the created gate structure on success.

  • NULL if memory allocation fails (errno is set to ENOMEM).

void gate_delete(gate_t *g)

Deletes the specified gate.

Disconnects all input and output signals from the gate and frees its allocated memory. Does nothing if g is NULL. After deletion, the pointer g becomes invalid.

Parameters:

g – Pointer to the gate to delete.

int gate_connect_gate(gate_t *g_out, gate_t *g_in, unsigned k)

Connects the output of one gate to the input of another gate.

Connects the output of g_out to the k-th input of g_in. Any signal previously connected to the k-th input of g_in will be disconnected.

Parameters:
  • g_out – Pointer to the output gate.

  • g_in – Pointer to the input gate.

  • k – Index of the input in g_in to connect.

Returns:

  • 0 on success.

  • -1 if any pointer is NULL, k is invalid, or memory allocation fails (errno is set to EINVAL or ENOMEM).

int gate_connect_signal(bool const *s, gate_t *g, unsigned k)

Connects a boolean signal to the input of a gate.

Connects a boolean signal s to the k-th input of gate g. Any signal previously connected to the k-th input will be disconnected.

Parameters:
  • s – Pointer to the constant boolean signal.

  • g – Pointer to the gate.

  • k – Index of the input in g to connect.

Returns:

  • 0 on success.

  • -1 if any pointer is NULL, k is invalid, or memory allocation fails (errno is set to EINVAL or ENOMEM).

ssize_t gate_evaluate(gate_t **g, bool *s, size_t m)

Evaluates the output signals of the specified gates and calculates the critical path length.

Computes the output signals of the gates in g and stores them in the s array. Also calculates the maximum critical path length for the given gates.

Parameters:
  • g – Array of pointers to gates.

  • s – Array to store the output signals of the gates.

  • m – Size of the g and s arrays.

Returns:

  • Critical path length on success (also populates the s array). s[i] contains the value of the signal at the output of the gate pointed to by g[i].

  • -1 if any pointer is NULL, m is zero, operation failed or memory allocation fails (errno is set to EINVAL, ECANCELED, or ENOMEM).

ssize_t gate_fan_out(gate_t const *g)

Returns the fan-out of the specified gate.

Calculates the number of inputs in other gates connected to the output of the given gate.

Parameters:

g – Pointer to the gate.

Returns:

  • The number of connected inputs on success.

  • -1 if g is NULL (errno is set to EINVAL).

ssize_t gate_fan_in(gate_t const *g)

Returns the fan-in of the specified gate.

Parameters:

g – Pointer to the gate.

Returns:

  • The number of inputs of the gate.

  • -1 if g is NULL (errno is set to EINVAL).

void *gate_input(gate_t const *g, unsigned k)

Retrieves the signal or gate connected to a specific input.

Returns a pointer to the boolean signal or gate connected to the k-th input of the gate g. Returns NULL if nothing is connected to the input.

Parameters:
  • g – Pointer to the gate.

  • k – Index of the input.

Returns:

  • Pointer to a bool* or gate_t* on success.

  • NULL if no connection exists (errno is set to 0) = NULL if g is NULL, or k is invalid (errno is set to EINVAL).

gate_t *gate_output(gate_t const *g, ssize_t k)

Retrieves a gate connected to the output of the specified gate.

Parameters:
  • g – Pointer to the gate.

  • k – Index of the connection to retrieve (from 0 to gate_fan_out(g) - 1).

Returns:

  • Pointer to the connected gate.

  • NULL if parameters are invalid or the output is not connected to any gate.