linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Keith Packard" <keithp@keithp.com>
To: Daniel Vetter <daniel@ffwll.ch>
Cc: linux-kernel@vger.kernel.org, Dave Airlie <airlied@redhat.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	dri-devel@lists.freedesktop.org
Subject: Re: [PATCH 1/3] drm: Widen vblank UAPI to 64 bits. Change vblank time to ktime_t [v2]
Date: Sun, 06 Aug 2017 13:35:03 -0400	[thread overview]
Message-ID: <87wp6geh14.fsf@keithp.com> (raw)
In-Reply-To: <20170802085358.cipsolpgxlb2e323@phenom.ffwll.local>


[-- Attachment #1.1: Type: text/plain, Size: 1749 bytes --]

Daniel Vetter <daniel@ffwll.ch> writes:

> Subject is a bit confusing since you say uapi, but this is just the
> internal prep work. Dropping UAPI fixes that. With that fixed:

Yeah, thanks.

> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

Added.

> Two more optional comments below, feel free to adapt or ignore. I'll wait
> for Michel's r-b before merging either way.
>
>>  static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
>> +				  u64 req_seq,
>>  				  union drm_wait_vblank *vblwait,
>
> Minor bikeshed: Since you pass the requested vblank number explicit, mabye
> also pass the user_data explicit and remove the vblwait struct from the
> parameter list? Restricts the old uapi cruft a bit.

I also need to re-write the reply.sequence value in the queue
function; seems like passing in the vblwait is a simpler plan.

>> +static u64 widen_32_to_64(u32 narrow, u64 near)
>> +{
>> +	u64 wide = narrow | (near & 0xffffffff00000000ULL);
>> +	if ((int64_t) (wide - near) > 0x80000000LL)
>> +		wide -= 0x100000000ULL;
>> +	else if ((int64_t) (near - wide) > 0x80000000LL)
>> +		wide += 0x100000000ULL;
>> +	return wide;
>
> return near + (int32_s) ((uint32_t)wide - near) ?

Oh, yes, that makes perfect sense -- an int32_t will obviously hold the
shortest distance between the two, whether negative or positive. Of
course, '(uint32_t) wide' is just 'narrow'.

> But then it took me way too long to think about this one, so maybe leave
> it at that.

Your version is a lot shorter, and I think it's actually clearer. How
about

static inline uint64_t widen_32_to_64(uint32_t narrow, uint64_t near)
{
	return near + (int32_t) (narrow - (uint32_t) near);
}

Here's a test program which validates the widen function.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: near.c --]
[-- Type: text/x-csrc, Size: 1989 bytes --]

#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

static inline uint64_t widen_32_to_64(uint32_t narrow, uint64_t near)
{
	return near + (int32_t) (narrow - (uint32_t) near);
}

uint64_t
random_u64(void) {
	assert(sizeof (long int) == 8);
	return (uint32_t) mrand48() | (((uint64_t) (uint32_t) mrand48()) << 32);
}

uint32_t
random_u32(int bits) {
	return random_u64() & ((1UL << bits) - 1);
}

int32_t
random_s32(int bits) {
	int32_t	value = (int32_t) random_u32(bits);

	return value << (32 - bits) >> (32 - bits);
}

int
main(int argc, char **argv) {
	int bits;
	int tries;

	/* Validate random number generators */
	for (bits = 1; bits <= 32; bits++) {
		int64_t	max = ((1L << (bits-1)) - 1);
		int32_t min = -max - 1;
		int32_t range_min = INT32_MAX;
		int32_t range_max = INT32_MIN;

		for (tries = 0; tries < 100000; tries++) {
			int32_t	i = random_s32(bits);

			if (i < min || max < i) {
				printf ("min %d i %d max %d\n", min, i, max);
				exit(1);
			}
			if (i < range_min)
				range_min = i;
			if (i > range_max)
				range_max = i;
		}
		if (range_min >= min/2 || (range_max <= max/2 && max != 0)) {
			printf ("bits %d min %d max %d range min %d range max %d\n",
				bits, min, max, range_min, range_max);
			exit(1);
		}
	}

	/* Check to make sure the 'widen' function generates the right answer */
	for (bits = 1; bits < 32; bits++) {
		for (tries = 0; tries < 100000; tries++) {

			/* A random 64-bit value */
			uint64_t	near = random_u64();

			/* Compute a nearby value, within a 32-bit delta of the target*/
			int32_t		delta = random_s32(bits);
			uint64_t	value = near + delta;

			/* Narrow the value to 32-bits */
			uint32_t	narrow = (uint32_t) (value);

			/* Use our 'widen' function to reconstruct the wider value */
			uint64_t	wide = widen_32_to_64(narrow, near);

			/* Make sure the reconstruction worked */
			if (wide != value)
				printf ("widen failed near %ld value %ld wide %ld\n",
					near, value, wide);
		}
	}
}

[-- Attachment #1.3: Type: text/plain, Size: 15 bytes --]


-- 
-keith

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

  parent reply	other threads:[~2017-08-06 17:35 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-05 22:10 [PATCH 0/3] drm: Add CRTC-id based ioctls for vblank query/event Keith Packard
2017-07-05 22:10 ` [PATCH 1/3] drm: Widen vblank count to 64 bits. Change vblank time precision to ns Keith Packard
2017-07-06  7:19   ` Daniel Vetter
2017-07-06 14:59     ` Keith Packard
2017-07-07 12:16       ` Daniel Vetter
2017-07-25 20:54         ` Keith Packard
2017-07-06  7:45   ` Michel Dänzer
2017-07-06  8:05     ` Michel Dänzer
2017-07-06 15:11       ` Keith Packard
2017-07-06 15:04     ` Keith Packard
2017-07-07  1:34       ` Michel Dänzer
2017-07-07  2:05         ` Michel Dänzer
2017-07-05 22:10 ` [PATCH 2/3] drm: Reorganize drm_pending_event to support future event types Keith Packard
2017-07-06  7:30   ` Daniel Vetter
2017-07-06 15:36     ` Keith Packard
2017-07-07 12:05       ` Daniel Vetter
2017-07-05 22:10 ` [PATCH 3/3] drm: Add CRTC_GET_SEQUENCE and CRTC_QUEUE_SEQUENCE ioctls Keith Packard
2017-07-06  7:53   ` Daniel Vetter
2017-07-06 10:16     ` Ville Syrjälä
2017-07-06 11:04       ` Daniel Vetter
2017-07-06 14:08         ` Ville Syrjälä
2017-07-06 16:28           ` Keith Packard
2017-07-06 17:59             ` Ville Syrjälä
2017-07-06 18:22               ` Keith Packard
2017-07-06 18:59                 ` Ville Syrjälä
2017-07-06 19:46                   ` Keith Packard
2017-07-06 16:27     ` Keith Packard
2017-07-06 21:49       ` Daniel Vetter
2017-08-01  5:03 ` [PATCH 0/3] drm: Add CRTC-id based ioctls for vblank query/event Keith Packard
2017-08-01  5:03   ` [PATCH 1/3] drm: Widen vblank UAPI to 64 bits. Change vblank time to ktime_t [v2] Keith Packard
2017-08-02  8:53     ` Daniel Vetter
2017-08-02  9:41       ` Michel Dänzer
2017-08-06 17:35       ` Keith Packard [this message]
2017-08-01  5:03   ` [PATCH 2/3] drm: Reorganize drm_pending_event to support future event types [v2] Keith Packard
2017-08-02  9:05     ` Daniel Vetter
2017-08-01  5:03   ` [PATCH 3/3] drm: Add CRTC_GET_SEQUENCE and CRTC_QUEUE_SEQUENCE ioctls [v2] Keith Packard
2017-08-02  9:25     ` Daniel Vetter
2017-08-06  3:32       ` Keith Packard
2017-08-07  3:02         ` Michel Dänzer
2017-08-07  8:34         ` Daniel Vetter
2017-10-09 17:18           ` Keith Packard
2017-10-10  8:55             ` Daniel Vetter
2017-08-02  9:45     ` Michel Dänzer
2017-08-06  3:42       ` Keith Packard
2017-08-07  3:03         ` Michel Dänzer
  -- strict thread matches above, loose matches on Subject: below --
2017-10-11  0:45 [PATCH 0/3] drm: add new vblank ioctls [v3] Keith Packard
2017-10-11  0:45 ` [PATCH 1/3] drm: Widen vblank UAPI to 64 bits. Change vblank time to ktime_t [v2] Keith Packard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wp6geh14.fsf@keithp.com \
    --to=keithp@keithp.com \
    --cc=airlied@redhat.com \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).