Commit a272378d1128d1c60a463a315646c86d174ff74c

Authored by Arnaldo Carvalho de Melo
Committed by David S. Miller
1 parent e7a81c6d62

[KTIME]: Introduce ktime_sub_ns and ktime_sub_us

First user will be the DCCP transport networking protocol.

Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 2 changed files with 45 additions and 0 deletions Side-by-side Diff

include/linux/ktime.h
... ... @@ -102,6 +102,13 @@
102 102 #define ktime_add_ns(kt, nsval) \
103 103 ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; })
104 104  
  105 +/*
  106 + * Subtract a scalar nanosecod from a ktime_t variable
  107 + * res = kt - nsval:
  108 + */
  109 +#define ktime_sub_ns(kt, nsval) \
  110 + ({ (ktime_t){ .tv64 = (kt).tv64 - (nsval) }; })
  111 +
105 112 /* convert a timespec to ktime_t format: */
106 113 static inline ktime_t timespec_to_ktime(struct timespec ts)
107 114 {
... ... @@ -200,6 +207,15 @@
200 207 extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);
201 208  
202 209 /**
  210 + * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  211 + * @kt: minuend
  212 + * @nsec: the scalar nsec value to subtract
  213 + *
  214 + * Returns the subtraction of @nsec from @kt in ktime_t format
  215 + */
  216 +extern ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec);
  217 +
  218 +/**
203 219 * timespec_to_ktime - convert a timespec to ktime_t format
204 220 * @ts: the timespec variable to convert
205 221 *
... ... @@ -287,6 +303,11 @@
287 303 static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
288 304 {
289 305 return ktime_add_ns(kt, usec * 1000);
  306 +}
  307 +
  308 +static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
  309 +{
  310 + return ktime_sub_ns(kt, usec * 1000);
290 311 }
291 312  
292 313 /*
... ... @@ -277,6 +277,30 @@
277 277 }
278 278  
279 279 EXPORT_SYMBOL_GPL(ktime_add_ns);
  280 +
  281 +/**
  282 + * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  283 + * @kt: minuend
  284 + * @nsec: the scalar nsec value to subtract
  285 + *
  286 + * Returns the subtraction of @nsec from @kt in ktime_t format
  287 + */
  288 +ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
  289 +{
  290 + ktime_t tmp;
  291 +
  292 + if (likely(nsec < NSEC_PER_SEC)) {
  293 + tmp.tv64 = nsec;
  294 + } else {
  295 + unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  296 +
  297 + tmp = ktime_set((long)nsec, rem);
  298 + }
  299 +
  300 + return ktime_sub(kt, tmp);
  301 +}
  302 +
  303 +EXPORT_SYMBOL_GPL(ktime_sub_ns);
280 304 # endif /* !CONFIG_KTIME_SCALAR */
281 305  
282 306 /*